Search Unity

Resolved Calling IEnumerator methods in a UnityTest in PlayTest fixtures

Discussion in 'Testing & Automation' started by Ollie_Palmer, Apr 15, 2021.

  1. Ollie_Palmer

    Ollie_Palmer

    Joined:
    Jun 17, 2019
    Posts:
    10
    Hello,

    I’m currently attempting to make modular helper/auxillary methods for my PlayTests (to avoid duplicate code).

    Code (CSharp):
    1. public IEnumerator navigateToGameScene()
    2. {
    3.     gamepad1 = InputSystem.AddDevice<Gamepad>();
    4.     yield return new WaitForSeconds(0.3f);
    5.     Press(gamepad1.dpad.up);
    6.     Release(gamepad1.dpad.up);
    7.     yield return new WaitForSeconds(0.3f);
    8.     Press(gamepad1.dpad.up);
    9.     Release(gamepad1.dpad.up);
    10.     yield return new WaitForSeconds(0.3f);
    11.     Press(gamepad1.buttonSouth);
    12.     Release(gamepad1.buttonSouth);
    13. }
    14.  
    However, some of these may require waiting for frames or some amount of seconds.
    How do I call these helper/auxillary methods that aren’t tagged with [UnityTest]?

    Using StartCoroutine(navigateToGameScene) doesn’t work.
    Ideally this is what I’d want to do:
    Code (CSharp):
    1.  
    2. [UnityTest]
    3. public IEnumerator somePlayTest()
    4. {
    5.         //Do some test specific setup
    6.         //Call auxillary method e.g.
    7.         yield new StartCouroutine(navigateToGameScene);
    8.         //Some more code
    9.         //Assertion
    10. }
    11.  
    Thanks in advance.
     
    dan_ginovker likes this.
  2. sbergen

    sbergen

    Joined:
    Jan 12, 2015
    Posts:
    53
    You should be able to do just
    Code (CSharp):
    1. yield return navigateToGameScene();
    However, you'll probably run into this Unity bug sooner or later.

    If you are interested in reusability of test methods like this, and working around that bug, you might want to take a look at Responsible ;)
     
    Ollie_Palmer likes this.
  3. Ollie_Palmer

    Ollie_Palmer

    Joined:
    Jun 17, 2019
    Posts:
    10
    That worked - thanks a lot for this and the heads up on that bug. :)

    Responsible looks neat, thanks for the recommendation!