{!See https://docs.google.com/document/d/1takg_GmIBBKKTj-GHZCwzxohpQz7Bhekivkk72kYMtE/edit for reference implementation of OneTrust, dataLayer and GTM} {!OneTrust Cookies Consent} {!OneTrust Cookies Consent end} {!dataLayer initialization push} {!dataLayer initialization push end} {!Google Tag Manager} {!Google Tag Manager end} Actions outside tests | Test Framework | 2.0.1-exp.2
docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Actions outside tests

    In many cases you might find the standard NUnit SetUp and TearDown attributes sufficient for performing pre-test setup and post-test teardown actions. Unity Test Framework extends these attributes with Unity-specific functionality. Our custom attributes UnitySetUp and UnityTearDown can yield commands and skip frames, in the same way as UnityTestAttribute.

    Action execution order

    The actions related to a test run in the following order:

    • Attributes implementing IApplyToContext
    • Any attribute implementing IOuterUnityTestAction has its BeforeTest invoked
    • Tests with UnitySetUpAttribute methods in their test class
    • Attributes implementing IWrapSetUpTearDown
    • Any method with the SetUp attribute
    • Action attributes have their BeforeTest method invoked
    • Attributes implementing IWrapTestMethod
    • The test itself runs
    • Action attributes have their AfterTest method invoked
    • Any method with the TearDown attribute
    • Tests with UnityTearDownAttribute methods in their test class
    • Attributes implementing IOuterUnityTestAction has its AfterTest invoked

    The list of actions is the same for both NUnit Test and UnityTest.

    Execution order

    Action Execution Order

    Note: Some browsers do not support SVG image files. If the image above does not display properly (for example, if you cannot see any text), please try another browser, such as Google Chrome or Mozilla Firefox.

    Unity OuterTestAttribute

    An OuterTestAttribute is a Unity wrapper outside of tests, which allows for any tests with this attribute to run code before and after the tests. This method allows for yielding commands in the same way as UnityTest. The attribute must inherit the NUnitAttribute and implement IOuterUnityTestAction.

    Execution order

    Unity OuterTestAttribute methods are not rerun on domain reload but NUnit Action attributes are:

    OuterUnityTestAction Execution Order

    Note: Some browsers do not support SVG image files. If the image above does not display properly (for example, if you cannot see any text), please try another browser, such as Google Chrome or Mozilla Firefox.

    UnitySetUp and UnityTearDown

    The UnitySetUp and UnityTearDown attributes are identical to the standard NUnit SetUp and TearDown attributes, with the exception that they allow for yielding instructions. The UnitySetUp and UnityTearDown attributes expect a return type of IEnumerator.

    Execution order

    UnitySetUp and UnityTearDown can be used with either the Test or UnityTest test attributes. In both cases the relative execution order of Unity and non-Unity SetUp and TearDown attributes is the same. The only difference is that a UnityTest allows for yielding instructions during the test that can result in a domain reload, in which case the non-Unity SetUp and TearDown methods are re-run before proceeding to the second part of the test.

    SetUp and TearDown Execution Order

    Note: Some browsers do not support SVG image files. If the image above does not display properly (for example, if you cannot see any text), please try another browser, such as Google Chrome or Mozilla Firefox.

    Base and Derived classes

    The term base in the execution order denotes a base class from which a test class inherits. UnitySetUp and UnityTearDown follow the same pattern as NUnit SetUp and TearDown attributes in determining execution order between base classes and their derivatives. SetUp methods are called on base classes first, and then on derived classes. TearDown methods are called on derived classes first, and then on the base class. See the NUnit Documentation for more details.

    Domain Reloads

    In Edit Mode tests it is possible to yield instructions that can result in a domain reload, such as entering or exiting Play Mode (see Custom yield instructions). When a domain reload happens, all non-Unity actions (such as OneTimeSetup and Setup) are rerun before the code that initiated the domain reload continues. Unity actions (such as UnitySetup) are not rerun. If the Unity action is the code that initiated the domain reload, then the rest of the code in the UnitySetup method runs after the domain reload.

    In This Article
    Back to top
    Copyright © 2023 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)