Skip to content
Open
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: 1 addition & 1 deletion docs/core/testing/mstest-analyzers/mstest0055.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ ai-usage: ai-generated

## Cause

A call to `string.Contains`, `string.StartsWith`, or `string.EndsWith` is made and its return value is ignored.
A call to <xref:System.String.Contains%2A>, <xref:System.String.StartsWith%2A>, or <xref:System.String.EndsWith%2A> is made and its return value is ignored.

## Rule description

Expand Down
87 changes: 87 additions & 0 deletions docs/core/testing/mstest-analyzers/mstest0056.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
---
title: "MSTEST0056: Use DisplayName property for test method display names"
description: "Learn about code analysis rule MSTEST0056: Use DisplayName property for test method display names"
ms.date: 12/29/2025
f1_keywords:
- MSTEST0056
- TestMethodAttributeShouldSetDisplayNameCorrectlyAnalyzer
helpviewer_keywords:
- TestMethodAttributeShouldSetDisplayNameCorrectlyAnalyzer
- MSTEST0056
author: evangelink
ms.author: amauryleve
ai-usage: ai-assisted
dev_langs:
- CSharp
---
# MSTEST0056: Use DisplayName property for test method display names

| Property | Value |
|-------------------------------------|----------------------------------------------------|
| **Rule ID** | MSTEST0056 |
| **Title** | Use DisplayName property for test method display names |
| **Category** | Usage |
| **Fix is breaking or non-breaking** | Non-breaking |
| **Enabled by default** | Yes |
| **Default severity** | Warning |
| **Introduced in version** | 4.0.0 |
| **Is there a code fix** | Yes |

## Cause

A test method attribute uses a string constructor argument instead of the <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute.DisplayName> property.

## Rule description

When specifying a custom display name for a test method, you should use the <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute.DisplayName> property instead of passing a string as a constructor argument. This ensures consistent usage across the MSTest framework and improves code readability.

```csharp
[TestClass]
public class TestClass
{
[TestMethod("My Test Name")] // Violation
public void TestMethod()
{
// Test code
}
}
```

## How to fix violations

Replace the constructor argument with the `DisplayName` property.

```csharp
[TestClass]
public class TestClass
{
[TestMethod(DisplayName = "My Test Name")]
public void TestMethod()
{
// Test code
}
}
```

## When to suppress warnings

Do not suppress warnings from this rule. Using the `DisplayName` property is the correct and recommended way to specify custom display names for test methods.

## Suppress a warning

If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.

```csharp
#pragma warning disable MSTEST0056
// The code that's violating the rule is on this line.
#pragma warning restore MSTEST0056
```

To disable the rule for a file, folder, or project, set its severity to `none` in the [configuration file](../../../fundamentals/code-analysis/configuration-files.md).

```ini
[*.{cs,vb}]
dotnet_diagnostic.MSTEST0056.severity = none
```

For more information, see [How to suppress code analysis warnings](../../../fundamentals/code-analysis/suppress-warnings.md).
87 changes: 87 additions & 0 deletions docs/core/testing/mstest-analyzers/mstest0057.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
---
title: "MSTEST0057: Propagate source information in custom test method attributes"
description: "Learn about code analysis rule MSTEST0057: Propagate source information in custom test method attributes"
ms.date: 12/29/2025
f1_keywords:
- MSTEST0057
- TestMethodAttributeShouldPropagateSourceInformationAnalyzer
helpviewer_keywords:
- TestMethodAttributeShouldPropagateSourceInformationAnalyzer
- MSTEST0057
author: evangelink
ms.author: amauryleve
ai-usage: ai-assisted
dev_langs:
- CSharp
---
# MSTEST0057: Propagate source information in custom test method attributes

| Property | Value |
|-------------------------------------|----------------------------------------------------|
| **Rule ID** | MSTEST0057 |
| **Title** | Propagate source information in custom test method attributes |
| **Category** | Usage |
| **Fix is breaking or non-breaking** | Non-breaking |
| **Enabled by default** | Yes |
| **Default severity** | Warning |
| **Introduced in version** | 4.0.0 |
| **Is there a code fix** | Yes |

