Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/core/testing/mstest-analyzers/design-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ Identifier | Name | Description
[MSTEST0025](mstest0025.md) | PreferAssertFailOverAlwaysFalseConditionsAnalyzer | Use 'Assert.Fail' instead of an always-failing assert
[MSTEST0029](mstest0029.md) | PublicMethodShouldBeTestMethod | A `public` method of a class marked with `[TestClass]` should be a test method (marked with `[TestMethod]`). The rule ignores methods that are marked with `[TestInitialize]`, or `[TestCleanup]` attributes.
[MSTEST0036](mstest0036.md) | DoNotUseShadowingAnalyzer | Shadowing test members could cause testing issues (such as NRE).
[MSTEST0044](mstest0044.md) | PreferTestMethodOverDataTestMethodAnalyzer | A method or type uses <xref:Microsoft.VisualStudio.TestTools.UnitTesting.DataTestMethodAttribute> or inherits from it.
[MSTEST0045](mstest0045.md) | UseCooperativeCancellationForTimeoutAnalyzer | A test method uses <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TimeoutAttribute> without setting the `CooperativeCancellation` property to `true`.
41 changes: 41 additions & 0 deletions docs/core/testing/mstest-analyzers/mstest0044.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
title: "MSTEST0044: Prefer TestMethod over DataTestMethod"
description: "Learn about code analysis rule MSTEST0044: Prefer TestMethod over DataTestMethod"
ms.date: 07/24/2025
f1_keywords:
- MSTEST0044
- PreferTestMethodOverDataTestMethodAnalyzer
helpviewer_keywords:
- PreferTestMethodOverDataTestMethodAnalyzer
- MSTEST0044
author: Evangelink
ms.author: amauryleve
---
# MSTEST0044: Prefer TestMethod over DataTestMethod

| Property | Value |
|-------------------------------------|------------------------------------------------------------------------------------------|
| **Rule ID** | MSTEST0044 |
| **Title** | Prefer TestMethod over DataTestMethod |
| **Category** | Design |
| **Fix is breaking or non-breaking** | Non-breaking |
| **Enabled by default** | Yes |
| **Default severity** | Warning |
| **Introduced in version** | 3.10.0 |
| **Is there a code fix** | Yes |

## Cause

A method or type uses <xref:Microsoft.VisualStudio.TestTools.UnitTesting.DataTestMethodAttribute> or inherits from it.

## Rule description

<xref:Microsoft.VisualStudio.TestTools.UnitTesting.DataTestMethodAttribute> provides no additional value over <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute> and will be removed in a future version. Use <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute> instead for all test methods, including those that use data source attributes like <xref:Microsoft.VisualStudio.TestTools.UnitTesting.DataRowAttribute>.

## How to fix violations

Use the provided code fixer to automatically replace <xref:Microsoft.VisualStudio.TestTools.UnitTesting.DataTestMethodAttribute> with <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute>. You can also manually replace the attribute if needed. If you have a custom attribute that inherits from <xref:Microsoft.VisualStudio.TestTools.UnitTesting.DataTestMethodAttribute>, change it to inherit from <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute> instead.

## When to suppress warnings

Don't suppress warnings from this rule. <xref:Microsoft.VisualStudio.TestTools.UnitTesting.DataTestMethodAttribute> will be removed in a future version, so you should migrate to <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute>.
45 changes: 45 additions & 0 deletions docs/core/testing/mstest-analyzers/mstest0045.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
title: "MSTEST0045: Use cooperative cancellation for timeout"
description: "Learn about code analysis rule MSTEST0045: Use cooperative cancellation for timeout"
ms.date: 07/24/2025
f1_keywords:
- MSTEST0045
- UseCooperativeCancellationForTimeoutAnalyzer
helpviewer_keywords:
- UseCooperativeCancellationForTimeoutAnalyzer
- MSTEST0045
author: Evangelink
ms.author: amauryleve
---
# MSTEST0045: Use cooperative cancellation for timeout

| Property | Value |
|-------------------------------------|------------------------------------------------------------------------------------------|
| **Rule ID** | MSTEST0045 |
| **Title** | Use cooperative cancellation for timeout |
| **Category** | Design |
| **Fix is breaking or non-breaking** | Non-breaking |
| **Enabled by default** | Yes |
| **Default severity** | Info |
| **Introduced in version** | 3.10.0 |
| **Is there a code fix** | Yes |

