-
Notifications
You must be signed in to change notification settings - Fork 6k
Introduce MSTest lifecycle documentation #46540
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d3b8cf7
Add initial test lifecycle page
marcelwgn fc1f786
Update documents
marcelwgn 7ef45df
Add lifecycle docs to navigation
marcelwgn 5bfcfd4
Apply suggestions from code review
marcelwgn 44cbd8f
Minor updates
marcelwgn 30b63da
Update xref
marcelwgn a9720e6
Remove implication of sync context
marcelwgn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
docs/core/testing/unit-testing-mstest-writing-tests-lifecycle.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
--- | ||
title: MSTest test lifecycle | ||
description: Learn about the creation and lifecycle of test classes and test methods in MSTest. | ||
author: marcelwgn | ||
ms.author: marcelwagner | ||
ms.date: 06/03/2025 | ||
--- | ||
|
||
# MSTest lifecycle | ||
|
||
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: | ||
|
||
- Assembly-level lifecycle | ||
- Class-level lifecycle | ||
- Test-level lifecycle | ||
|
||
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: | ||
|
||
1. Assembly Initialization | ||
1. Class Initialization (for every test class) | ||
1. Test initialization (for every test method) | ||
1. Test Execution | ||
1. Test Cleanup (for every test method) | ||
1. Class Cleanup (for every test class) | ||
1. Assembly Cleanup | ||
|
||
## Assembly-level Lifecycle | ||
|
||
The assembly lifecycle describes the lifecycle of the entire assembly, which includes all test classes and methods. | ||
To manage the assembly lifecycle, MSTest provides the [AssemblyInitialize](xref:Microsoft.VisualStudio.TestTools.UnitTesting.AssemblyInitializeAttribute) and [AssemblyCleanup](xref:Microsoft.VisualStudio.TestTools.UnitTesting.AssemblyCleanupAttribute) attributes. | ||
To learn more about these attributes, refer to the [AssemblyInitialize and AssemblyCleanup](./unit-testing-mstest-writing-tests-attributes.md#assembly-level) documentation. | ||
|
||
## Class-level Lifecycle | ||
|
||
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. | ||
These attributes allow you to define setup and teardown methods that are executed before and after all tests in a class, respectively. | ||
For more information on these attributes, refer to the [ClassInitialize and ClassCleanup](./unit-testing-mstest-writing-tests-attributes.md#class-level) documentation. | ||
The class level lifecycle is only run once per class, regardless of the number of tests in a class. | ||
|
||
## Test-level Lifecycle | ||
|
||
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. | ||
Test level lifecycle can be grouped into setup, execution and cleanup with setup and cleanup supporting multiple ways to be implemented. | ||
|
||
#### Setup | ||
|
||
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. | ||
|
||
> [!NOTE] | ||
> Unlike the class constructor, `TestInitialize` methods can be async and also support attribute usage such as the `TimeoutAttribute`. | ||
|
||
#### Execution | ||
|
||
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. | ||
|
||
> [!WARNING] | ||
> In the case of asynchronous test methods, no [SynchronizationContext](xref:System.Threading.SynchronizationContext) is provided. This means any async code is being run on a ThreadPool thread, and not on the main thread. 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). | ||
marcelwgn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#### Cleanup | ||
|
||
The cleanup phase of the test level lifecycle is responsible for cleaning up the test environment after the execution of each test method. | ||
This can be achieved using the `TestCleanup` attribute or by implementing the `IDisposable`/`IAsyncDisposable` interface in the test class. | ||
If a test class implements `IDisposable` or `IAsyncDisposable`, its `Dispose`/`DisposeAsync` method is executed after the `TestCleanup` method. | ||
In case of inheritance, execution of `TestCleanup` methods follows the order from the derived class to the base class. | ||
To learn more about the `TestInitialize` attribute, refer to the [test level attribute](./unit-testing-mstest-writing-tests-attributes.md#test-level) documentation. | ||
|
||
#### Order | ||
|
||
The complete order of the test level lifecycle is as follows: | ||
|
||
1. Create instance of test class | ||
1. Set `TestContext` property if present | ||
1. Run TestInitialize (if implemented) | ||
1. Test method execution | ||
1. Update `TestContext` with test results (such as `Outcome` property) | ||
1. Run TestCleanup if implemented | ||
1. Run DisposeAsync if implemented | ||
1. Run Dispose if implemented |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.