Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

TestCaseSource compatible with UnityTest

Discussion in 'Testing & Automation' started by Maeslezo, Nov 17, 2020.

  1. Maeslezo

    Maeslezo

    Joined:
    Jun 16, 2015
    Posts:
    351
    Hello,

    Is TestCaseSource compatible with UnityTest?

    Code (CSharp):
    1.     public class SourceTests
    2.     {
    3.         [UnityTest]
    4.         public IEnumerator TestUnity()
    5.         {
    6.             Assert.Pass(); //Pass
    7.             yield return null;
    8.         }
    9.  
    10.         [UnityTest, TestCaseSource(nameof(CaseSource))]
    11.         public IEnumerator SourceTestUnity(int a, float b, bool c)
    12.         {
    13.             Assert.Pass(); //Fail
    14.             yield return null;
    15.         }
    16.  
    17.         [Test, TestCaseSource(nameof(CaseSource))]
    18.         public void SourceTest(int a, float b, bool c)
    19.         {
    20.             Assert.Pass(); //Pass
    21.         }
    22.  
    23.         static object[] CaseSource =
    24.         {
    25.             new object[] { 12, 3.0f, false },
    26.             new object[] { 12, 3.0f, false },
    27.             new object[] { 10, 3.0f, false },
    28.         };
    29.     }

    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
     
    JesseSTG likes this.
  2. sbergen

    sbergen

    Joined:
    Jan 12, 2015
    Posts:
    53
    Instead of
    Code (CSharp):
    1. new object[] { 12, 3.0f, false }
    you'll need to use
    Code (CSharp):
    1. TestCaseData(12, 3.0f, false).Returns(null);
    It's sort of a hack, but gets the job done.
     
    murphyne, CodeSmile, Gearmoon and 2 others like this.
  3. Maeslezo

    Maeslezo

    Joined:
    Jun 16, 2015
    Posts:
    351
    It works perfect. Thanks!
     
    JesseSTG likes this.
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,943
    To make this clearer, this is the whole setup:

    Code (CSharp):
    1. private static IEnumerable TestSource
    2. {
    3.     get
    4.     {
    5.         yield return new TestCaseData(3, 3).Returns(null);
    6.         yield return new TestCaseData(4, 4).Returns(null);
    7.         yield return new TestCaseData(2, 2).Returns(null);
    8.     }
    9. }
    10.  
    11. [UnityTest] [TestCaseSource(nameof(TestSource))]
    12. public IEnumerator TestWithTestCaseData(int width, int length)
    13. {
    14.     // ...
    15. }