|
| 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 | +``` |
0 commit comments