Search Unity

PlayMode using wrong Scene

Discussion in 'Testing & Automation' started by monteric, Feb 27, 2021.

  1. monteric

    monteric

    Joined:
    Feb 16, 2021
    Posts:
    1
    I'm currently setting up a basic Project with EditMode and PlayMode tests, that I can use for reference in the future. Whilst the EditMode tests have been a breeze, I have yet to get a PlayMode test to work. This is my Project setup:

    upload_2021-2-27_13-9-4.png

    And this is my PlayMode test code:


    Code (CSharp):
    1. namespace Tests
    2. {
    3.     public class PlayerMovementTests
    4.     {
    5.         [UnityTest]
    6.         public IEnumerator PlayerMovementTestsWithEnumeratorPasses()
    7.         {
    8.             var activeScene = SceneManager.GetActiveScene();
    9.             if(activeScene == null)
    10.             {
    11.                 Debug.Log("Couldnt get active scene!");
    12.                 Assert.Fail();
    13.             } else
    14.             {
    15.                 Debug.Log("Found active scene " + activeScene.name);
    16.             }
    17.             var player = GameObject.Find("Player");
    18.             if (player == null)
    19.             {
    20.                 Debug.Log("Couldnt find player!");
    21.                 Assert.Fail();
    22.             }
    23.             else
    24.             {
    25.                 PlayerMovement playerMovement = player.GetComponent(typeof(PlayerMovement)) as PlayerMovement;
    26.                 Debug.Log("checking player position");
    27.                 Debug.Log(player.transform.position.ToString());
    28.                 Debug.Log("moving player left");
    29.                 playerMovement.moveLeft();
    30.                 Debug.Log("Player moved, waiting for movement to stop...");
    31.  
    32.                 yield return new WaitForSeconds(1f);
    33.  
    34.                 Debug.Log("rechecking player position");
    35.                 Debug.Log(player.transform.position.ToString());
    36.                 Assert.IsTrue(player.transform.position.x < 0);
    37.             }
    38.  
    39.         }
    40.     }
    41. }
    What I expect to happen:
    The Scene "SampleScene" gets loaded, as it's the only one, and then my code finds the Entity with the name "Player" and applies the functions.

    What happens: In editor mode, it appears to not load any scene at all, as it looks like this:
    upload_2021-2-27_13-21-57.png

    And the output is this;


    Found active scene InitTestScene637500289106144771
    UnityEngine.Debug:Log (object)
    Tests.PlayerMovementTests/<PlayerMovementTestsWithEnumeratorPasses>d__0:MoveNext () (at Assets/Tests/PlayMode/PlayerMovementTests.cs:23)
    UnityEngine.SetupCoroutine:InvokeMoveNext (System.Collections.IEnumerator,intptr)
    Couldnt find player!
    UnityEngine.Debug:Log (object)
    Tests.PlayerMovementTests/<PlayerMovementTestsWithEnumeratorPasses>d__0:MoveNext () (at Assets/Tests/PlayMode/PlayerMovementTests.cs:28)
    UnityEngine.SetupCoroutine:InvokeMoveNext (System.Collections.IEnumerator,intptr)


    When I run it using "Run All Tests (StandaloneWindows)", it builds the project and runs it in a different window. It appears (?) to load a scene, as seen by the skyimage, but all tests fail:
    upload_2021-2-27_13-23-33.png

    this would be the output:

    <i>Autoconnected Player</i> Found active scene InitTestScene637500289589567897
    <i>Autoconnected Player</i> Couldnt find player!


    As I'm planning on starting a new project, I want to get the testing right, but it seems to not even work for 1 scene, how would I do this with multiple ones?
     
  2. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,659
    Your test should not make assumptions about which scenes are loaded at the start of the test - even if there's only one scene in your project right now. Your tests are responsible for ensuring that the scene that they want to use is loaded (i.e. using SceneManager.LoadScene).

    You can do this in the test itself, or if you are going to have a bunch of tests all running in the same scene, you could do it in a setup method that the framework runs before each of the tests in your class.