Search code examples
c#nunitvaluetuple

How can I use tuples in nunit TestCases?


I am trying to specify nUnit testCases using tuples, but I get a compiler error in VisualStudio.

This simple example demonstrates what I am trying to do:

    [TestCase((1, 2), (3, 5))]
    public void TestRangeOverlaps((int start, int end) firstRange, (int start, int end) secondRange)
    {

    }

If this is possible, what am I missing?


Solution

  • You can use TestCaseSource attribute and specify IEnumerable<(int, int)[]> for value source.

    Every IEnumerable item represents a set of parameters passed to test method. In your case it's a two tuples, so you should return an array of them every time to pass to TestRangeOverlaps

    [Test]
    [TestCaseSource(nameof(Tuples))]
    public void TestRangeOverlaps((int start, int end) firstRange, (int start, int end) secondRange)
    {
    }
    
    public static IEnumerable<(int, int)[]> Tuples
    {
        get
        {
            yield return new[] { (1, 2), (3, 5) };
        }
    }
    

    TestCase attribute supports only constant values