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 Maybe A Silly Question But Should I Click Run All?

Discussion in 'Testing & Automation' started by Kalita2127, Feb 17, 2023.

  1. Kalita2127

    Kalita2127

    Joined:
    Dec 6, 2014
    Posts:
    279
    The title says it all. So far It's just okay if I just run selected. But then the problem occurs when I click the run all. It seems like I write it only for individual testing, and not designed to pass the run all tests.
    Can someone give me some resources to properly write unit test in unity? Anything will be appreciated.
    Thankss!
     
  2. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    Are you doing a bunch of setup for your test, and not cleaning up afterwards?

    About Unity Test Framework | Test Framework | 1.3.3 (unity3d.com)
    Installation | NUnit Docs
     
  3. Kalita2127

    Kalita2127

    Joined:
    Dec 6, 2014
    Posts:
    279
    Uhh, yes. What kind of cleaning up should I did? I was just basically doing login, select avatar, and then play tests whatever on that scene.

    Thanks for the source. But I can't find the articles that explains how to write proper unit test in unity. You noticed that I did something wrong right when you asked that question?
     
  4. Kalita2127

    Kalita2127

    Joined:
    Dec 6, 2014
    Posts:
    279
    Like what I mentioned above. I passed any single test, but then it failed when I run all at once. So there must be a proper way to make run all works. Or is it okay to just run the test one by one?
     
  5. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    Any setup that you do for your tests will remain in place, unless you specifically tear it down.
    That's why there are both SetUp- & TearDown-Methods for Tests.
    https://docs.nunit.org/articles/nunit/writing-tests/setup-teardown/index.html
    Most likely the setup of one of your tests is influencing the other tests. Make sure you teardown everything you setup for the test, so you can run clean tests
     
    Kalita2127 likes this.
  6. Kalita2127

    Kalita2127

    Joined:
    Dec 6, 2014
    Posts:
    279
    I've read that. And yes in
    UnitySetup
    I loaded the Initialize scene.
    Code (CSharp):
    1.  
    2.         public static async UniTask SkipToSelectAvatar(bool isGuest = false)
    3.         {
    4.             AsyncOperation operation = SceneManager.LoadSceneAsync("Initializer.unity", LoadSceneMode.Single);
    5.  
    6.             await UniTask.WaitUntil(() => operation.isDone);
    7.  
    8.             AppMain.State = EAppState.LOGIN;
    9.  
    10.             Login(isGuest);
    11.  
    12.             await UniTask.Yield();
    13.  
    14.             //var cts = new CancellationTokenSource();
    15.             //cts.Cancel();
    16.  
    17.             await UniTask.WaitUntil(() => AppMain.State == EAppState.SELECT_AVATAR); //.Timeout(TimeSpan.FromSeconds(20), DelayType.DeltaTime, PlayerLoopTiming.Update, cts);
    18.         }
    But the destination wasn't in Initialize. The tests is in the other scene (let's say scene3). At the end of the test, what should I clean up? Do I need to unload the scene3? There isn't much helpful example I found on the internet. Including this
     
  7. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    Yes. You'll probably also want to LogOut(), since you're doing a login, and reset AppMain.State.
     
  8. Kalita2127

    Kalita2127

    Joined:
    Dec 6, 2014
    Posts:
    279
    Ahh I see. I was assuming that all values changed when unit testing will be automatically resetted after it has finished.