更改NUnit测试的名称 [英] Change name of NUnit test
问题描述
我希望基于 NUnit
框架的单元测试在Visual Studio测试资源管理器中更具可读性.
I want my unit-tests based on NUnit
framework named a bit more human readable in Visual Studio test explorer.
例如,最好不要使用 Test Case#1,类别:第一,类别:第二
,而不是使用 Test_Case_1
或 TestCase1
(也可以通过 [Category]
属性分配值),并在方法名称中使用空格和字符.
For example instead of having Test_Case_1
or TestCase1
I would better have something like Test Case #1, Category: First, Category: Second
(by assigning values from [Category]
attributes as well), with spaces and characters not allowed in methods names.
我知道它在 xUnit
中是开箱即用的,但是我无法参与其中,因为我使用的自定义项无法使用 xUnit
框架.
I know it's out-of-the-box possible in xUnit
, but I cannot involve it, since I'm using my customizations that I was not able to implement using xUnit
framework.
是否可以用 NUnit
重写单元测试显示名称?到目前为止,我可以看到, TestDetail
的 FullName
字段具有私有设置程序.
Is it possible to rewrite unit test display name with NUnit
? So far I can see, that FullName
field of TestDetail
has private setter.
是否还有其他方法或方法更改 NUnit
测试的显示名称?
Is any other ways or approaches change display name for NUnit
tests?
推荐答案
如果使用参数化测试,则支持此功能,添加 TestCase
时可以指定 TestName
属性.
This is supported if you are using parametrised tests, you can specify the TestName
when adding the TestCase
attribute.
如果您不使用 TestCase ,那么您可能会将它用作不太理想的解决方案,以实现您要完成的任务.因此,您可以这样声明测试:
If you aren't using TestCase, then you could use it as a less than ideal work around to achieve what you're trying to do. So you would declare your test like this:
[TestCase(null,TestName="Test Case #1, Category: First, Category: Second")]
public void TestCase(object ignored)
这不是理想的选择,因为它不是编程的,因此您必须手动键入测试名称,而不是从测试方法的属性中生成它.您还必须将一个参数传递给方法,即 ignored
和 null
的含义.当然,您可以开始使用参数化测试,在这种情况下,您需要将实际值传递给测试.
This isn't ideal because it's not programmatic so you have to manually type the test name, rather than generating it from the attributes on the test method. You also have to pass a parameter to the method, which is what the ignored
and null
is about. Of course, you could start using parametrised tests in which case you'd be passing in an actual value to your tests.
[TestCase(5,TestName="Test Case #1, Category: First, Category: Second")]
public void TestCase(int someInput) {
Assert.AreEqual(5, someInput);
}
这篇关于更改NUnit测试的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!