## Cause

A custom <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute> class does not propagate caller information to the base class constructor.

## Rule description

When creating custom test method attributes that derive from <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute>, you should propagate source information using caller information attributes. This allows MSTest to correctly track the source file and line number for test methods, improving diagnostics and test result reporting.

```csharp
public class MyTestMethodAttribute : TestMethodAttribute
{
public MyTestMethodAttribute() // Violation
: base()
{
}
}
```

## How to fix violations

Add `CallerFilePath` and `CallerLineNumber` parameters to the constructor and pass them to the base class.

```csharp
using System.Runtime.CompilerServices;

public class MyTestMethodAttribute : TestMethodAttribute
{
public MyTestMethodAttribute(
[CallerFilePath] string callerFilePath = "",
[CallerLineNumber] int callerLineNumber = -1)
: base(callerFilePath, callerLineNumber)
{
}
}
```

## When to suppress warnings

Do not suppress warnings from this rule. Propagating source information is essential for proper test reporting and diagnostics.

## Suppress a warning

If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.

```csharp
#pragma warning disable MSTEST0057
// The code that's violating the rule is on this line.
#pragma warning restore MSTEST0057
```

To disable the rule for a file, folder, or project, set its severity to `none` in the [configuration file](../../../fundamentals/code-analysis/configuration-files.md).

```ini
[*.{cs,vb}]
dotnet_diagnostic.MSTEST0057.severity = none
```

For more information, see [How to suppress code analysis warnings](../../../fundamentals/code-analysis/suppress-warnings.md).
105 changes: 105 additions & 0 deletions docs/core/testing/mstest-analyzers/mstest0058.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
---
title: "MSTEST0058: Avoid assertions in catch blocks"
description: "Learn about code analysis rule MSTEST0058: Avoid assertions in catch blocks"
ms.date: 12/29/2025
f1_keywords:
- MSTEST0058
- AvoidAssertsInCatchBlocksAnalyzer
helpviewer_keywords:
- AvoidAssertsInCatchBlocksAnalyzer
- MSTEST0058
author: evangelink
ms.author: amauryleve
ai-usage: ai-assisted
dev_langs:
- CSharp
---
# MSTEST0058: Avoid assertions in `catch` blocks

| Property | Value |
|-------------------------------------|----------------------------------------------------|
| **Rule ID** | MSTEST0058 |
| **Title** | Avoid assertions in catch blocks |
| **Category** | Usage |
| **Fix is breaking or non-breaking** | Non-breaking |
| **Enabled by default** | Yes |
| **Default severity** | Info |
| **Introduced in version** | 4.1.0 |
| **Is there a code fix** | No |

## Cause

A test method contains assertion statements within a `catch` block.

## Rule description

Placing assertions in `catch` blocks is an anti-pattern that can lead to confusing test results and makes tests harder to understand. When an exception is thrown, the `catch` block executes, and the assertion runs. However, if no exception is thrown, the `catch` block never executes, potentially giving false confidence that the test passed.

Instead, use <xref:Microsoft.VisualStudio.TestTools.UnitTesting.Assert.Throws*?displayProperty=nameWithType> or similar methods to verify that expected exceptions are thrown. This makes the test intent clearer and ensures proper test behavior.

```csharp
[TestClass]
public class TestClass
{
[TestMethod]
public void TestMethod()
{
try
{
// Code that might throw.
DoSomethingThatMightThrow();
}
catch
{
Assert.Fail("Exception was thrown"); // Violation
}
}
}
```

## How to fix violations

Use `Assert.ThrowsException` or related assertion methods to test for exceptions.

```csharp
[TestClass]
public class TestClass
{
[TestMethod]
public void TestMethod_ThrowsException()
{
Assert.Throws<InvalidOperationException>(() => DoSomethingThatMightThrow());
}

[TestMethod]
public void TestMethod_DoesNotThrow()
{
// If no exception is expected, just call the method directly
DoSomethingThatMightNotThrow();
// Add positive assertions here
}
}
```

