如何教SpecFlow向我的测试类添加额外的NUnit属性

mar*_*c_s 7 nunit class-attributes specflow

SpecFlow非常棒 - 它非常有助于我们进行适当的集成测试.

我想知道的一件事是,是否有办法告诉SpecFlow将其他NUnit属性添加到它在功能代码隐藏文件中创建的测试类中.

现在,我的测试类生成如下:

[System.CodeDom.Compiler.GeneratedCodeAttribute("TechTalk.SpecFlow", "1.8.1.0")]
[System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[NUnit.Framework.TestFixtureAttribute()]
[NUnit.Framework.DescriptionAttribute("Some action description here")]
public partial class MySampleFeature
{  
   ......
}
Run Code Online (Sandbox Code Playgroud)

在SpecFlow中是否有任何方法告诉它添加额外的NUnit属性来定义测试的类别 - 如下所示:

[System.CodeDom.Compiler.GeneratedCodeAttribute("TechTalk.SpecFlow", "1.8.1.0")]
[System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[NUnit.Framework.TestFixtureAttribute()]
[NUnit.Framework.DescriptionAttribute("Some action description here")]
[NUnit.Framework.Category("LongRunningTests")]   <== add this "Category" attribute
public partial class MySampleFeature
{  
   ......
}
Run Code Online (Sandbox Code Playgroud)

手动将其添加到生成的代码隐藏是浪费 - 下次SpecFlow重新生成代码隐藏时,我必须记住再次执行它(很可能,我会忘记).

如果SpecFlow中还没有这种能力 - 如何申请加入?:-)

nem*_*esv 10

实际上,NUnit.Framework.Category如果您在功能或方案上使用标记(查找标记部分),则已支持该属性.所以,如果你写

@LongRunningTests
Feature: MySampleFeature
Run Code Online (Sandbox Code Playgroud)

它会生成适当的Category属性.

但是,如果您想拥有其他自定义属性,则需要编写自定义生成器提供程序,并实现该IUnitTestGeneratorProvider接口并在config的specflow部分中注册unitTestProvidergeneratorProvider属性.

您可以在github上找到内置实现的源代码.

  • 这在SpecFlow 1.9中看起来不可能,因为无法访问IUnitTestGeneratorProvider接口.该文档指出,在发布时,提供程序将在v.2.0中替换为插件.例如,我想实现一个自定义的xUnitGeneratorProvider,以便将标签推送到xUnit.NET Traits. (2认同)