Loading...
Loading...
Compare original and translation side by side
dotnet testImportant: Do not mix VSTest-based and MTP-based .NET test projects in the same solution or run configuration -- this is an unsupported scenario.
dotnet test重要提示:请勿在同一解决方案或运行配置中混合使用基于VSTest和基于MTP的.NET测试项目——这属于不支持的场景。
dotnet rundotnet watchvstest.console.exedotnet testdotnet testdotnet rundotnet watchdotnet testvstest.console.exedotnet testmigrate-mstest-v1v2-to-v3migrate-mstest-v3-to-v4migrate-mstest-v1v2-to-v3migrate-mstest-v3-to-v4| Input | Required | Description |
|---|---|---|
| Project or solution path | Yes | The |
| Test framework | No | MSTest, NUnit, xUnit.net v2, or xUnit.net v3. Auto-detected from package references |
| .NET SDK version | No | Determines |
| CI/CD pipeline files | No | Paths to pipeline definitions that invoke |
| 输入项 | 是否必填 | 描述 |
|---|---|---|
| 项目或解决方案路径 | 是 | 包含测试项目的 |
| 测试框架 | 否 | MSTest、NUnit、xUnit.net v2或xUnit.net v3。将从包引用中自动检测 |
| .NET SDK版本 | 否 | 决定 |
| CI/CD流水线文件 | 否 | 调用 |
platform-detectionMSTestMSTest.TestAdapterMSTest.Sdk<IsTestApplication>falseMSTest.TestFrameworkNUnit3TestAdapterxunitxunit.runner.visualstudiodotnet --versiondotnet testDirectory.Build.propsvstest.console.exedotnet test--filter--logger--collect--settings--blame*dotnet testplatform-detectionMSTestMSTest.TestAdapterMSTest.Sdk<IsTestApplication>falseMSTest.TestFrameworkNUnit3TestAdapterxunitxunit.runner.visualstudiodotnet --versiondotnet testDirectory.Build.propsvstest.console.exedotnet test--filter--logger--collect--settings--blame*dotnet testCritical: Set MTP runner properties inat the solution or repo root whenever possible, rather than per-project. This prevents inconsistent configuration where some projects use VSTest and others use MTP (an unsupported scenario). Note: MTP also requires test projects to haveDirectory.Build.props. Only<OutputType>Exe</OutputType>sets this automatically. For all other setups (MSTest NuGet packages withMSTest.Sdk, NUnit withEnableMSTestRunner, xUnit.net withEnableNUnitRunner), prefer settingYTest.MTP.XUnit2centrally in<OutputType>Exe</OutputType>with a condition that targets only test projects. If you cannot reliably target only test projects fromDirectory.Build.props, settingDirectory.Build.propsper-project is an acceptable exception.<OutputType>Exe</OutputType>Conditioning in: Do NOT useDirectory.Build.props--Condition="'$(IsTestProject)' == 'true'"is set by the test SDK targets later in evaluation and is not available whenIsTestProjectis imported. Use a property that is available early, such asDirectory.Build.props, to target test projects by naming convention. For example, if all test projects end inMSBuildProjectName:.Testsxml<PropertyGroup Condition="$(MSBuildProjectName.EndsWith('.Tests'))"> <OutputType>Exe</OutputType> </PropertyGroup>Adjust the condition (e.g.,,.EndsWith('Tests')) to match the test project naming convention used in the repository..Contains('.Test')
关键提示:尽可能在解决方案或仓库根目录的中设置MTP运行器属性,而非逐个项目配置。这可避免部分项目使用VSTest、部分使用MTP的不一致配置(属于不支持的场景)。 注意:MTP还要求测试项目设置Directory.Build.props。仅<OutputType>Exe</OutputType>会自动设置该属性。对于其他所有配置(使用MSTest.Sdk的MSTest NuGet包、使用EnableMSTestRunner的NUnit、使用EnableNUnitRunner的xUnit.net),建议在YTest.MTP.XUnit2中通过条件仅针对测试项目集中设置Directory.Build.props。如果无法通过<OutputType>Exe</OutputType>可靠地仅定位测试项目,则逐个项目设置Directory.Build.props是可接受的例外情况。<OutputType>Exe</OutputType>Directory.Build.props中的条件设置:请勿使用——Condition="'$(IsTestProject)' == 'true'"是在评估后期由测试SDK目标设置的,在导入IsTestProject时尚未可用。请使用早期可用的属性,例如Directory.Build.props,通过命名约定定位测试项目。例如,如果所有测试项目均以MSBuildProjectName结尾:.Testsxml<PropertyGroup Condition="$(MSBuildProjectName.EndsWith('.Tests'))"> <OutputType>Exe</OutputType> </PropertyGroup>根据仓库中使用的测试项目命名约定调整条件(例如、.EndsWith('Tests'))。.Contains('.Test')
Directory.Build.propsDirectory.Build.props<PropertyGroup>
<EnableMSTestRunner>true</EnableMSTestRunner>
<OutputType>Exe</OutputType>
</PropertyGroup>MSTest.SdkEnableMSTestRunnerOutputType Exe<UseVSTest>true</UseVSTest><PropertyGroup>
<EnableMSTestRunner>true</EnableMSTestRunner>
<OutputType>Exe</OutputType>
</PropertyGroup>MSTest.SdkEnableMSTestRunnerOutputType Exe<UseVSTest>true</UseVSTest>NUnit3TestAdapterNUnit3TestAdapter<PackageReference Include="NUnit3TestAdapter" Version="5.0.0" /><PropertyGroup>
<EnableNUnitRunner>true</EnableNUnitRunner>
<OutputType>Exe</OutputType>
</PropertyGroup>NUnit3TestAdapterNUnit3TestAdapter<PackageReference Include="NUnit3TestAdapter" Version="5.0.0" /><PropertyGroup>
<EnableNUnitRunner>true</EnableNUnitRunner>
<OutputType>Exe</OutputType>
</PropertyGroup>YTest.MTP.XUnit2OutputTypeExe<PackageReference Include="YTest.MTP.XUnit2" Version="0.4.0" /><PropertyGroup>
<OutputType>Exe</OutputType>
</PropertyGroup>Note:preserves the VSTestYTest.MTP.XUnit2syntax, so no filter migration is needed for xUnit.net v2. It also supports--filterfor runsettings (xunit-specific configurations only),--settings, TRX reporting viaxunit.runner.json, and--report-trx.--treenode-filter
YTest.MTP.XUnit2OutputTypeExe<PackageReference Include="YTest.MTP.XUnit2" Version="0.4.0" /><PropertyGroup>
<OutputType>Exe</OutputType>
</PropertyGroup>注意:保留了VSTest的YTest.MTP.XUnit2语法,因此xUnit.net v2无需进行过滤器迁移。它还支持--filter(仅针对xunit特定配置的runsettings)、--settings、通过xunit.runner.json生成TRX报告,以及--report-trx。--treenode-filter
xunit.v3<PropertyGroup>
<UseMicrosoftTestingPlatformRunner>true</UseMicrosoftTestingPlatformRunner>
</PropertyGroup>Important: xUnit.net v3 on MTP does NOT support the VSTestsyntax. You must translate filters to xUnit.net v3's native filter options (see Step 5).--filter
xunit.v3<PropertyGroup>
<UseMicrosoftTestingPlatformRunner>true</UseMicrosoftTestingPlatformRunner>
</PropertyGroup>重要提示:在MTP上运行的xUnit.net v3不支持VSTest的语法。必须将过滤器转换为xUnit.net v3的原生过滤选项(请参阅步骤5)。--filter
dotnet testdotnet testtestglobal.json{
"sdk": {
"version": "10.0.100"
},
"test": {
"runner": "Microsoft.Testing.Platform"
}
}dotnet testdotnet test --report-trxImportant:does not support trailing commas. Ensure the JSON is strictly valid.global.json
global.jsontest{
"sdk": {
"version": "10.0.100"
},
"test": {
"runner": "Microsoft.Testing.Platform"
}
}dotnet testdotnet test --report-trx重要提示:不支持尾随逗号。请确保JSON格式严格有效。global.json
dotnet testDirectory.Build.props<PropertyGroup>
<TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>
</PropertyGroup>Important: In this mode, you must useto separate--build arguments from MTP arguments. For example:dotnet test.dotnet test --no-build -- --list-tests
Directory.Build.propsdotnet test<PropertyGroup>
<TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>
</PropertyGroup>重要提示:在此模式下,必须使用分隔--的构建参数与MTP参数。例如:dotnet test。dotnet test --no-build -- --list-tests
-c-f--no-build--nologo-v| VSTest argument | MTP equivalent | Notes |
|---|---|---|
| Not applicable | MTP does not use external adapter discovery |
| Not applicable | |
| | Requires |
| | Requires CrashDump extension |
| | Requires |
| | Requires HangDump extension |
| | Requires HangDump extension |
| | Per-extension arguments |
| | |
| | Same syntax for MSTest, NUnit, and xUnit.net v2 (with |
| | Requires |
| | Same |
| | MSTest and NUnit still support |
| | Same |
| | Applicable only to MSTest and NUnit |
-c-f--no-build--nologo-v| VSTest参数 | MTP等效参数 | 说明 |
|---|---|---|
| 不适用 | MTP不使用外部适配器发现机制 |
| 不适用 | |
| | 需要 |
| | 需要CrashDump扩展包 |
| | 需要 |
| | 需要HangDump扩展包 |
| | 需要HangDump扩展包 |
| | 扩展包专属参数 |
| | |
| | MSTest、NUnit和xUnit.net v2(使用 |
| | 需要 |
| | 保持一致 |
| | MSTest和NUnit仍支持 |
| | 保持一致 |
| | 仅适用于MSTest和NUnit |
YTest.MTP.XUnit2--filter--filterYTest.MTP.XUnit2--filter--filter| Flag | Description |
|---|---|
| Run all tests in a given class. Supports wildcards ( |
| Exclude all tests in a given class |
| Run a specific test method |
| Exclude a specific test method |
| Run all tests in a namespace |
| Exclude all tests in a namespace |
| Run tests with a matching trait |
| Exclude tests with a matching trait |
--filter-class Foo Bar| 标志 | 描述 |
|---|---|
| 运行指定类中的所有测试。支持通配符( |
| 排除指定类中的所有测试 |
| 运行特定测试方法 |
| 排除特定测试方法 |
| 运行指定命名空间中的所有测试 |
| 排除指定命名空间中的所有测试 |
| 运行具有匹配特性的测试 |
| 排除具有匹配特性的测试 |
--filter-class Foo BarVSTest | xUnit.net v3 MTP equivalent | Notes |
|---|---|---|
| | Wildcards required for substring match |
| | Exact match on fully qualified method |
| | Wildcards for substring match |
| | Filter by trait name/value pair |
| Complex expressions | | Uses xUnit.net query filter language (see below) |
VSTest | xUnit.net v3 MTP等效语法 | 说明 |
|---|---|---|
| | 子字符串匹配需要使用通配符 |
| | 完全匹配全限定方法名 |
| | 子字符串匹配需要使用通配符 |
| | 按特性名称/值对过滤 |
| 复杂表达式 | | 使用xUnit.net查询过滤语言(请参阅下文) |
--filter-query/<assemblyFilter>/<namespaceFilter>/<classFilter>/<methodFilter>[traitName=traitValue]*--filter-query/<assemblyFilter>/<namespaceFilter>/<classFilter>/<methodFilter>[traitName=traitValue]*undefinedundefined
> **Note**: When combining `--filter-class` and `--filter-trait`, both conditions must match (AND behavior). For complex expressions, use `--filter-query` with the path-segment syntax. See the [xUnit.net query filter language docs](https://xunit.net/docs/query-filter-language) for full reference.
> **注意**:组合使用`--filter-class`和`--filter-trait`时,两个条件必须同时满足(逻辑与)。对于复杂表达式,请使用带路径段语法的`--filter-query`。完整参考请查看[xUnit.net查询过滤语言文档](https://xunit.net/docs/query-filter-language)。<!-- TRX report generation (replaces --logger trx) -->
<PackageReference Include="Microsoft.Testing.Extensions.TrxReport" Version="1.6.2" />
<!-- Crash dump collection (replaces --blame-crash) -->
<PackageReference Include="Microsoft.Testing.Extensions.CrashDump" Version="1.6.2" />
<!-- Hang dump collection (replaces --blame-hang) -->
<PackageReference Include="Microsoft.Testing.Extensions.HangDump" Version="1.6.2" />
<!-- Code coverage (replaces --collect "Code Coverage") -->
<PackageReference Include="Microsoft.Testing.Extensions.CodeCoverage" Version="17.13.0" /><!-- TRX报告生成(替代--logger trx) -->
<PackageReference Include="Microsoft.Testing.Extensions.TrxReport" Version="1.6.2" />
<!-- 崩溃转储收集(替代--blame-crash) -->
<PackageReference Include="Microsoft.Testing.Extensions.CrashDump" Version="1.6.2" />
<!-- 挂起转储收集(替代--blame-hang) -->
<PackageReference Include="Microsoft.Testing.Extensions.HangDump" Version="1.6.2" />
<!-- 代码覆盖率(替代--collect "Code Coverage") -->
<PackageReference Include="Microsoft.Testing.Extensions.CodeCoverage" Version="17.13.0" />VSTest@3DotNetCoreCLI@2undefinedVSTest@3DotNetCoreCLI@2undefined
**If already using DotNetCoreCLI@2**: Update arguments per Step 5 translations. Remember the `--` separator on .NET 9 and earlier:
```yaml
- task: DotNetCoreCLI@2
displayName: Run tests
inputs:
command: 'test'
arguments: '--no-build -- --report-trx --results-directory $(Agent.TempDirectory)'
**如果已在使用DotNetCoreCLI@2**:根据步骤5的转换规则更新参数。注意在.NET 9及更早版本中需要使用`--`分隔符:
```yaml
- task: DotNetCoreCLI@2
displayName: Run tests
inputs:
command: 'test'
arguments: '--no-build -- --report-trx --results-directory $(Agent.TempDirectory)'dotnet testdotnet testvstest.console.exedotnet testvstest.console.exedotnet test--ignore-exit-code 8Directory.Build.props<PropertyGroup>
<TestingPlatformCommandLineArguments>$(TestingPlatformCommandLineArguments) --ignore-exit-code 8</TestingPlatformCommandLineArguments>
</PropertyGroup>TESTINGPLATFORM_EXITCODE_IGNORE=8--ignore-exit-code 8Directory.Build.props<PropertyGroup>
<TestingPlatformCommandLineArguments>$(TestingPlatformCommandLineArguments) --ignore-exit-code 8</TestingPlatformCommandLineArguments>
</PropertyGroup>TESTINGPLATFORM_EXITCODE_IGNORE=8Microsoft.NET.Test.Sdkxunit.runner.visualstudioYTest.MTP.XUnit2NUnit3TestAdapterNote: If you need to maintain VSTest compatibility during a transition period, keep these packages.
Microsoft.NET.Test.Sdkxunit.runner.visualstudioYTest.MTP.XUnit2NUnit3TestAdapter注意:如果在过渡期间需要保持VSTest兼容性,请保留这些包。
dotnet builddotnet test./bin/Debug/net8.0/MyTests.exedotnet builddotnet test./bin/Debug/net8.0/MyTests.exedotnet builddotnet test./bin/Debug/net8.0/MyTests.exevstest.console.exe<OutputType>Exe</OutputType>dotnet builddotnet test./bin/Debug/net8.0/MyTests.exevstest.console.exe<OutputType>Exe</OutputType>| Pitfall | Solution |
|---|---|
| Mixing VSTest and MTP projects in the same solution | Migrate all test projects together -- mixed mode is unsupported |
| Use |
| Exit code 8 on CI without failures | MTP fails when zero tests run; use |
| MSTest.Sdk v4 + vstest.console no longer works | MSTest.Sdk v4 no longer adds |
Missing | Required for all setups except MSTest.Sdk (which sets it automatically) |
Using | |
| 陷阱 | 解决方案 |
|---|---|
| 同一解决方案中混合VSTest和MTP项目 | 同时迁移所有测试项目——混合模式不受支持 |
.NET 9及更早版本中 | 使用 |
| CI中无失败但返回退出代码8 | MTP在无测试运行时会失败;使用 |
| MSTest.Sdk v4 + vstest.console无法正常工作 | MSTest.Sdk v4不再添加 |
缺少 | 除MSTest.Sdk(会自动设置)外,所有配置均需设置此属性 |
在 | |
run-testsmtp-hot-reloadrun-testsmtp-hot-reload