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 Unity
Action execution order
The actions related to a test run in the following order:
- Attributes implementing IApplyToContext
- Any attribute implementing IOuter
Unity has itsTest Action BeforeTest
invoked - Tests with Unity
Set methods in their test classUp Attribute - 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 Unity
Tear methods in their test classDown Attribute - Attributes implementing IOuter
Unity has itsTest Action AfterTest
invoked
The list of actions is the same for both NUnit Test
and UnityTest
.
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 IOuter
Execution order
Unity OuterTestAttribute methods are not rerun on domain reload but NUnit Action attributes are:
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.
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.