Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question Tests Failed Using Run All But Worked With Single Run

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

  1. Ali_Akbar_Montazeri

    Ali_Akbar_Montazeri

    Joined:
    Dec 6, 2014
    Posts:
    221
    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?