Specflow步骤定义继承

时间:2011-03-16 13:43:58

标签: specflow

我有ExcelStepDefinition类,其中包括我的excel测试步骤。我也有WordStepDefinition类。由于我有两个类相同的大量步骤,我创建了一些StepDefinition类,它将成为这两个类的基类。

在这个基类中,我需要在构造函数中有一些args,这取决于实例化的类(excel或word)。我已经做了所有这些,但是当我在Nunit中开始测试时,它失败并跟随堆栈跟踪:

System.IndexOutOfRangeException : Index was outside the bounds of the array.
TearDown : System.IndexOutOfRangeException : Index was outside the bounds of the array.
at TechTalk.SpecFlow.ScenarioContext.GetBindingInstance(Type bindingType)
at TechTalk.SpecFlow.ScenarioContext.GetBindingInstance(Type bindingType)
at lambda_method(ExecutionScope )
at TechTalk.SpecFlow.Bindings.MethodBinding.InvokeAction(Object[] arguments, ITestTracer testTracer, TimeSpan& duration)
at TechTalk.SpecFlow.TestRunner.FireEvents(BindingEvent bindingEvent, IEnumerable`1 tags)
at TechTalk.SpecFlow.TestRunner.FireScenarioEvents(BindingEvent bindingEvent)
at TechTalk.SpecFlow.TestRunner.OnScenarioStart(ScenarioInfo scenarioInfo)
at ABZ.ExcelTest.DisplayValueOfLinkedItemUsingFormattingRulesDefinedForAGivenLanguageFeature.ScenarioSetup(ScenarioInfo scenarioInfo) in D:\Projects\VS2008\ABZ\ABZ Report Office\ABZ.ExcelTest\ExcelSwitchLanguage.feature.cs:line 0
at ABZ.ExcelTest.DisplayValueOfLinkedItemUsingFormattingRulesDefinedForAGivenLanguageFeature.DisplayFactValueWithFormattingDefinedInSelectedLanguage(String cell, String column, String label, String lang, String cellValue) in d:\Projects\VS2008\ABZ\ABZ Report Office\ABZ.ExcelTest\ExcelSwitchLanguage.feature:line 23
--TearDown
at TechTalk.SpecFlow.ScenarioContext.GetBindingInstance(Type bindingType)
at TechTalk.SpecFlow.ScenarioContext.GetBindingInstance(Type bindingType)
at lambda_method(ExecutionScope )
at TechTalk.SpecFlow.Bindings.MethodBinding.InvokeAction(Object[] arguments, ITestTracer testTracer, TimeSpan& duration)
at TechTalk.SpecFlow.TestRunner.FireEvents(BindingEvent bindingEvent, IEnumerable`1 tags)
at TechTalk.SpecFlow.TestRunner.FireScenarioEvents(BindingEvent bindingEvent)
at TechTalk.SpecFlow.TestRunner.OnScenarioEnd()
at ABZ.ExcelTest.DisplayValueOfLinkedItemUsingFormattingRulesDefinedForAGivenLanguageFeature.ScenarioTearDown() in D:\Projects\VS2008\ABZ\ABZ Report Office\ABZ.ExcelTest\ExcelSwitchLanguage.feature.cs:line 0

以下是基类和派生类(只是定义和构造函数):

// base class
[Binding]
    public class StepDefinition : Steps
    {
        IOfficeAppDriver officeAppDriver ;
        public StepDefinition(IReportFactoryAddInGuiElements repo, string application)
        {
            officeAppDriver = new OfficeAppDriver(new ReportFactoryOfficeAddInDriver(repo), application);
        }

// derivded one
[Binding]   
    public class ExcelStepDefinition : StepDefinition
    {          
        IExcelDriver excelDriver;
        public ExcelStepDefinition() : base(new Excel2007Repository(), "excel")
        {
            excelDriver = new ExcelDriver(officeAppDriver.ReportFactoryOfficeAddInDriver, factReader);          
        }

也许在这个构造函数中不可能有args,我试过没有它们并且它通过了。

你知道如何解决这个问题吗?

3 个答案:

答案 0 :(得分:6)

- 这是my answer on the SpecFlow Googe Group -

的副本

我认为这里存在误解。

我认为以你提出的方式使用继承是没有意义的。 与传统的xUnit Test框架相比,SpecFlow的工作方式完全不同。 在SpecFlow中,步骤定义是全局的。步骤定义没有 驻留在基类中以便从子类中使用。步 定义与xUnit的测试装置中的方法不相同 框架。

通常所有使用[Binding]属性修饰的类 由SpecFlow扫描以发现步骤定义。 找到的所有步骤定义在运行时都可用 SpecFlow解析并执行功能。 对于SpecFlow来查找匹配的步骤定义,它与之无关 步骤定义的类定义。

然而,当SpecFlow找到匹配的步骤定义时,它需要 能够实例化定义它的类。因此 包含步骤定义的类不得是抽象的。 该实例主要用于在相关步骤之间传递状态 定义(但是还有其他可能通过状态)。

钩子也是如此(之前...... /之后......):它们是全局的, 在运行时,它们定义在哪个类上并不重要。

以上是一般概念。 当我们开始考虑范围内的步骤时,事情会变得复杂一些: 步骤定义可以限定为标记和方案,钩子可以 范围为标签。

实施例:
https://github.com/techtalk/SpecFlow/blob/master/Tests/FeatureTests/ScopedStep/ScopedStepsBindings.cs
https://github.com/techtalk/SpecFlow-Examples/blob/master/ASP.NET-MVC/BookShop/BookShop.AcceptanceTests.Selenium/Support/SeleniumSupport.cs

在这里阅读更多内容:
http://groups.google.com/group/specflow/browse_frm/thread/080c531cb17c86e0/5350665da2544871?#5350665da2544871

阅读有关Cucumber wiki的更多信息。
关于全球步骤:
https://github.com/cucumber/cucumber/wiki/Feature-Coupled-Steps-(Antipattern
步骤组织:
https://github.com/cucumber/cucumber/wiki/Step-Organisation

答案 1 :(得分:2)

我发现这是错误。 我为这个bug提供了针对specflow的补丁,它将在下一个版本中发布。从现在开始,可以继承抽象类。

答案 2 :(得分:0)

我认为你是对的 - 在某种程度上。

SpecFlow支持一种称为上下文注入的东西,这意味着您可以将上下文注入到Binding类中(请参阅此https://github.com/techtalk/SpecFlow/blob/master/Tests/FeatureTests/ContextInjection/FeatureWithADependentContextSteps.cs)。你甚至可以有多个上下文参数(https://github.com/techtalk/SpecFlow/blob/master/Tests/FeatureTests/ContextInjection/FeatureWithMultipleContextsSteps.cs)。

我认为这可能与您的问题有关。问题是所有注入的上下文都需要无参数,即用一个简单的new实例化,如下所示:

var obj = new MyType();

据我所知,你的课程StepDefintion还包含步骤?当这些被调用时,SpecFlow将尝试解决注入的构造函数的依赖关系并失败,如果字符串上没有其他内容,SpecFlow无法知道要设置的内容。

也许您可以将Office-repository的内容分解为StepDefinition-classes可以作为参数的单独类。

或者你可以使用没有步骤的基类(因此没有[Binding] -attribute)。

我没有多检查过这个,但这就是我认为发生的事情 - 喜欢听到你的想法。

相关问题