TestCaseSource compatible with UnityTest

Hello,

Is TestCaseSource compatible with UnityTest?

    public class SourceTests
    {
        [UnityTest]
        public IEnumerator TestUnity()
        {
            Assert.Pass(); //Pass
            yield return null;
        }

        [UnityTest, TestCaseSource(nameof(CaseSource))]
        public IEnumerator SourceTestUnity(int a, float b, bool c)
        {
            Assert.Pass(); //Fail
            yield return null;
        }

        [Test, TestCaseSource(nameof(CaseSource))]
        public void SourceTest(int a, float b, bool c)
        {
            Assert.Pass(); //Pass
        }

        static object[] CaseSource =
        {
            new object[] { 12, 3.0f, false },
            new object[] { 12, 3.0f, false },
            new object[] { 10, 3.0f, false },
        };
    }

I can use TestCaseSource with regular nunit tests but when I use it with UnityTest I have this error “Method has non-void return value, but no result is expected”

Can I use TestCaseSource somehow with UnityTests?

Thank you

1 Like

Instead of

new object[] { 12, 3.0f, false }

you’ll need to use

TestCaseData(12, 3.0f, false).Returns(null);

It’s sort of a hack, but gets the job done.

5 Likes

It works perfect. Thanks!

1 Like

To make this clearer, this is the whole setup:

private static IEnumerable TestSource
{
    get
    {
        yield return new TestCaseData(3, 3).Returns(null);
        yield return new TestCaseData(4, 4).Returns(null);
        yield return new TestCaseData(2, 2).Returns(null);
    }
}

[UnityTest] [TestCaseSource(nameof(TestSource))]
public IEnumerator TestWithTestCaseData(int width, int length)
{
    // ...
}