Skip to content

Commit 7ed1358

Browse files
CopilotYoussef1313
andcommitted
Add documentation for MSTEST0050 analyzer rule
Co-authored-by: Youssef1313 <[email protected]>
1 parent e2af697 commit 7ed1358

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
---
2+
title: "MSTEST0050: Global test fixture should be valid"
3+
description: "Learn about code analysis rule MSTEST0050: Global test fixture should be valid"
4+
ms.date: 07/29/2025
5+
f1_keywords:
6+
- MSTEST0050
7+
- GlobalTestFixtureShouldBeValidAnalyzer
8+
helpviewer_keywords:
9+
- GlobalTestFixtureShouldBeValidAnalyzer
10+
- MSTEST0050
11+
author: Evangelink
12+
ms.author: amauryleve
13+
---
14+
# MSTEST0050: Global test fixture should be valid
15+
16+
| Property | Value |
17+
|-------------------------------------|------------------------------------------------------------------------------------------|
18+
| **Rule ID** | MSTEST0050 |
19+
| **Title** | Global test fixture should be valid |
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.10.0 |
25+
| **Is there a code fix** | Yes |
26+
27+
## Cause
28+
29+
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.
30+
31+
## Rule description
32+
33+
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.
34+
35+
Common violations include:
36+
37+
- Incorrect method signatures or parameters
38+
- Invalid access modifiers
39+
- Missing or incorrect return types
40+
- Improper method declarations
41+
42+
## How to fix violations
43+
44+
Ensure that global test fixture methods follow the required layout:
45+
46+
- Methods marked with <xref:Microsoft.VisualStudio.TestTools.UnitTesting.AssemblyInitializeAttribute> should be `public static` and take a <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestContext> parameter
47+
- 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)
48+
49+
## When to suppress warnings
50+
51+
Don't suppress warnings from this rule. Global test fixture methods with invalid layouts may not execute properly or may cause runtime errors.
52+
53+
## Example of a violation
54+
55+
```csharp
56+
[TestClass]
57+
public class GlobalTestFixture
58+
{
59+
// Violation: AssemblyInitialize method should be static and take TestContext parameter
60+
[AssemblyInitialize]
61+
public void Setup()
62+
{
63+
// Setup code
64+
}
65+
}
66+
```
67+
68+
## Example of how to fix
69+
70+
```csharp
71+
[TestClass]
72+
public class GlobalTestFixture
73+
{
74+
// Fixed: Correct method signature for AssemblyInitialize
75+
[AssemblyInitialize]
76+
public static void Setup(TestContext testContext)
77+
{
78+
// Setup code
79+
}
80+
}
81+
```

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,4 @@ Identifier | Name | Description
4343
[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.
4444
[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.
4545
[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>.
46+
[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.

0 commit comments

Comments
 (0)