NUnit cannot recognise a TestCase when it contains an array

29,533

Solution 1

Following on from this bug at JetBrains it looks as though the solution here is to use the TestName attribute on your different cases:

[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.

Solution 2

For an array that contains strings, use an object array with the TestCase attributes along with params:

[Test]
[TestCase(new object[] {"foo", "bar", "baz"})]
[TestCase(new object[] {"300", "500", "700"})]    
public void MyClass_SomeOtherMethod(params string[] bunchOfStrings)
{
    // assert something...
}

Solution 3

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.

Solution 4

This works in VS 2017 with NUnit 3.11.0

[TestCase(new long[] {5, 6, 942135153})]
public void DummyTest(long[] values)
{
    Assert.That(values != null);
}
Share:
29,533
user1539401
Author by

user1539401

I'm a programmer who has worked with a few different languages and platforms over the years and learned a lot from all of them. My core skills include C#, JavaScript, Ruby, SQL and web development in general, but I'm also passable with Java, Python, PHP, Perl and some shell scripting dialects. Programming is fun but if I could make training horses my day job, I probably would.

Updated on March 16, 2021

Comments

  • user1539401
    user1539401 about 3 years

    This is quite simple but annoying behaviour I am running into with 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.

    I have a second TestCase that looks like this:

    [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

    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?