5

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?

Pavel Anikhouski
  • 21,776
  • 12
  • 51
  • 66
GarethOwen
  • 6,075
  • 5
  • 39
  • 56
  • This is not specific to nunit, but a general restriction of Attributes: only constant values of very specific types can be used as attribute parameters (see https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/attributes). – Klaus Gütter May 20 '20 at 10:44

1 Answers1

7

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

Pavel Anikhouski
  • 21,776
  • 12
  • 51
  • 66