Search Unity

Question How to run tests for multiple settings that can't be changed in PlayMode?

Discussion in 'Testing & Automation' started by fherbst, Sep 19, 2019.

  1. fherbst

    fherbst

    Joined:
    Jun 24, 2012
    Posts:
    802
    I have some tests that I need to run against different settings that can't be changed in PlayMode.
    These settings are currently (I might need others):
    - color space
    - XR rendering mode

    Ideally, I'd like to have the ability to tell a specific test to leave playmode, change some setting, re-enter playmode and run the test. Is that possible? Or is there a workaround that doesn't involve "change settings manually, then run the tests"?
     
  2. M_R

    M_R

    Joined:
    Apr 15, 2015
    Posts:
    559
    - write an edit mode UnityTest
    - yield return new EnterPlayMode();
    - run test
    - yield return new ExitPlayMode();

    caveats:
    - enter play mode causes a domain reload (*), non-serialized state (including test local variables) is lost
    - this implies changing mode for each test, can be time-consuming if you have many tests

    (*): 2019.3 introduces the possibility to skip it, but can break stuff (static variables, singletons, [ExecuteInEditMode] scripts, etc...)
     
  3. fherbst

    fherbst

    Joined:
    Jun 24, 2012
    Posts:
    802
    Hm, that sounds it would basically turn the Play Mode tests to Edit Mode tests with hacky playmode then? I see... doesn't sound ideal. But nice workaround, thanks.
     
  4. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    @fherbst are these settings modifiable via some editor API?
    if they are, you can basically create a play mode test that will set the required settings before it executes, then restore them to what they were as the test completes.

    The test itself should not take care of exiting playmode and running the next test in line.
     
  5. fherbst

    fherbst

    Joined:
    Jun 24, 2012
    Posts:
    802
    Yes, there's an API, but at least the Editor warns (in Project settings) that these can't be changed at runtime.
     
  6. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    They will not be changed at runtime. You will have a bunch of tests. As a pre-step for each one of them, you will call this AOI to set whatever settings u need, and then run the test.
     
  7. fherbst

    fherbst

    Joined:
    Jun 24, 2012
    Posts:
    802
    Hm, that sounds good, but I'm not sure I understand - would this only work for running individual tests? I'd need it to work with "Run All".
    (I'll test it out)
     
  8. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    re-thinking this approach, it will enter play mode only once and run all tests, so it does not achieve what you want.
    Are you sure these settings cannot be modified during play mode ?