Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nunit runsTestCase with a TestCaseSource with the first iteration having no parameters? Why?

Hi I am new to Nunit and I am passing a series of objects to a TestCase as a TestCaseSource. For some reason though Nunit seems to run the test first with no parameters passed to it which results in an ignored output:

The test:

private readonly object[] _nunitIsWeird =
{
    new object[] {new List<string>{"one", "two", "three"}, 3},
    new object[] {new List<string>{"one", "two"}, 2}

};

[TestCase, TestCaseSource("_nunitIsWeird")]
public void TheCountsAreCorrect(List<string> entries, int expectedCount)
{
    Assert.AreEqual(expectedCount,Calculations.countThese(entries));
}

TheCountsAreCorrect (3 tests), Failed: One or more child tests had errors TheCountsAreCorrect(), Ignored: No arguments were provided TheCountsAreCorrect(System.Collections.Generic.List1[System.String],2), Success TheCountsAreCorrect(System.Collections.Generic.List1[System.String],3), Success

So the first test is ignored because there are no parameters, but I don't want this test run, ever, it makes no sense and it's mucking up my test output. I tried ignoring it and that sets the test output correctly but it comes back when I run all tests again.

Is there something I am missing, I have looked everywhere.

like image 909
Phil Avatar asked Jun 14 '15 07:06

Phil


1 Answers

TestCase and TestCaseSource do two different things. You just need to remove the TestCase attribute.

[TestCaseSource("_nunitIsWeird")]
public void TheCountsAreCorrect(List<string> entries, int expectedCount)
{
    Assert.AreEqual(expectedCount,Calculations.countThese(entries));
}

The TestCase attribute is for supplying inline data, so NUnit is attempting to supply no parameters to the test, which is failing. Then it's processing the TestCaseSource attribute, and looking up the data that it supplies and trying to pass that to the test as well, which is working correctly.

As a side note, strictly speaking, the docs suggest that you should also mark your TestCaseSource test with a Test attribute like below, however I've never found this necessary:

[Test, TestCaseSource("_nunitIsWeird")]
public void TheCountsAreCorrect(List<string> entries, int expectedCount)
like image 118
forsvarir Avatar answered Nov 05 '22 17:11

forsvarir