How to use tuple with TestCasesSource in NUnit?

779 views Asked by At

I've been writing some tests in C# using NUnit unitl I encountered an issue with test where I'd like to pass 2 times a 3 Item Tuples as an argument to the test. Seems like a similar problem as described here: How can I use tuples in nunit TestCases?

So I implemented the solution with introducing new static method and passing its name to the TestCaseSource however it doesn't seem to work fully in my case. The only difference is I have tuples consisting of 3 Item tuple instead of 2.

The test passes just partially - it passes the Assert.AreEqual but somehow it doesn't passes the whole test(weird considering the fact there is only one set of arguments?) and shows 1 test has not been run.

Here's the test source code:

    [Test]
    [TestCaseSource(nameof(TestGetTimeLeftData))]
    public void TestGetTimeLeft((int, int, int) alarmTime, (int, int, int) clockTime)
    {
        (int, int, int) expectedTime = (3, 0, 0);
        (int, int, int) result = Helper.GetTimeLeft(alarmTime, clockTime);
        Assert.AreEqual(expectedTime, result);
    }

    private static IEnumerable<(int, int, int)[]> TestGetTimeLeftData
    {
        get
        {
            yield return new[] { (2, 0, 0), (23, 0, 0) };
        }
    }

Test Explorer Window Output

Am I missing something or doing something wrong?

Thanks in advance

1

There are 1 answers

2
Charlie On

Your test takes two arguments, both (int, int, int). The yield statement in your test case source is returning a single argument, which is an array of (int, int, int). that's not the same thing.

Try returning a TestCaseData object instead...

private static IEnumerable<TestCaseData> TestGetTimeLeftData
{
    get
    {
        yield return new TestCaseData((2, 0, 0), (23, 0, 0));
    }
}

This can be done with an object array instead, but TestCaseData exists in order to make it easier to write test case sources.