Skip to content

Commit 81f3f10

Browse files
committed
Document MSTest 3.8 TestContext support for ClassCleanup/AssemblyCleanup
1 parent ea2df31 commit 81f3f10

File tree

3 files changed

+7
-23
lines changed

3 files changed

+7
-23
lines changed

docs/core/testing/mstest-analyzers/mstest0011.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Methods marked with `[ClassCleanup]` should follow the following layout to be va
3838
- it should not be `async void`
3939
- it should not be a special method (finalizer, operator...).
4040
- it should not be generic
41-
- it should not take any parameter
41+
- it should not take any parameter, or starting with MSTest 3.8, it can have a single `TestContext` parameter
4242
- return type should be `void`, `Task` or `ValueTask`
4343
- `InheritanceBehavior.BeforeEachDerivedClass` attribute parameter should be specified if the class is `abstract`.
4444
- `InheritanceBehavior.BeforeEachDerivedClass` attribute parameter should not be specified if the class is `sealed`.

docs/core/testing/mstest-analyzers/mstest0013.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Methods marked with `[AssemblyCleanup]` should follow the following layout to be
3838
- it should not be `async void`
3939
- it should not be a special method (finalizer, operator...).
4040
- it should not be generic
41-
- it should not take any parameter
41+
- it should not take any parameter, or starting with MSTest 3.8, it can have a single `TestContext` parameter
4242
- return type should be `void`, `Task` or `ValueTask`
4343

4444
The type declaring these methods should also respect the following rules:

docs/core/testing/unit-testing-mstest-writing-tests-attributes.md

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ Setup and cleanup that is common to multiple tests can be extracted to a separat
189189

190190
[AssemblyInitialize](<xref:Microsoft.VisualStudio.TestTools.UnitTesting.AssemblyInitializeAttribute>) is called right after your assembly is loaded and [AssemblyCleanup](<xref:Microsoft.VisualStudio.TestTools.UnitTesting.AssemblyCleanupAttribute>) is called right before your assembly is unloaded.
191191

192-
The methods marked with these attributes should be defined as `static void`, `static Task` or `static ValueTask` (starting with MSTest v3.3), in a `TestClass`, and appear only once. The initialize part requires one argument of type [TestContext](xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestContext) and the cleanup no argument.
192+
The methods marked with these attributes should be defined as `static void`, `static Task` or `static ValueTask` (starting with MSTest v3.3), in a `TestClass`, and appear only once. The initialize part requires one parameter of type [TestContext](xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestContext) and the cleanup either no parameters, or starting with MSTest 3.8 can have one parameter of type [TestContext](xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestContext).
193193

194194
```csharp
195195
[TestClass]
@@ -201,23 +201,7 @@ public class MyTestClass
201201
}
202202

203203
[AssemblyCleanup]
204-
public static void AssemblyCleanup()
205-
{
206-
}
207-
}
208-
```
209-
210-
```csharp
211-
[TestClass]
212-
public class MyOtherTestClass
213-
{
214-
[AssemblyInitialize]
215-
public static async Task AssemblyInitialize(TestContext testContext)
216-
{
217-
}
218-
219-
[AssemblyCleanup]
220-
public static async Task AssemblyCleanup()
204+
public static void AssemblyCleanup() // Starting with MSTest 3.8, it can be AssemblyCleanup(TestContext testContext)
221205
{
222206
}
223207
}
@@ -231,7 +215,7 @@ It's possible to control the inheritance behavior: only for current class using
231215

232216
It's also possible to configure whether the class cleanup should be run at the end of the class or at the end of the assembly.
233217

234-
The methods marked with these attributes should be defined as `static void`, `static Task` or `static ValueTask` (starting with MSTest v3.3), in a `TestClass`, and appear only once. The initialize part requires one argument of type [TestContext](xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestContext) and the cleanup no argument.
218+
The methods marked with these attributes should be defined as `static void`, `static Task` or `static ValueTask` (starting with MSTest v3.3), in a `TestClass`, and appear only once. The initialize part requires one parameter of type [TestContext](xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestContext) and the cleanup either no parameters, or starting with MSTest 3.8 can have one parameter of type [TestContext](xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestContext).
235219

236220
```csharp
237221
[TestClass]
@@ -243,7 +227,7 @@ public class MyTestClass
243227
}
244228

245229
[ClassCleanup]
246-
public static void ClassCleanup()
230+
public static void ClassCleanup() // Starting with MSTest 3.8, it can be ClassCleanup(TestContext testContext)
247231
{
248232
}
249233
}
@@ -259,7 +243,7 @@ public class MyOtherTestClass
259243
}
260244

261245
[ClassCleanup]
262-
public static async Task ClassCleanup()
246+
public static async Task ClassCleanup() // Starting with MSTest 3.8, it can be ClassCleanup(TestContext testContext)
263247
{
264248
}
265249
}

0 commit comments

Comments
 (0)