Search code examples
c#nunit

NUnit Test with an array of values


I am trying to use NUnit with the values attribute so that I can specify many different inputs without having 100 separate tests.

However now I am realizing there are times where I want to use the same set of inputs but on very different test like below.

Is there a way that I can specify all the values in one place, like an array and use the array for each values attribute?

I want to make sure that the test runs as 100 individual tests, instead of 1 test that runs 100 values.

I have looked in the Nunit documentation, but I cannot find a way to accomplish this. Any ideas?

Code:

[Test]
public void Test1([Values("Value1", "Value2", "Value3", ... "Value100")] string value)
{
    //Run Test here
}

[Test]
public void Test2([Values("Value1", "Value2", "Value3", ... "Value100")] string value)
{
    //Run Test here
}

[Test]
public void Test3([Values("Value1", "Value2", "Value3", ... "Value100")] string value)
{
    //Run Test here
}

Solution

  • TestCaseSource attribute is suitable here.

    See example:

    private string[] commonCases = { "Val1", "Val2", "Val3" };
    
    [Test]
    [TestCaseSource(nameof(commonCases))]
    public void Test1(string value)
    {
        ....
    }
    
    [Test]
    [TestCaseSource(nameof(commonCases))]
    public void Test12(string value)
    {
        ....
    }