## When to suppress warnings

You might suppress this warning if you have a legitimate need to catch an exception and perform complex validation that cannot be easily expressed using standard assertion methods. However, consider refactoring your test to use more explicit exception assertions first.

## Suppress a warning

If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.

```csharp
#pragma warning disable MSTEST0058
// The code that's violating the rule is on this line.
#pragma warning restore MSTEST0058
```

To disable the rule for a file, folder, or project, set its severity to `none` in the [configuration file](../../../fundamentals/code-analysis/configuration-files.md).

```ini
[*.{cs,vb}]
dotnet_diagnostic.MSTEST0058.severity = none
```

For more information, see [How to suppress code analysis warnings](../../../fundamentals/code-analysis/suppress-warnings.md).
86 changes: 86 additions & 0 deletions docs/core/testing/mstest-analyzers/mstest0059.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
---
title: "MSTEST0059: Do not use both Parallelize and DoNotParallelize attributes"
description: "Learn about code analysis rule MSTEST0059: Do not use both Parallelize and DoNotParallelize attributes"
ms.date: 12/29/2025
f1_keywords:
- MSTEST0059
- UseParallelizeAttributeAnalyzer
helpviewer_keywords:
- UseParallelizeAttributeAnalyzer
- MSTEST0059
author: evangelink
ms.author: amauryleve
ai-usage: ai-assisted
dev_langs:
- CSharp
---
# MSTEST0059: Do not use both Parallelize and DoNotParallelize attributes

| Property | Value |
|-------------------------------------|----------------------------------------------------|
| **Rule ID** | MSTEST0059 |
| **Title** | Do not use both Parallelize and DoNotParallelize attributes |
| **Category** | Usage |
| **Fix is breaking or non-breaking** | Non-breaking |
| **Enabled by default** | Yes |
| **Default severity** | Warning |
| **Introduced in version** | 4.1.0 |
| **Is there a code fix** | No |

## Cause

An assembly contains both <xref:Microsoft.VisualStudio.TestTools.UnitTesting.ParallelizeAttribute> and <xref:Microsoft.VisualStudio.TestTools.UnitTesting.DoNotParallelizeAttribute> attributes.

## Rule description

The <xref:Microsoft.VisualStudio.TestTools.UnitTesting.ParallelizeAttribute> and <xref:Microsoft.VisualStudio.TestTools.UnitTesting.DoNotParallelizeAttribute> attributes are mutually exclusive. Having both attributes in the same assembly creates conflicting configuration that can lead to unpredictable test execution behavior. You should choose one parallelization strategy for your test assembly.

```csharp
using Microsoft.VisualStudio.TestTools.UnitTesting;

[assembly: Parallelize(Workers = 2, Scope = ExecutionScope.MethodLevel)] // Violation
[assembly: DoNotParallelize]
```

## How to fix violations

Remove one of the conflicting attributes based on your intended parallelization strategy.

If you want parallel execution:

```csharp
using Microsoft.VisualStudio.TestTools.UnitTesting;

[assembly: Parallelize(Workers = 2, Scope = ExecutionScope.MethodLevel)]
```

If you want sequential execution:

```csharp
using Microsoft.VisualStudio.TestTools.UnitTesting;

[assembly: DoNotParallelize]
```

## When to suppress warnings

Do not suppress warnings from this rule. Having both attributes creates ambiguous test configuration that should be resolved.

## Suppress a warning

If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.

```csharp
#pragma warning disable MSTEST0059
// The code that's violating the rule is on this line.
#pragma warning restore MSTEST0059
```

To disable the rule for a file, folder, or project, set its severity to `none` in the [configuration file](../../../fundamentals/code-analysis/configuration-files.md).

```ini
[*.{cs,vb}]
dotnet_diagnostic.MSTEST0059.severity = none
```

For more information, see [How to suppress code analysis warnings](../../../fundamentals/code-analysis/suppress-warnings.md).
Loading