Skip to content

Commit 295d55a

Browse files
Document MSTEST0039 and MSTEST0040 (#44419)
* Document MSTEST0039 and MSTEST0040 * Fix markdownlint violation * Reword * Update further * Apply suggestions from code review Co-authored-by: Genevieve Warren <[email protected]> --------- Co-authored-by: Genevieve Warren <[email protected]>
1 parent 5452826 commit 295d55a

File tree

8 files changed

+92
-4
lines changed

8 files changed

+92
-4
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: "[DeploymentItem] can be specified only on test class or test method."
2+
title: "MSTEST0035: [DeploymentItem] can be specified only on test class or test method."
33
description: "Learn about code analysis rule MSTEST0035: `[DeploymentItem]` can be specified only on test class or test method."
44
ms.date: 08/02/2024
55
f1_keywords:

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: "Do not use shadowing inside test class."
2+
title: "MSTEST0036: Do not use shadowing inside test class."
33
description: "Learn about code analysis rule MSTEST0036: Do not use shadowing inside test class."
44
ms.date: 08/19/2024
55
f1_keywords:

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: "Use proper 'Assert' methods"
2+
title: "MSTEST0037: Use proper 'Assert' methods"
33
description: "Learn about code analysis rule MSTEST0037: Use proper 'Assert' methods."
44
ms.date: 11/17/2024
55
f1_keywords:

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: "Don't use 'Assert.AreSame' or 'Assert.AreNotSame' with value types"
2+
title: "MSTEST0038: Don't use 'Assert.AreSame' or 'Assert.AreNotSame' with value types"
33
description: "Learn about code analysis rule MSTEST0038: Don't use 'Assert.AreSame' or 'Assert.AreNotSame' with value types"
44
ms.date: 01/06/2025
55
f1_keywords:
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
title: "MSTEST0039: Use newer 'Assert.Throws' methods"
3+
description: "Learn about code analysis rule MSTEST0039: Use 'Assert.ThrowsExactly' instead of 'Assert.ThrowsException'"
4+
ms.date: 01/17/2025
5+
f1_keywords:
6+
- MSTEST0039
7+
- UseNewerAssertThrowsAnalyzer
8+
helpviewer_keywords:
9+
- UseNewerAssertThrowsAnalyzer
10+
- MSTEST0039
11+
author: Youssef1313
12+
ms.author: ygerges
13+
---
14+
# MSTEST0039: Use newer 'Assert.Throws' methods
15+
16+
| Property | Value |
17+
|-------------------------------------|------------------------------------------------------------------------|
18+
| **Rule ID** | MSTEST0039 |
19+
| **Title** | Use newer 'Assert.Throws' methods |
20+
| **Category** | Usage |
21+
| **Fix is breaking or non-breaking** | Non-breaking |
22+
| **Enabled by default** | Yes |
23+
| **Default severity** | Info |
24+
| **Introduced in version** | 3.8.0 |
25+
| **Is there a code fix** | Yes |
26+
27+
## Cause
28+
29+
The use of <xref:Microsoft.VisualStudio.TestTools.UnitTesting.Assert.ThrowsException*?displayProperty=nameWithType> or <xref:Microsoft.VisualStudio.TestTools.UnitTesting.Assert.ThrowsExceptionAsync*?displayProperty=nameWithType>, which are no longer recommended.
30+
31+
## Rule description
32+
33+
<xref:Microsoft.VisualStudio.TestTools.UnitTesting.Assert.ThrowsException*?displayProperty=nameWithType> and <xref:Microsoft.VisualStudio.TestTools.UnitTesting.Assert.ThrowsExceptionAsync*?displayProperty=nameWithType> are not recommended and might be deprecated in the future.
34+
35+
## How to fix violations
36+
37+
Use `Assert.ThrowsExactly` or `Assert.ThrowsExactlyAsync` instead of `Assert.ThrowsException` or `Assert.ThrowsExceptionAsync`.
38+
39+
## When to suppress warnings
40+
41+
Do not suppress a warning from this rule. It's strongly recommended to move from the old APIs to the new ones.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
title: "MSTEST0040: Do not assert inside 'async void' contexts"
3+
description: "Learn about code analysis rule MSTEST0040: Do not assert inside 'async void' methods, local functions, or lambdas because they may not fail the test"
4+
ms.date: 01/17/2025
5+
f1_keywords:
6+
- MSTEST0040
7+
- AvoidUsingAssertsInAsyncVoidContextAnalyzer
8+
helpviewer_keywords:
9+
- AvoidUsingAssertsInAsyncVoidContextAnalyzer
10+
- MSTEST0040
11+
author: Youssef1313
12+
ms.author: ygerges
13+
---
14+
# MSTEST0040: Do not assert inside 'async void' contexts
15+
16+
| Property | Value |
17+
|-------------------------------------|------------------------------------------------------------------------|
18+
| **Rule ID** | MSTEST0040 |
19+
| **Title** | Do not assert inside 'async void' contexts |
20+
| **Category** | Usage |
21+
| **Fix is breaking or non-breaking** | Non-breaking |
22+
| **Enabled by default** | Yes |
23+
| **Default severity** | Warning |
24+
| **Introduced in version** | 3.8.0 |
25+
| **Is there a code fix** | No |
26+
27+
## Cause
28+
29+
The use of any assertion method in an `async void` method, local function, or lambda.
30+
31+
## Rule description
32+
33+
Exceptions that are thrown in an `async void` context are unhandled. A failing assertion in an `async void` method will be swallowed and will not crash the process when using VSTest under .NET Framework. Under .NET, a failing assertion in an `async void` method might crash the process when using Microsoft.Testing.Platform or VSTest. In cases where a custom `SynchronizationContext` is present that catches the exception, the failing assertion might be silently swallowed.
34+
35+
## How to fix violations
36+
37+
Refactor the code to not use assertions in `async void`.
38+
39+
## When to suppress warnings
40+
41+
Do not suppress a warning from this rule.

docs/core/testing/mstest-analyzers/usage-rules.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,5 @@ Identifier | Name | Description
3535
[MSTEST0035](mstest0035.md) | UseDeploymentItemWithTestMethodOrTestClassTitle | `[DeploymentItem]` can be specified only on test class or test method
3636
[MSTEST0037](mstest0037.md) | UseProperAssertMethodsAnalyzer | Use proper `Assert` methods
3737
[MSTEST0038](mstest0038.md) | AvoidAssertAreSameWithValueTypesAnalyzer | Don't use `Assert.AreSame` or `Assert.AreNotSame` with value types
38+
[MSTEST0039](mstest0039.md) | UseNewerAssertThrowsAnalyzer | Use newer 'Assert.Throws' methods
39+
[MSTEST0040](mstest0040.md) | AvoidUsingAssertsInAsyncVoidContextAnalyzer | Do not assert inside 'async void' contexts

docs/navigate/devops-testing/toc.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,10 @@ items:
193193
href: ../../core/testing/mstest-analyzers/mstest0037.md
194194
- name: MSTEST0038
195195
href: ../../core/testing/mstest-analyzers/mstest0038.md
196+
- name: MSTEST0039
197+
href: ../../core/testing/mstest-analyzers/mstest0039.md
198+
- name: MSTEST0040
199+
href: ../../core/testing/mstest-analyzers/mstest0040.md
196200
- name: Microsoft Testing Platform
197201
items:
198202
- name: Overview

0 commit comments

Comments
 (0)