## Cause

A test method uses <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TimeoutAttribute> without setting the `CooperativeCancellation` property to `true`.

## Rule description

When using <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TimeoutAttribute>, you should set `CooperativeCancellation` to `true` to enable cooperative cancellation. Without cooperative cancellation, the test framework stops observing the test execution when the timeout is reached, but the test continues running in the background. This can lead to problems for other tests or cleanup steps, as the original test is still executing and might interfere with subsequent operations.

When using cooperative cancellation mode, MSTest only triggers cancellation of the token and you're responsible for flowing and utilizing the test context token in your test code. This mode aligns with the default behavior of cancellation in .NET.

## How to fix violations

Use the provided code fixer to automatically set the `CooperativeCancellation` property to `true` on the <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TimeoutAttribute>. You can also manually add the property if needed.

Alternatively, you can configure cooperative cancellation globally in your [runsettings](/dotnet/core/testing/unit-testing-mstest-configure#mstest-element) or [testconfig.json](/dotnet/core/testing/unit-testing-mstest-configure#timeout-settings) file to apply this setting to all timeout attributes in your test project.

## When to suppress warnings

Suppress this warning if you specifically need the test to be terminated abruptly when the timeout is reached, rather than using cooperative cancellation.
52 changes: 52 additions & 0 deletions docs/core/testing/mstest-analyzers/mstest0046.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
title: "MSTEST0046: Use Assert instead of StringAssert"
description: "Learn about code analysis rule MSTEST0046: Use Assert instead of StringAssert"
ms.date: 07/24/2025
f1_keywords:
- MSTEST0046
- StringAssertToAssertAnalyzer
helpviewer_keywords:
- StringAssertToAssertAnalyzer
- MSTEST0046
author: Evangelink
ms.author: amauryleve
---
# MSTEST0046: Use Assert instead of StringAssert

| Property | Value |
|-------------------------------------|------------------------------------------------------------------------------------------|
| **Rule ID** | MSTEST0046 |
| **Title** | Use Assert instead of StringAssert |
| **Category** | Usage |
| **Fix is breaking or non-breaking** | Non-breaking |
| **Enabled by default** | Yes |
| **Default severity** | Info |
| **Introduced in version** | 3.10.0 |
| **Is there a code fix** | Yes |

## Cause

A test method uses <xref:Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert> methods instead of equivalent <xref:Microsoft.VisualStudio.TestTools.UnitTesting.Assert> methods.

## Rule description

<xref:Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert> methods have equivalent counterparts in <xref:Microsoft.VisualStudio.TestTools.UnitTesting.Assert> that provide the same functionality. Use the <xref:Microsoft.VisualStudio.TestTools.UnitTesting.Assert> methods for consistency, better readability, improved discoverability, and alignment with behavior across all test frameworks.

The following <xref:Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert> methods have equivalent <xref:Microsoft.VisualStudio.TestTools.UnitTesting.Assert> methods:

- `StringAssert.Contains(value, substring)` → `Assert.Contains(substring, value)`
- `StringAssert.StartsWith(value, substring)` → `Assert.StartsWith(substring, value)`
- `StringAssert.EndsWith(value, substring)` → `Assert.EndsWith(substring, value)`
- `StringAssert.Matches(value, pattern)` → `Assert.MatchesRegex(pattern, value)`
- `StringAssert.DoesNotMatch(value, pattern)` → `Assert.DoesNotMatchRegex(pattern, value)`

> [!WARNING]
> When migrating from <xref:Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert> to <xref:Microsoft.VisualStudio.TestTools.UnitTesting.Assert> methods, be careful about the change in parameter order. In <xref:Microsoft.VisualStudio.TestTools.UnitTesting.Assert> methods, the expected value is always the first parameter.

## How to fix violations

Use the provided code fixer to automatically replace <xref:Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert> method calls with their equivalent <xref:Microsoft.VisualStudio.TestTools.UnitTesting.Assert> methods. You can also manually replace the method calls if needed.

## When to suppress warnings

Don't suppress warnings from this rule. The <xref:Microsoft.VisualStudio.TestTools.UnitTesting.Assert> methods provide the same functionality with better consistency.
55 changes: 55 additions & 0 deletions docs/core/testing/mstest-analyzers/mstest0048.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
title: "MSTEST0048: Avoid TestContext properties in fixture methods"
description: "Learn about code analysis rule MSTEST0048: Avoid TestContext properties in fixture methods"
ms.date: 07/24/2025
f1_keywords:
- MSTEST0048
- TestContextPropertyUsageAnalyzer
helpviewer_keywords:
- TestContextPropertyUsageAnalyzer
- MSTEST0048
author: Evangelink
ms.author: amauryleve
---
# MSTEST0048: Avoid TestContext properties in fixture methods

| Property | Value |
|-------------------------------------|------------------------------------------------------------------------------------------|
| **Rule ID** | MSTEST0048 |
| **Title** | Avoid TestContext properties in fixture methods |
| **Category** | Usage |
| **Fix is breaking or non-breaking** | Non-breaking |
| **Enabled by default** | Yes |
| **Default severity** | Warning |
| **Introduced in version** | 3.10.0 |
| **Is there a code fix** | No |

## Cause

A fixture method (methods with <xref:Microsoft.VisualStudio.TestTools.UnitTesting.AssemblyInitializeAttribute>, <xref:Microsoft.VisualStudio.TestTools.UnitTesting.AssemblyCleanupAttribute>, <xref:Microsoft.VisualStudio.TestTools.UnitTesting.ClassInitializeAttribute>, or <xref:Microsoft.VisualStudio.TestTools.UnitTesting.ClassCleanupAttribute>) accesses restricted <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestContext> properties.

## Rule description

Certain <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestContext> properties aren't available in fixture methods because they're specific to individual test execution:

Properties unavailable in class and assembly fixture methods:

- `TestData`
- `TestDisplayName`
- `DataRow`
- `DataConnection`
- `TestName`
- `ManagedMethod`

Properties unavailable in assembly-level fixture methods:

- `FullyQualifiedTestClassName`
- `ManagedType`

## How to fix violations

Remove the usage of restricted <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestContext> properties from fixture methods. Consider using alternative approaches for class/assembly initialization and cleanup that don't rely on test-specific context information.

## When to suppress warnings

Don't suppress warnings from this rule. Accessing these properties in fixture methods can result in unexpected behavior or runtime errors.
46 changes: 46 additions & 0 deletions docs/core/testing/mstest-analyzers/mstest0049.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
title: "MSTEST0049: Flow TestContext cancellation token"
description: "Learn about code analysis rule MSTEST0049: Flow TestContext cancellation token"
ms.date: 07/24/2025
f1_keywords:
- MSTEST0049
- FlowTestContextCancellationTokenAnalyzer
helpviewer_keywords:
- FlowTestContextCancellationTokenAnalyzer
- MSTEST0049
author: Evangelink
ms.author: amauryleve
---
# MSTEST0049: Flow TestContext cancellation token

| Property | Value |
|-------------------------------------|------------------------------------------------------------------------------------------|
| **Rule ID** | MSTEST0049 |
| **Title** | Flow TestContext cancellation token |
| **Category** | Usage |
| **Fix is breaking or non-breaking** | Non-breaking |
| **Enabled by default** | Yes |
| **Default severity** | Info |
| **Introduced in version** | 3.10.0 |
| **Is there a code fix** | Yes |

## Cause

A method call within a test context doesn't use the <xref:System.Threading.CancellationToken> available from <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestContext> when the called method has a parameter or overload that accepts a <xref:System.Threading.CancellationToken>.

## Rule description

When <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestContext> is available in your test methods, you should flow its <xref:System.Threading.CancellationToken> to async operations. This enables cooperative cancellation when timeouts occur and ensures proper resource cleanup. The <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestContext> cancellation token is especially important when using cooperative cancellation with timeout attributes.

This rule triggers when:

- A method call has an optional <xref:System.Threading.CancellationToken> parameter that isn't explicitly provided.
- A method call has an overload that accepts a <xref:System.Threading.CancellationToken> but the non-cancellable overload is used instead.

## How to fix violations

Use the provided code fixer to automatically pass the <xref:System.Threading.CancellationToken> from <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestContext> to method calls that support cancellation. You can also manually update method calls to include `TestContext.CancellationToken` as a parameter.

## When to suppress warnings

Suppress this warning if you intentionally don't want to support cancellation for specific operations, or if the operation should continue even when the test is cancelled.
Loading