1

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 Answers1

1

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.

Charlie
  • 12,928
  • 1
  • 27
  • 31
  • Thanks, that seems to look more neat than the solution with an array of tuples. However I still have just **6/7 tests passed**. Right now I'm confused because I opened this project in **Rider** and there are only 6 test instead of 7. I know my quirk but I can't grasp why there is an additional test in VS. – MichaelPeter May 07 '21 at 19:32
  • I don't understand. Your example shows 2 tests. – Charlie May 08 '21 at 22:27