Thank you for submitting your inquiry.
You can track the status of your inquiry here.
You may want to save your case's ticket: 1103945_6gcb34pn4tqnmgbv


    (Closed) Unit Test SetUp and TearDown methods are called before previous scene is destroyed
     
     
     
    5
    Unit Test SetUp and TearDown methods are called before previous scene is destroyed

    1. What happened

    In a Unit Test class, [SetUp] and [TearDown] can be used to ensure that certain conditions exist before and after each test method runs. However, when two tests are called within a single test run, the SetUp and TearDown methods of the second test are executed BEFORE the first scene is completely destroyed. This means that any behavior executed in OnDestroy methods on objects in the first scene will execute AFTER the SetUp and TearDown methods.

    This causes behavior where my unit tests run when executed one at a time, but fail when run as a batch.

    2. How we can reproduce it using the example you attached

    Open the Test Runner, and go to Play Mode tests, and then run all the tests. The second test will always fail. Here's what happening:

    There's an object in this scene named ObjectToDestroy. It has a class on it called ObjectController. The OnDestroy method of ObjectController sets a global static value to -1. The SetUp and TearDown methods in the unit tests set that global static value to 0, and the tests themselves simply assert that the value is equal to 0.

    When running the tests as one batch, you'll find that the OnDestroy method of the object is being called AFTER the SetUp method from this scene, and AFTER the TearDown method of the previous scene. Here's the log output of the two tests:

    Test SetUp executing
    Test1 Running
    Test Teardown executing
    Test SetUp executing
    Test2 Running
    ObjectController OnDestroy executing
    Test Teardown executing


    Notice that "ObjectController OnDestroy executing" is called very late. This causes me issues in my unit tests, because I can't reliably use SetUp and TearDown to ensure global values are set correctly.

    It seems like Test 2 should simply not start running until after the scene is fully destroyed.

    License type: Free
     
       
      To follow up on this case, send email to bugs@unity3d.com mentioning 'Case 1103945' in the subject.
      Your Cases:
      Case Title Status Date Opened
      1156127 Cinemachine Virtual Camera component breaks Scene Visibility toggle Open 5/20/2019 6:40 PM
      1155068 DynamicGI not recomputing when lighting changes Open 5/16/2019 9:00 PM
      1143875 Incorrect Occlusion Culling visibility when multiple ProBuilder Objects exist in scene Open 4/6/2019 7:40 AM
      1140635 Unity crashes when running complete set of play mode tests Closed 3/26/2019 1:40 PM
      1133241 Test Runner Category filter includes more than just the selected item Open 3/3/2019 9:20 PM
      1122059 Script Execution Order dialog doesn't show complete class names for verbose class names Closed 1/29/2019 5:20 PM
      1121234 Capsule collider generates unexpected force when repeatedly landing directly between two blocks Closed 1/26/2019 10:20 PM
      1121220 Another editor crash Closed 1/26/2019 9:10 PM
      1121219 Another editor crash Closed 1/26/2019 9:00 PM
      1121184 Unity editor crashed Closed 1/26/2019 5:50 PM
      1120178 Rigidbody better able to overcome frictional force when moving perpendicular to direction of travel Closed 1/24/2019 2:40 AM
      1118333 Unity editor crashed while editing a prefab (Resources_Bindings::FindObjectsOfTypeAll Closed 1/18/2019 6:30 PM
      1115574 Occlusion culling causes lights to be turned off Closed 1/9/2019 5:10 PM
      1115557 CinemachineBrain component flags scene as "Dirty" upon exiting play mode Closed 1/9/2019 4:30 PM
      1112094 HDRP Light Dimmer does not influence Realtime GI Closed 12/22/2018 5:20 PM
      1111680 ProBuilder Window tooltips take focus away from file Rename Open 12/21/2018 6:30 AM
      1111543 Assembly Definition files significantly increase the time it takes to perform common editor actions Closed 12/20/2018 6:30 PM
      1110217 Script Execution Order on a base class does not affect subclasses Closed 12/17/2018 3:40 AM
      1109998 2018.3 regression: Ctrl-S no longer saves scene immediately after interacting with Scene view Closed 12/16/2018 5:00 AM
      1109465 Shader Graph Inverse Model transformation on World position results in view-based rendering Open 12/14/2018 7:30 AM
      1109388 HDRP + Lightmap Static + Realtime GI: Light passes through objects Closed 12/13/2018 10:40 PM
      1105873 Clicking through the backface of a mesh does not select the root of a prefab Open 12/2/2018 2:40 AM
      1104279 Every once in a while, the Move tool no longer moves in the editor Closed 11/26/2018 9:50 PM
      1103945 Unit Test SetUp and TearDown methods are called before previous scene is destroyed Closed 11/25/2018 10:20 PM
      1103279 ProBuilder's "Bridge Edges" command adds faces with permanantly flipped UV Closed 11/23/2018 3:20 AM
      1101442 Changing AudioMixer volume results in popping sound Closed 11/16/2018 5:30 PM
      1099358 Unable to click on ProGrids buttons when in ProBuilder Vertex/Edge/Face mode Closed 11/9/2018 8:00 PM
      1099354 Shader Graph Master Preview under HDRP doesn't update in real time Closed 11/9/2018 7:50 PM
      1099327 Presence of Assembly Definition files causes extremely long AssetImport times when creating new c# script Closed 11/9/2018 5:30 PM
      1097591 Adding a decal to a broken prefab, then apply, causes every scene to display the decal, with no way to remove it Closed 11/4/2018 6:30 AM
      1097192 Include a Shader Graph shader in Always Included Shaders results in a player that always crashes Open 11/2/2018 8:20 PM
      1095670 Adding multiple lights causes all shadows to be disabled (HDRP Closed 10/29/2018 6:10 PM
      1092275 Occlusion Culling is applied to Scene View camera at all times Closed 10/19/2018 4:30 AM
      1074129 Unity crashes when Visual Studio debugger attached and hits breakpoint Closed 8/24/2018 8:00 AM
      1033981 ScriptableObjects log errors related to methods they don't even implement Closed 5/5/2018 1:20 AM
      998932 A$ Submission 109633: SmartNS Closed 2/6/2018 10:33 PM
      877620 Entering Play mode after attaching Visual Studio Debugger crashes every time Closed 2/4/2017 12:50 AM