Does Nunit run TestCase with TestCaseSource with the first iteration without parameters? What for?

Hi, I am new to Nunit and I am passing a series of objects to TestCase as a TestCaseSource. For some reason, although Nunit seems to run the test first without any parameters passed to it, which leads to ignored output:

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 of the child tests had TheCountsAreCorrect () errors, Ignored: no arguments were provided TheCountsAreCorrect (System.Collections.Generic.List 1[System.String],2), Success TheCountsAreCorrect(System.Collections.Generic.List 1 [System.String], 3), Success

So, the first test is ignored because there are no parameters, but I do not want this test run to ever be, it does not make sense, and it removes the test result. I tried to ignore it, and it sets the test output correctly, but it returns when I run all the tests again.

Is there something that I don’t see, I searched everywhere.

+5
source share
1 answer

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 designed to supply embedded data, so NUnit tries not to pass any parameters to a test that does not work. Then it processes the TestCaseSource attribute and looks for the data that it supplies, and tries to pass this to the test, and also works correctly.

As a note, strictly speaking, the docs suggest that you should also mark your TestCaseSource test with the Test attribute, as shown below, however I never considered it necessary:

 [Test, TestCaseSource("_nunitIsWeird")] public void TheCountsAreCorrect(List<string> entries, int expectedCount) 
+6
source

All Articles