|
| 1 | + |
| 2 | +# MSTest v3 Migration Guide |
| 3 | + |
| 4 | +## Overview |
| 5 | +This guide assists users in upgrading their MSTest projects from v0/v1 to MSTest v3. MSTest v3 introduces new features, improvements, and some breaking changes. |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## Introduction and Motivation |
| 10 | + |
| 11 | +Migrating to MSTest v3 offers several benefits: |
| 12 | +- Improved test execution speeds and memory efficiency. |
| 13 | +- Stricter validation for more reliable tests. |
| 14 | +- Leveraging modern .NET features and cross-platform compatibility. |
| 15 | + |
| 16 | +--- |
| 17 | + |
| 18 | +## Steps to Migrate |
| 19 | + |
| 20 | +### 1. Remove Assembly Reference |
| 21 | +Remove the `Microsoft.VisualStudio.QualityTools.UnitTestFramework` reference from your project. |
| 22 | + |
| 23 | +```xml |
| 24 | +<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> |
| 25 | + <Private>False</Private> |
| 26 | +</Reference> |
| 27 | +``` |
| 28 | + |
| 29 | +### 2. Update Your Project |
| 30 | +- **NuGet Package**: Install the latest [MSTest](https://www.nuget.org/packages/MSTest). |
| 31 | +- **Project File**: Update your project file to use MSTest SDK. |
| 32 | + |
| 33 | +```xml |
| 34 | +<Project Sdk="MSTest.Sdk/3.3.1"> |
| 35 | + <PropertyGroup> |
| 36 | + <TargetFramework>net8.0</TargetFramework> |
| 37 | + </PropertyGroup> |
| 38 | +</Project> |
| 39 | +``` |
| 40 | + |
| 41 | +### 3. Update Your Code |
| 42 | +- **Replace Deprecated Methods**: Update deprecated methods to newer versions. |
| 43 | + - `Assert.AreEqual/AreNotEqual` (with object) → Use generic versions. |
| 44 | + - `Assert.AreSame/AreNotSame` (with object) → Use generic versions. |
| 45 | +- **Test Initialization**: Use `TestInitialize` methods for async initialization. |
| 46 | +- **Cleanup**: Use `TestCleanup` methods or the `Dispose` pattern for cleanup. |
| 47 | +- **RunSettings**: The `.testsettings` file is no longer supported, meaning `<LegacySettings>` is also no longer available. Use [.runsettings](https://learn.microsoft.com/en-gb/visualstudio/test/configure-unit-tests-by-using-a-dot-runsettings-file?view=vs-2022) for test configuration. |
| 48 | + |
| 49 | +--- |
| 50 | + |
| 51 | +## New Features in MSTest v3 |
| 52 | + |
| 53 | +- Improved defaults for test projects |
| 54 | +- Simplified setup and usage |
| 55 | +- Enhanced extensibility of the MSTest runner |
| 56 | +- New Roslyn-based code analyzers for improved test development |
| 57 | +- Support for WinUI applications |
| 58 | +- In-assembly parallel execution |
| 59 | +- Dynamic data sources for data-driven tests |
| 60 | + |
| 61 | +--- |
| 62 | + |
| 63 | +## Deprecated Features |
| 64 | + |
| 65 | +- Dropped support for: |
| 66 | + - **.NET Framework 4.5** (use .NET 4.6.2 or higher) |
| 67 | + - **.NET Standard 1.0** (use .NET Standard 2.0) |
| 68 | + - **UWP versions before 16299** |
| 69 | + - **WinUI versions before 18362** |
| 70 | + - **.NET 5** (use .NET Core 3.1 or .NET 6) |
| 71 | + |
| 72 | +--- |
| 73 | + |
| 74 | +## Breaking Changes and Removed APIs |
| 75 | + |
| 76 | +### Assertion Overloads |
| 77 | +MSTest v3 removes certain `Assert` overloads that accept object types to promote type-safe assertions. Update tests using these overloads to specify types explicitly. |
| 78 | + |
| 79 | +**Example**: |
| 80 | + |
| 81 | +```csharp |
| 82 | +// Old (v0/v1) |
| 83 | +Assert.AreEqual(expected, actual); // both are objects |
| 84 | +
|
| 85 | +// New (v3) |
| 86 | +Assert.AreEqual<int>(expectedInt, actualInt); // specify the type |
| 87 | +``` |
| 88 | + |
| 89 | +### DataRowAttribute Updates |
| 90 | +The `DataRowAttribute` constructors have been simplified in MSTest v3. Update parameterized tests using `DataRow` to align with the revised constructors. |
| 91 | + |
| 92 | +**Example**: |
| 93 | + |
| 94 | +```csharp |
| 95 | +[TestMethod] |
| 96 | +[DataRow(1, "test")] |
| 97 | +public void MyTestMethod(int number, string text) { ... } |
| 98 | +``` |
| 99 | + |
| 100 | +### Timeout Settings |
| 101 | +Timeout settings are standardized across frameworks in MSTest v3. Verify and adjust any timeout configurations in existing tests for compatibility. |
| 102 | + |
| 103 | +--- |
| 104 | + |
| 105 | +## Configuration Changes |
| 106 | + |
| 107 | +### Settings and Configuration Files |
| 108 | +MSTest v3 supports both XML and JSON formats for configuration files. |
| 109 | + |
| 110 | +1. **Verify Configurations**: Ensure that existing `.runsettings` files align with MSTest v3’s syntax and structure. |
| 111 | +2. **Convert to JSON (if applicable)**: For JSON-based configurations, convert XML settings to JSON format. |
| 112 | + |
| 113 | +--- |
| 114 | + |
| 115 | +## Parallel Execution and Performance Optimization |
| 116 | + |
| 117 | +### New Parallelism Options |
| 118 | +Configure parallel execution in `.runsettings` or JSON configuration files to improve performance. |
| 119 | + |
| 120 | +**Example**: |
| 121 | + |
| 122 | +```json |
| 123 | +{ |
| 124 | + "RunConfiguration": { |
| 125 | + "MaxCpuCount": -1 // Uses all available processors |
| 126 | + } |
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | +### Improved Resource Usage |
| 131 | +MSTest v3 optimizes resource management, resulting in lower memory usage and better CPU efficiency. |
| 132 | + |
| 133 | +--- |
| 134 | + |
| 135 | +## Handling Obsolete Attributes and Migrating Custom Extensions |
| 136 | +Review deprecated attributes, such as `[DeploymentItem]`, and replace them with MSTest v3 alternatives where possible. |
| 137 | + |
| 138 | +--- |
| 139 | + |
| 140 | +## Code Analyzers and Best Practices |
| 141 | +MSTest v3 includes built-in code analyzers for best practices, avoiding configuration pitfalls, and proper use of MSTest attributes and settings. |
| 142 | + |
| 143 | +--- |
| 144 | + |
| 145 | +## Sample Code: Migration Example |
| 146 | + |
| 147 | +### Before (MSTest v1) |
| 148 | +```csharp |
| 149 | +[TestMethod] |
| 150 | +[Timeout(1000)] |
| 151 | +[DataRow(1, "data")] |
| 152 | +public void ExampleTestMethod(int number, string data) { |
| 153 | + Assert.AreEqual(number, Convert.ToInt32(data)); |
| 154 | +} |
| 155 | +``` |
| 156 | + |
| 157 | +### After (MSTest v3) |
| 158 | +```csharp |
| 159 | +[TestMethod] |
| 160 | +[Timeout(1000)] |
| 161 | +[DataRow(1, "data")] |
| 162 | +public void ExampleTestMethod(int number, string data) { |
| 163 | + Assert.AreEqual<int>(number, Convert.ToInt32(data)); |
| 164 | +} |
| 165 | + |
0 commit comments