Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
81 changes: 81 additions & 0 deletions docs/core/testing/mstest-analyzers/mstest0050.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
---
title: "MSTEST0050: Global test fixture should be valid"
description: "Learn about code analysis rule MSTEST0050: Global test fixture should be valid"
ms.date: 07/29/2025
f1_keywords:
- MSTEST0050
- GlobalTestFixtureShouldBeValidAnalyzer
helpviewer_keywords:
- GlobalTestFixtureShouldBeValidAnalyzer
- MSTEST0050
author: Evangelink
ms.author: amauryleve
---
# MSTEST0050: Global test fixture should be valid

| Property | Value |
|-------------------------------------|------------------------------------------------------------------------------------------|
| **Rule ID** | MSTEST0050 |
| **Title** | Global test fixture should be valid |
| **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** | Yes |

## Cause

A global test fixture method (marked with <xref:Microsoft.VisualStudio.TestTools.UnitTesting.AssemblyInitializeAttribute> or <xref:Microsoft.VisualStudio.TestTools.UnitTesting.AssemblyCleanupAttribute>) doesn't follow the required layout or has invalid configuration.

## Rule description

Global test fixture methods must follow specific requirements to ensure proper test execution. This rule validates that methods marked with <xref:Microsoft.VisualStudio.TestTools.UnitTesting.AssemblyInitializeAttribute> or <xref:Microsoft.VisualStudio.TestTools.UnitTesting.AssemblyCleanupAttribute> adhere to the correct method signature and configuration rules.

Common violations include:

- Incorrect method signatures or parameters
- Invalid access modifiers
- Missing or incorrect return types
- Improper method declarations

## How to fix violations

Ensure that global test fixture methods follow the required layout:

- Methods marked with <xref:Microsoft.VisualStudio.TestTools.UnitTesting.AssemblyInitializeAttribute> should be `public static` and take a <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestContext> parameter
- Methods marked with <xref:Microsoft.VisualStudio.TestTools.UnitTesting.AssemblyCleanupAttribute> should be `public static` and optionally take a <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestContext> parameter (starting with MSTest 3.6)

## When to suppress warnings

Don't suppress warnings from this rule. Global test fixture methods with invalid layouts may not execute properly or may cause runtime errors.

## Example of a violation

```csharp
[TestClass]
public class GlobalTestFixture
{
// Violation: AssemblyInitialize method should be static and take TestContext parameter
[AssemblyInitialize]
public void Setup()
{
// Setup code
}
}
```

## Example of how to fix

```csharp
[TestClass]
public class GlobalTestFixture
{
// Fixed: Correct method signature for AssemblyInitialize
[AssemblyInitialize]
public static void Setup(TestContext testContext)
{
// Setup code
}
}
```
1 change: 1 addition & 0 deletions docs/core/testing/mstest-analyzers/usage-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ Identifier | Name | Description
[MSTEST0046](mstest0046.md) | StringAssertToAssertAnalyzer | A test method uses <xref:Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert> methods instead of equivalent <xref:Microsoft.VisualStudio.TestTools.UnitTesting.Assert> methods.
[MSTEST0048](mstest0048.md) | TestContextPropertyUsageAnalyzer | 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.
[MSTEST0049](mstest0049.md) | FlowTestContextCancellationTokenAnalyzer | 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>.
[MSTEST0050](mstest0050.md) | GlobalTestFixtureShouldBeValidAnalyzer | A global test fixture method (marked with <xref:Microsoft.VisualStudio.TestTools.UnitTesting.AssemblyInitializeAttribute> or <xref:Microsoft.VisualStudio.TestTools.UnitTesting.AssemblyCleanupAttribute>) doesn't follow the required layout or has invalid configuration.
Loading