Search Unity

Question how to tell if play mode tests are running?

Discussion in 'Testing & Automation' started by andrewfroll7, Apr 26, 2021.

  1. andrewfroll7

    andrewfroll7

    Joined:
    Jun 19, 2020
    Posts:
    10
    When our game boots to execute play mode tests, I'd like to make sure it has done some setup, like loading a default savegame instead of whatever the dev has on their machine right now.

    I could currently do that through SetUp, but that relies on all test fixtures remembering to do that, or all test fixtures remembering to derive from some common base.

    Instead it would be easier if I could just do if (playmodeTestsExecuting) {... in our game bootstrapper. Is that possible? Or is there a better way to do this that I'm missing?
     
  2. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    Are you running the tests on a real device, or in the editor?

    In the UTF documentation (here), it is demonstrated how you could "split" the act of building a player (client) with play mode tests, and then take that build and install it on a target device.

    Based on that example, i believe you could "modify" your build to include a generated scene (that will only be used when creating play mode tests). In that scene you can add test-specific components that will run whatever logic you need (e.g: load a save game or any other logic that is needed by all tests), then proceed to the normal flow of running the tests.

    NOTE: I haven't tried this technique myself, but in theory it might be possible, maybe @superpig can advise if this is a promising direction ?
     
  3. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,659
  4. andrewfroll7

    andrewfroll7

    Joined:
    Jun 19, 2020
    Posts:
    10
    @liortal editor tests, at the moment. even when we're doing player tests, we'd want consistent behaviour between both.

    @superpig as I mentioned SetUp remains an option, but I'm imagining when there's bookkeeping for every single fixture, I'd rather have that automatically handled in game-side code than have to remember to do it every time (even when "do it" means just subclassing and super.SetUp()). "easy to use correctly, hard to use wrong" and all that. If there's nothing obvious to check off, then that's ok.
     
  5. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,659
    SetUpFixture is not the same thing as SetUp.
     
  6. andrewfroll7

    andrewfroll7

    Joined:
    Jun 19, 2020
    Posts:
    10
    oh man, sorry for skimming your post! yes SetUpFixture looks like it might be my guy, thanks!
     
  7. gwelkind

    gwelkind

    Joined:
    Sep 16, 2015
    Posts:
    66
    Thanks superpig!

    I'm just going to set a static flag within this script and use it to differentiate my behavior in code :)
     
  8. jhiginbotham

    jhiginbotham

    Joined:
    Aug 28, 2020
    Posts:
    1
    I had a similar issue, where I needed to perform some ancillary actions if running from the editor versus a standalone build. But needed to skip those actions if the test runner was running playmode tests, as those actions prevented the tests from running successfully and were not needed for the unit tests. Within a #if UNITY_EDITOR block I checked the stack trace for "UnityEngine.TestRunner": Environment.StackTrace.Contains("UnityEngine.TestRunner"). This solved my problem.
     
    kayy and thekid579 like this.