C# NUnit 无法识别包含数组的 TestCase
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17925916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NUnit cannot recognise a TestCase when it contains an array
提问by glenatron
This is quite simple but annoying behaviour I am running into with NUnit:
这是我在使用 NUnit 时遇到的非常简单但令人讨厌的行为:
I have some tests like this:
我有一些这样的测试:
[Test]
[TestCase( 1, 2, "hello" )]
[TestCase( 3, 5, "goodbye" )]
public void MyClass_MyMethod( int a, int b, string c )
{
Assert.IsTrue( a < b );
}
This works fine and in the ReSharper NUnit pane I can see each TestCase getting its own response in the result.
这工作正常,在 ReSharper NUnit 窗格中,我可以看到每个 TestCase 在结果中都有自己的响应。
I have a second TestCase that looks like this:
我有第二个 TestCase,看起来像这样:
[Test]
[TestCase( 1, 2, new long[] { 100, 200 })]
[TestCase( 5, 3, new long[] { 300, 500 })]
public void MyClass_MyOtherMethod( long a, long b, long[] bunchOfNumbers )
{
Assert.IsTrue( a < b );
}
When I run it I see this:
当我运行它时,我看到:
One or more child tests had errors Exception doesn't have a stacktrace
public void MyClass_MyOtherMethod(5,3,System.Int64[]) failed
一个或多个子测试有错误异常没有堆栈跟踪
public void MyClass_MyOtherMethod(5,3,System.Int64[]) 失败
The difference being that with my other tests it draws out each TestCase as a separate checkbox on the test list, whereas this one does not get shown and I have no detail until I run it in a debugger as to what went wrong and where. I am a little concerned about how this test will behave on the build machine. Does anyone have any idea what is going on and why?
不同之处在于,与我的其他测试相比,它将每个 TestCase 作为测试列表中的一个单独复选框绘制出来,而这个测试不会显示出来,并且在我在调试器中运行它之前,我没有任何细节关于哪里出错了。我有点担心这个测试在构建机器上的表现。有谁知道发生了什么以及为什么?
采纳答案by glenatron
Following on from this bug at JetBrainsit looks as though the solution here is to use the TestName
attribute on your different cases:
继JetBrains 的这个错误之后,看起来这里的解决方案是TestName
在您的不同情况下使用该属性:
[Test]
[TestCase( 1, 2, new long[] { 100, 200 }, TestName="Test 1" )]
[TestCase( 5, 3, new long[] { 300, 500 }, TestName="Test 2" )]
public void MyClass_MyOtherMethod( long a, long b, long[] bunchOfNumbers )
{
Assert.IsTrue( a < b );
}
Everything now shows correctly in ReSharper if one of my tests fails.
如果我的一项测试失败,现在一切都在 ReSharper 中正确显示。
回答by Johan Larsson
An alternative is to use a string for the array:
另一种方法是对数组使用字符串:
[TestCase( 1, 2, "100, 200")]
[TestCase( 5, 3, "300, 500")]
public void MyClass_MyOtherMethod(long a, long b, string bunchOfNumbersString)
{
var bunchOfNumbers= bunchOfNumbersString.Split(',')
.Select(long.Parse)
.ToArray();
...
}
The upside with this approach is that it will render nicly in the testrunner.
这种方法的好处是它可以在测试运行器中很好地呈现。
Side note: The [Test]
is not needed when using [TestCase]
or at least I don't see that it solves a problem.
旁注:[Test]
使用时不需要,[TestCase]
或者至少我没有看到它解决了问题。
回答by Ray
For an array that contains strings, use an object
array with the TestCase
attributes along with params
:
对于包含字符串的object
数组,请使用具有以下TestCase
属性的数组params
:
[Test]
[TestCase(new object[] {"foo", "bar", "baz"})]
[TestCase(new object[] {"300", "500", "700"})]
public void MyClass_SomeOtherMethod(params string[] bunchOfStrings)
{
// assert something...
}
回答by Matt
This works in VS 2017 with NUnit 3.11.0
这适用于使用 NUnit 3.11.0 的 VS 2017
[TestCase(new long[] {5, 6, 942135153})]
public void DummyTest(long[] values)
{
Assert.That(values != null);
}