|
| 1 | +--- |
| 2 | +title: MSTest test lifecycle |
| 3 | +description: Learn about the creation and lifecycle of test classes and test methods in MSTest. |
| 4 | +author: marcelwgn |
| 5 | +ms.author: marcelwagner |
| 6 | +ms.date: 06/03/2025 |
| 7 | +--- |
| 8 | + |
| 9 | +# MSTest lifecycle |
| 10 | + |
| 11 | +MSTest provides a well-defined lifecycle for test classes and test methods, allowing for setup and teardown operations to be performed at various stages of the test execution process. The lifecycle can be grouped into the following three stages: |
| 12 | + |
| 13 | +- Assembly-level lifecycle |
| 14 | +- Class-level lifecycle |
| 15 | +- Test-level lifecycle |
| 16 | + |
| 17 | +The execution of the lifecycle events occurs from the highest level (assembly) to the lowest level (test method). The order of execution is as follows: |
| 18 | + |
| 19 | +1. Assembly Initialization |
| 20 | +1. Class Initialization (for every test class) |
| 21 | +1. Test initialization (for every test method) |
| 22 | +1. Test Execution |
| 23 | +1. Test Cleanup (for every test method) |
| 24 | +1. Class Cleanup (for every test class) |
| 25 | +1. Assembly Cleanup |
| 26 | + |
| 27 | +## Assembly-level Lifecycle |
| 28 | + |
| 29 | +The assembly lifecycle describes the lifecycle of the entire assembly, which includes all test classes and methods. |
| 30 | +To manage the assembly lifecycle, MSTest provides the [AssemblyInitialize](xref:Microsoft.VisualStudio.TestTools.UnitTesting.AssemblyInitializeAttribute) and [AssemblyCleanup](xref:Microsoft.VisualStudio.TestTools.UnitTesting.AssemblyCleanupAttribute) attributes. |
| 31 | +To learn more about these attributes, refer to the [AssemblyInitialize and AssemblyCleanup](./unit-testing-mstest-writing-tests-attributes.md#assembly-level) documentation. |
| 32 | + |
| 33 | +## Class-level Lifecycle |
| 34 | + |
| 35 | +The test class lifecycle refers to the lifecycle of individual test classes within the assembly and can be implemented using the [ClassInitialize](xref:Microsoft.VisualStudio.TestTools.UnitTesting.ClassInitializeAttribute) and [ClassCleanup](xref:Microsoft.VisualStudio.TestTools.UnitTesting.ClassCleanupAttribute) attributes. |
| 36 | +These attributes allow you to define setup and teardown methods that are executed before and after all tests in a class, respectively. |
| 37 | +For more information on these attributes, refer to the [ClassInitialize and ClassCleanup](./unit-testing-mstest-writing-tests-attributes.md#class-level) documentation. |
| 38 | +The class level lifecycle is only run once per class, regardless of the number of tests in a class. |
| 39 | + |
| 40 | +## Test-level Lifecycle |
| 41 | + |
| 42 | +The test level lifecycle is executed for every test method. For parameterized tests, each set of parameters is treated as a separate test method, and the lifecycle is executed for each set of parameters. |
| 43 | +Test level lifecycle can be grouped into setup, execution and cleanup with setup and cleanup supporting multiple ways to be implemented. |
| 44 | + |
| 45 | +#### Setup |
| 46 | + |
| 47 | +The setup phase of the test level lifecycle is responsible for preparing the test environment before the execution of each test method. This can be achieved using the `TestInitialize` attribute or by implementing a constructor in the test class. In the case of inheritance, execution of `TestInitialize` methods follows the order from the base class to the derived class. If a test class implements a constructor, it is executed before the `TestInitialize` method. To learn more about the `TestInitialize` attribute, refer to the [test level attribute](./unit-testing-mstest-writing-tests-attributes.md#test-level) documentation. |
| 48 | + |
| 49 | +> [!NOTE] |
| 50 | +> Unlike the class constructor, `TestInitialize` methods can be async and also support attribute usage such as the `TimeoutAttribute`. |
| 51 | +
|
| 52 | +#### Execution |
| 53 | + |
| 54 | +The execution phase is the phase where the actual test method is executed. If a test method returns a Task or ValueTask, the test method will be awaited. |
| 55 | + |
| 56 | +> [!WARNING] |
| 57 | +> In the case of asynchronous test methods, no [SynchronizationContext](xref:System.Threading.SynchronizationContext) is provided. This does not apply to `UITestMethod` tests for UWP and WinUI as they run on the UI thread which has a [SynchronizationContext](xref:System.Threading.SynchronizationContext). |
| 58 | +
|
| 59 | +#### Cleanup |
| 60 | + |
| 61 | +The cleanup phase of the test level lifecycle is responsible for cleaning up the test environment after the execution of each test method. |
| 62 | +This can be achieved using the `TestCleanup` attribute or by implementing the `IDisposable`/`IAsyncDisposable` interface in the test class. |
| 63 | +If a test class implements `IDisposable` or `IAsyncDisposable`, its `Dispose`/`DisposeAsync` method is executed after the `TestCleanup` method. |
| 64 | +In case of inheritance, execution of `TestCleanup` methods follows the order from the derived class to the base class. |
| 65 | +To learn more about the `TestInitialize` attribute, refer to the [test level attribute](./unit-testing-mstest-writing-tests-attributes.md#test-level) documentation. |
| 66 | + |
| 67 | +#### Order |
| 68 | + |
| 69 | +The complete order of the test level lifecycle is as follows: |
| 70 | + |
| 71 | +1. Create instance of test class |
| 72 | +1. Set `TestContext` property if present |
| 73 | +1. Run TestInitialize (if implemented) |
| 74 | +1. Test method execution |
| 75 | +1. Update `TestContext` with test results (such as `Outcome` property) |
| 76 | +1. Run TestCleanup if implemented |
| 77 | +1. Run DisposeAsync if implemented |
| 78 | +1. Run Dispose if implemented |
0 commit comments