如何教 SpecFlow 向我的测试类添加额外的 NUnit 属性 [英] How to teach SpecFlow to add additional NUnit attributes to my test class
问题描述
SpecFlow 很棒 - 它对我们进行适当的集成测试非常有帮助.
SpecFlow is great - and it helps us very much to do proper integration testing.
我想知道的一件事是,是否有办法告诉 SpecFlow 向它在功能代码隐藏文件中创建的测试类添加额外的 NUnit 属性.
One thing I was wondering is whether there's a way to tell SpecFlow to add additional NUnit attributes to the test class it creates in the feature code-behind file.
现在,我的测试类生成如下:
Right now, my test class gets generated something like this:
[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
{
......
}
SpecFlow 中是否有任何方法可以告诉它添加一个额外的 NUnit 属性来定义测试的类别 - 像这样:
Is there any way in SpecFlow to tell it to add an additional NUnit attribute to define the category of the test - like this:
[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
{
......
}
手动将其添加到生成的代码隐藏中是一种浪费 - 下次 SpecFlow 重新生成该代码隐藏时,我必须记住再次执行此操作(而且很有可能我会忘记).
Adding this manually to the generated code-behind is wasteful - next time SpecFlow re-generates that code-behind, I have to remember doing it again (and chances are, I'll forget).
如果 SpecFlow 中尚不存在该功能 - 如何申请添加此功能?:-)
And if that capability is not yet present in SpecFlow - how to petition for this to be added? :-)
推荐答案
事实上,NUnit.Framework.Category
属性已经被支持,如果你使用 标签(查找标签部分)关于您的功能或场景.所以如果你写
In fact the NUnit.Framework.Category
attribute is already supported if you use tags (look for the tags section) on your feature or scenarios. So if you write
@LongRunningTests
Feature: MySampleFeature
它将生成正确的Category
属性.
it will generate the proper Category
attribute.
但是,如果您想拥有额外的自定义属性,您需要编写一个自定义生成器提供程序来实现 IUnitTestGeneratorProvider
接口并注册到 unitTestProvider 的 generatorProvider
属性在您的配置的 specflow 部分.
However if you want to have additional custom attributes you need to write a custom generator provider with implementing the IUnitTestGeneratorProvider
interface and register with the unitTestProvider's generatorProvider
attribute in your config's specflow section.
您可以在 github 找到内置实现的来源.
You can find the source of the built in implementations at github.
这篇关于如何教 SpecFlow 向我的测试类添加额外的 NUnit 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!