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

Question Tests Failed Using Run All But Worked With Single Run

Discussion in 'Testing & Automation' started by Kalita2127, Mar 28, 2023.

  1. Kalita2127

    Kalita2127

    Joined:
    Dec 6, 2014
    Posts:
    279
    So let me explain the scenario first in my case. I have 3 scenes, it's Initialize (for initiation), Login (home page contains several login methods), and Select Avatar (for avatar customization). First I'm using UnitySetup to basically setup and skipping the Login scene to SelectAvatar scene (because this is where the tests happen) with this:

    Code (CSharp):
    1.  
    2.         [UnitySetUp]
    3.         public IEnumerator LoadScene()
    4.         {
    5.             Debug.Log("Load scene called"); // Called in each test
    6.  
    7.             // Initialize, load Login, then wait till it loads SelectAvatar scene
    8.             yield return UtilTestMethods.SkipToSelectAvatar(Api.ELoginProvider.GUEST).ToCoroutine();
    9.             yield return new WaitUntil(() => AppMain.State == EAppState.SELECT_AVATAR);
    10.             yield return new WaitUntil(() => SceneManager.GetActiveScene().name == "SelectAvatar");
    11.             yield return new WaitForSeconds(UtilTestMethods.NORMAL_TEST_DURATION);
    12.  
    13.             Debug.Log("Load scene finished"); // Also called
    14.         }
    15.  
    and then the test methods:
    Code (CSharp):
    1.  
    2.         // First test is green
    3.         [Test]
    4.         public void _0_CanGetCachedUsername()
    5.         {
    6.             Assert.IsNotNull(AppMain.User.Nickname);
    7.             Assert.IsNotEmpty(AppMain.User.Nickname);
    8.         }
    9.  
    10.         // Second test red because it supposed to load SelectAvatar scene but ended up landed in Login scene thus the variable avaMenuVC is null
    11.         [Test]
    12.         public void _01_CanFilterUsername()
    13.         {
    14.             var avaMenuVC = GameObject.FindObjectOfType<AvatarMenuViewController>();
    15.             Assert.IsNotNull(avaMenuVC);
    16.  
    17.             var avaMenuView = avaMenuVC.GetPrivateField<AvatarMenuView>("_view");
    18.             Assert.IsNotNull(avaMenuView);
    19.  
    20.             var warningText = avaMenuView.GetPrivateField<TextMeshProUGUI>("_warningText"); ;
    21.             Assert.IsNotNull(warningText);
    22.  
    23.             var usernameIF = avaMenuView.GetPrivateField<TMP_InputField>("_usernameInputField");
    24.             Assert.IsNotNull(usernameIF);
    25.  
    26.             usernameIF.ActivateInputField();
    27.  
    28.             usernameIF.text = "";
    29.  
    30.             usernameIF.ProcessEvent(Event.KeyboardEvent("a"));
    31.             usernameIF.ForceLabelUpdate();
    32.  
    33.             Assert.True(warningText.gameObject.activeInHierarchy);
    34.             Assert.AreEqual(warningText.text, "Minimum 2 characters");
    35.  
    36.             usernameIF.text = "";
    37.  
    38.             usernameIF.ProcessEvent(Event.KeyboardEvent("space"));
    39.             usernameIF.ForceLabelUpdate();
    40.  
    41.             Assert.True(warningText.gameObject.activeInHierarchy);
    42.             Assert.AreEqual(warningText.text, "Minimum 2 characters");
    43.  
    44.             usernameIF.text = "";
    45.  
    46.             usernameIF.ProcessEvent(Event.KeyboardEvent("space"));
    47.             usernameIF.ProcessEvent(Event.KeyboardEvent("space"));
    48.             usernameIF.ForceLabelUpdate();
    49.  
    50.             Assert.True(warningText.gameObject.activeInHierarchy);
    51.             Assert.AreEqual(warningText.text, "Invalid username");
    52.         }
    53.         // And another tests
    54.  
    and this is inside my UnityTearDown:
    Code (CSharp):
    1.  
    2.         [UnityTearDown]
    3.         public IEnumerator UnloadScene()
    4.         {
    5.             if(SceneManager.GetActiveScene().name == "Sky_City_Hub")
    6.                 yield return SceneManager.UnloadSceneAsync("Sky_City_Hub");
    7.  
    8.             yield return SceneManager.UnloadSceneAsync("SelectAvatar");
    9.         }
    10.  
    That and I also got error that said
    TearDown : System.ArgumentException : Scene to unload is invalid
    . Well that was expected because it landed on Login scene. Any ideas why it behave like this?