Specflow 步骤定义继承

标签 specflow

我有 ExcelStepDefinition 类,其中是我进行 Excel 测试的步骤。我也有 WordStepDefinition 类。由于这两个类有大量相同的步骤,因此我创建了一些 StepDefinition 类,它将作为这两个类的基类。

在这个基类中,我需要在构造函数中有一些参数,这取决于实例化的类(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);          
        }

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

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

最佳答案

-- 这是来自 my answer on the SpecFlow Googe Group 的副本--

我认为这里有一个误解。

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

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

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

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

以上是一般概念。 当我们开始考虑范围步骤时,事情变得有点复杂: 步骤定义的范围可以是标签和场景, Hook 可以是 范围仅限于标签。

示例:
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

关于Specflow 步骤定义继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5326210/

相关文章:

c# - 使用 Specflow.Assist .Dynamic 访问具有特殊字符的属性

nunit - 在没有 Visual Studio 的情况下运行 SpecFlow 测试

asp.net-web-api - 测试项目扫描我的 Controller 两次

c# - 已安装SpecFlow.Allure,但在服务器上不起作用

unit-testing - Specflow - 有没有办法管理背景,使其仅针对功能中的某些场景运行?

testing - 如果你有一个功能的场景/测试,你是否必须测试相反的?

testing - SpecFlow - 多个 BeforeScenario 方法的排序

c# - SpecFlow 可重用步骤定义

c# - 如何从 SpecFlow 获取步骤计时信息

nunit - 执行 specflow 功能的顺序