Search code examples
c#nunittestcasetestcasedata

TestCase with list or params


I am trying to write a testcase that takes a string and expects the string split up. I cannot initialize a List in a TestCase, so I tried using TestCaseSource with a params argument, however I get

Wrong number of arguments provided

Is there any way for me to accomplish my end goal?

public IEnumerable<TestCaseData> blah
{
 get
 {
  yield return new TestCaseData("hello World", "h", "e", "l", "l", "o", " ", "W", "o", "r", "l", "d");
 }
}

[TestCaseSource("blah")]
public void testmethod(String orig, params String[] myList)

Solution

  • Even though both your testmethod and TestCaseData constructor take params, TestCaseData interprets params differently: it tries to map them one-to-one to the parameters of the method being tested. In your case, NUnit expects a testmethod with 12 parameters, but your method has only two. This causes the error that you see.

    To fix this problem, you need to change the constructor call as follows:

    yield return new TestCaseData(
        "hello World"
    ,   new[] {"h", "e", "l", "l", "o", " ", "W", "o", "r", "l", "d"}
    );
    

    Now you are passing only two arguments, the second one being an array that must be passed to your params String[] myList.