run-tests
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseRun .NET Tests
运行.NET测试
Detect the test platform and framework, run tests, and apply filters using .
dotnet test使用检测测试平台与框架、运行测试并应用筛选条件。
dotnet testWhen to Use
使用场景
- User wants to run tests in a .NET project
- User needs to run a subset of tests using filters
- User needs help detecting which test platform (VSTest vs MTP) or framework is in use
- User wants to understand the correct filter syntax for their setup
- 用户需要在.NET项目中运行测试
- 用户需要使用筛选条件运行部分测试
- 用户需要帮助检测当前使用的测试平台(VSTest vs MTP)或框架
- 用户希望了解适用于当前环境的正确筛选语法
When Not to Use
非适用场景
- User needs to write or generate test code (use general coding assistance)
- User needs CI/CD pipeline configuration (use CI-specific skills)
- User needs to debug a test (use debugging skills)
- 用户需要编写或生成测试代码(请使用通用代码协助能力)
- 用户需要配置CI/CD流水线(请使用CI专属能力)
- 用户需要调试测试(请使用调试能力)
Inputs
输入项
| Input | Required | Description |
|---|---|---|
| Project or solution path | No | Path to the test project (.csproj) or solution (.sln). Defaults to current directory. |
| Filter expression | No | Filter expression to select specific tests |
| Target framework | No | Target framework moniker to run against (e.g., |
| 输入项 | 是否必填 | 描述 |
|---|---|---|
| 项目或解决方案路径 | 否 | 测试项目(.csproj)或解决方案(.sln)的路径。默认使用当前目录。 |
| 筛选表达式 | 否 | 用于选择特定测试的筛选表达式 |
| 目标框架 | 否 | 要运行测试的目标框架标识符(例如: |
Workflow
工作流程
Quick Reference
快速参考
| Platform | SDK | Command pattern |
|---|---|---|
| VSTest | Any | |
| MTP | 8 or 9 | |
| MTP | 10+ | |
Detection files to always check (in order): → → →
global.json.csprojDirectory.Build.propsDirectory.Packages.props| 平台 | SDK版本 | 命令格式 |
|---|---|---|
| VSTest | 任意版本 | |
| MTP | 8或9 | |
| MTP | 10+ | |
必须检查的检测文件(按顺序): → → →
global.json.csprojDirectory.Build.propsDirectory.Packages.propsStep 1: Detect the test platform and framework
步骤1:检测测试平台与框架
Determine which test platform (VSTest or Microsoft.Testing.Platform) and which test framework (MSTest, xUnit, NUnit, TUnit) the project uses.
确定项目使用的测试平台(VSTest或Microsoft.Testing.Platform)和测试框架(MSTest、xUnit、NUnit、TUnit)。
Detecting the test framework
检测测试框架
Read the file and / (for centrally managed dependencies) and look for:
.csprojDirectory.Build.propsDirectory.Packages.props| Package or SDK reference | Framework |
|---|---|
| MSTest |
| MSTest (also valid for v3/v4) |
| xUnit |
| NUnit |
| TUnit (MTP only) |
读取文件以及/(用于集中管理依赖项),并查找以下内容:
.csprojDirectory.Build.propsDirectory.Packages.props| 包或SDK引用 | 对应框架 |
|---|---|
| MSTest |
| MSTest(同样适用于v3/v4版本) |
| xUnit |
| NUnit |
| TUnit(仅支持MTP) |
Detecting the test platform
检测测试平台
The detection logic depends on the .NET SDK version. Run to determine it.
dotnet --version检测逻辑取决于.NET SDK版本。运行确定版本。
dotnet --version.NET SDK 10+
.NET SDK 10+
On .NET 10+, the setting is the authoritative source:
global.jsontest.runner- If contains
global.json→ MTP"test": { "runner": "Microsoft.Testing.Platform" } - If has
global.json, or no"runner": "VSTest"section exists → VSTesttest
Important: On .NET 10+,alone does not switch to MTP. The<TestingPlatformDotnetTestSupport>runner setting takes precedence. If the runner is VSTest (or unset), the project uses VSTest regardless ofglobal.json.TestingPlatformDotnetTestSupport
在.NET 10+中,的设置是权威依据:
global.jsontest.runner- 若包含
global.json→ MTP"test": { "runner": "Microsoft.Testing.Platform" } - 若包含
global.json,或无"runner": "VSTest"配置段 → VSTesttest
重要提示:在.NET 10+中,仅设置不会切换到MTP。<TestingPlatformDotnetTestSupport>中的runner设置优先级更高。若runner为VSTest(或未设置),无论global.json如何配置,项目均使用VSTest。TestingPlatformDotnetTestSupport
.NET SDK 8 or 9
.NET SDK 8或9
On older SDKs, check these signals in priority order:
1. Check the MSBuild property. Look in the , , and . If set to in any of these files, the project uses MTP.
<TestingPlatformDotnetTestSupport>.csprojDirectory.Build.propsDirectory.Packages.propstrueCritical: Always readandDirectory.Build.propsif they exist. MTP properties are frequently set there instead of in theDirectory.Packages.props, so checking only the project file will miss them..csproj
2. Check project-level signals:
| Signal | Platform |
|---|---|
| MTP by default |
| MTP runner (xUnit) |
| MTP runner (MSTest) |
| MTP runner (NUnit) |
| MTP |
| MTP (TUnit is MTP-only) |
Note: The presence ofdoes not necessarily mean VSTest. Some frameworks (e.g., MSTest) pull it in transitively for compatibility, even when MTP is enabled. Do not use this package as a signal on its own — always check the MTP signals above first.Microsoft.NET.Test.Sdk
Key distinction: VSTest is the classic platform that usesunder the hood. Microsoft.Testing.Platform (MTP) is the newer, faster platform. Both can be invoked viavstest.console, but their filter syntax and CLI options differ.dotnet test
在旧版SDK中,按以下优先级检查信号:
1. 检查MSBuild属性。查看、以及。若任意文件中设置为,则项目使用MTP。
<TestingPlatformDotnetTestSupport>.csprojDirectory.Build.propsDirectory.Packages.propstrue关键提示:若存在和Directory.Build.props,务必检查这些文件。MTP属性通常在这些文件中设置,而非Directory.Packages.props,仅检查项目文件会遗漏配置。.csproj
2. 检查项目级信号:
| 信号 | 对应平台 |
|---|---|
将 | 默认使用MTP |
| MTP运行器(xUnit) |
| MTP运行器(MSTest) |
| MTP运行器(NUnit) |
直接引用 | MTP |
引用 | MTP(TUnit仅支持MTP) |
注意:存在并不一定意味着使用VSTest。部分框架(如MSTest)为了兼容性会间接引入该包,即使已启用MTP。请勿仅以此包作为判断依据——务必优先检查上述MTP信号。Microsoft.NET.Test.Sdk
核心区别:VSTest是传统平台,底层使用;Microsoft.Testing.Platform(MTP)是更新、更快的平台。两者均可通过vstest.console调用,但筛选语法和CLI选项不同。dotnet test
Step 2: Run tests
步骤2:运行测试
VSTest (any .NET SDK version)
VSTest(任意.NET SDK版本)
bash
dotnet test [<PROJECT> | <SOLUTION> | <DIRECTORY> | <DLL> | <EXE>]Common flags:
| Flag | Description |
|---|---|
| Target a specific framework in multi-TFM projects (e.g., |
| Skip build, use previously built output |
| Run selected tests (see Step 3) |
| Generate TRX results file |
| Collect code coverage using Microsoft Code Coverage (built-in, always available) |
| Enable blame mode to detect tests that crash the host |
| Collect a crash dump when the test host crashes |
| Abort test if it hangs longer than duration (e.g., |
| Verbosity: |
bash
dotnet test [<PROJECT> | <SOLUTION> | <DIRECTORY> | <DLL> | <EXE>]常用参数:
| 参数 | 描述 |
|---|---|
| 在多目标框架项目中指定特定框架(例如: |
| 跳过构建,使用之前构建的输出 |
| 运行选定的测试(见步骤3) |
| 生成TRX结果文件 |
| 使用Microsoft Code Coverage(内置,始终可用)收集代码覆盖率 |
| 启用追责模式,检测导致宿主崩溃的测试 |
| 当测试宿主崩溃时收集崩溃转储 |
| 若测试超时未响应则终止(例如: |
| 日志详细程度: |
MTP with .NET SDK 8 or 9
.NET SDK 8或9下的MTP
With , bridges to MTP but uses VSTest-style argument parsing. MTP-specific arguments must be passed after :
<TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>dotnet test--bash
dotnet test [<PROJECT> | <SOLUTION> | <DIRECTORY> | <DLL> | <EXE>] -- <MTP_ARGUMENTS>当设置时,会桥接到MTP,但使用VSTest风格的参数解析。MTP专属参数必须在之后传递:
<TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>dotnet test--bash
dotnet test [<PROJECT> | <SOLUTION> | <DIRECTORY> | <DLL> | <EXE>] -- <MTP_ARGUMENTS>MTP with .NET SDK 10+
.NET SDK 10+下的MTP
With the runner set to , natively understands MTP arguments without :
global.jsonMicrosoft.Testing.Platformdotnet test--bash
dotnet test
[--project <PROJECT_OR_DIRECTORY>]
[--solution <SOLUTION_OR_DIRECTORY>]
[--test-modules <EXPRESSION>]
[<MTP_ARGUMENTS>]Examples:
bash
undefined当中的runner设置为时,原生支持MTP参数,无需分隔:
global.jsonMicrosoft.Testing.Platformdotnet test--bash
dotnet test
[--project <PROJECT_OR_DIRECTORY>]
[--solution <SOLUTION_OR_DIRECTORY>]
[--test-modules <EXPRESSION>]
[<MTP_ARGUMENTS>]示例:
bash
undefinedRun all tests in a project
运行项目中所有测试
dotnet test --project path/to/MyTests.csproj
dotnet test --project path/to/MyTests.csproj
Run all tests in a directory containing a project
运行包含项目的目录中所有测试
dotnet test --project path/to/
dotnet test --project path/to/
Run all tests in a solution (sln, slnf, slnx)
运行解决方案中所有测试(sln、slnf、slnx)
dotnet test --solution path/to/MySolution.sln
dotnet test --solution path/to/MySolution.sln
Run all tests in a directory containing a solution
运行包含解决方案的目录中所有测试
dotnet test --solution path/to/
dotnet test --solution path/to/
Run with MTP flags
使用MTP参数运行
dotnet test --project path/to/MyTests.csproj --report-trx --blame-hang-timeout 5min
> **Note**: The .NET 10+ `dotnet test` syntax does **not** accept a bare positional argument like the VSTest syntax. Use `--project`, `--solution`, or `--test-modules` to specify the target.dotnet test --project path/to/MyTests.csproj --report-trx --blame-hang-timeout 5min
> **注意**:.NET 10+的`dotnet test`语法**不接受**VSTest风格的裸位置参数。请使用`--project`、`--solution`或`--test-modules`指定目标。Common MTP flags
通用MTP参数
These flags apply to MTP on both SDK versions. On SDK 8/9, pass after ; on SDK 10+, pass directly.
--Built-in flags (always available):
| Flag | Description |
|---|---|
| Skip build, use previously built output |
| Target a specific framework in multi-TFM projects |
| Enable diagnostic logging for the test platform |
| Directory for diagnostic log output |
Extension-dependent flags (require the corresponding extension package to be registered):
| Flag | Requires | Description |
|---|---|---|
| Framework-specific (not all frameworks support this) | Run selected tests (see Step 3) |
| | Generate TRX results file |
| | Set TRX output filename |
| | Abort test if it hangs longer than duration (e.g., |
| | Collect a crash dump when the test host crashes |
| | Collect code coverage using Microsoft Code Coverage |
Some frameworks (e.g., MSTest) bundle common extensions by default. Others may require explicit package references. If a flag is not recognized, check that the corresponding extension package is referenced in the project.
以下参数适用于所有SDK版本的MTP。在SDK 8/9中,需在之后传递;在SDK 10+中,可直接传递。
--内置参数(始终可用):
| 参数 | 描述 |
|---|---|
| 跳过构建,使用之前构建的输出 |
| 在多目标框架项目中指定特定框架 |
| 启用测试平台的诊断日志 |
| 诊断日志输出目录 |
依赖扩展的参数(需注册对应扩展包):
| 参数 | 依赖包 | 描述 |
|---|---|---|
| 框架专属(并非所有框架都支持) | 运行选定的测试(见步骤3) |
| | 生成TRX结果文件 |
| | 设置TRX输出文件名 |
| | 若测试超时未响应则终止(例如: |
| | 当测试宿主崩溃时收集崩溃转储 |
| | 使用Microsoft Code Coverage收集代码覆盖率 |
部分框架(如MSTest)默认捆绑常用扩展。其他框架可能需要显式引用扩展包。若参数未被识别,请检查项目是否引用了对应扩展包。
Alternative MTP invocations
MTP的其他调用方式
MTP test projects are standalone executables. Beyond , they can be run directly:
dotnet testbash
undefinedMTP测试项目是独立可执行文件。除外,还可直接运行:
dotnet testbash
undefinedBuild and run
构建并运行
dotnet run --project <PROJECT_PATH>
dotnet run --project <PROJECT_PATH>
Run a previously built DLL
运行已构建的DLL
dotnet exec <PATH_TO_DLL>
dotnet exec <PATH_TO_DLL>
Run the executable directly (Windows)
直接运行可执行文件(Windows)
<PATH_TO_EXE>
These alternative invocations accept MTP command line arguments directly (no `--` separator needed).<PATH_TO_EXE>
这些调用方式可直接接受MTP命令行参数(无需`--`分隔符)。Step 3: Run filtered tests
步骤3:运行筛选后的测试
The filter syntax depends on the platform and test framework.
筛选语法取决于平台和测试框架。
VSTest filters (MSTest, xUnit, NUnit on VSTest)
VSTest筛选(VSTest上的MSTest、xUnit、NUnit)
bash
dotnet test --filter <EXPRESSION>Expression syntax:
<Property><Operator><Value>[|&<Expression>]Operators:
| Operator | Meaning |
|---|---|
| Exact match |
| Not exact match |
| Contains |
| Does not contain |
Combinators: (OR), (AND). Parentheses for grouping:
|&(A|B)&CSupported properties by framework:
| Framework | Properties |
|---|---|
| MSTest | |
| xUnit | |
| NUnit | |
An expression without an operator is treated as .
FullyQualifiedName~<value>Examples (VSTest):
bash
undefinedbash
dotnet test --filter <EXPRESSION>表达式语法:
<Property><Operator><Value>[|&<Expression>]运算符:
| 运算符 | 含义 |
|---|---|
| 精确匹配 |
| 不精确匹配 |
| 包含 |
| 不包含 |
组合符: (或)、(与)。可使用括号分组:
|&(A|B)&C各框架支持的属性:
| 框架 | 支持属性 |
|---|---|
| MSTest | |
| xUnit | |
| NUnit | |
无运算符的表达式会被解析为。
FullyQualifiedName~<value>示例(VSTest):
bash
undefinedRun tests whose name contains "LoginTest"
运行名称包含"LoginTest"的测试
dotnet test --filter "Name~LoginTest"
dotnet test --filter "Name~LoginTest"
Run a specific test class
运行特定测试类
dotnet test --filter "ClassName=MyNamespace.MyTestClass"
dotnet test --filter "ClassName=MyNamespace.MyTestClass"
Run tests in a category
运行指定分类的测试
dotnet test --filter "TestCategory=Integration"
dotnet test --filter "TestCategory=Integration"
Exclude a category
排除指定分类的测试
dotnet test --filter "TestCategory!=Slow"
dotnet test --filter "TestCategory!=Slow"
Combine: class AND category
组合条件:指定类且指定分类
dotnet test --filter "ClassName=MyNamespace.MyTestClass&TestCategory=Unit"
dotnet test --filter "ClassName=MyNamespace.MyTestClass&TestCategory=Unit"
Either of two classes
运行两个类中的任意一个
dotnet test --filter "ClassName=MyNamespace.ClassA|ClassName=MyNamespace.ClassB"
undefineddotnet test --filter "ClassName=MyNamespace.ClassA|ClassName=MyNamespace.ClassB"
undefinedMTP filters — MSTest and NUnit
MTP筛选 — MSTest和NUnit
MSTest and NUnit on MTP use the same syntax as VSTest (same properties, operators, and combinators). The only difference is how the flag is passed:
--filterbash
undefinedMTP上的MSTest和NUnit使用与VSTest相同的语法(相同属性、运算符和组合符)。唯一区别是参数传递方式:
--filterbash
undefined.NET SDK 8/9 (after --)
.NET SDK 8/9(在--之后)
dotnet test -- --filter "Name~LoginTest"
dotnet test -- --filter "Name~LoginTest"
.NET SDK 10+ (direct)
.NET SDK 10+(直接传递)
dotnet test --filter "Name~LoginTest"
undefineddotnet test --filter "Name~LoginTest"
undefinedMTP filters — xUnit (v3)
MTP筛选 — xUnit(v3)
xUnit v3 on MTP uses framework-specific filter flags instead of the generic expression:
--filter| Flag | Description |
|---|---|
| Run all tests in a given class |
| 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 |
Multiple values can be specified with a single flag: .
--filter-class Foo Barbash
undefinedMTP上的xUnit v3使用框架专属筛选参数,而非通用的表达式:
--filter| 参数 | 描述 |
|---|---|
| 运行指定类中的所有测试 |
| 排除指定类中的所有测试 |
| 运行特定测试方法 |
| 排除特定测试方法 |
| 运行指定命名空间中的所有测试 |
| 排除指定命名空间中的所有测试 |
| 运行匹配特征的测试 |
| 排除匹配特征的测试 |
单个参数可指定多个值:。
--filter-class Foo Barbash
undefined.NET SDK 8/9
.NET SDK 8/9
dotnet test -- --filter-class "MyNamespace.LoginTests"
dotnet test -- --filter-class "MyNamespace.LoginTests"
.NET SDK 10+
.NET SDK 10+
dotnet test --filter-class "MyNamespace.LoginTests"
dotnet test --filter-class "MyNamespace.LoginTests"
Combine: namespace + trait
组合条件:指定命名空间且指定特征
dotnet test --filter-namespace "MyApp.Tests.Integration" --filter-trait "Category=Smoke"
undefineddotnet test --filter-namespace "MyApp.Tests.Integration" --filter-trait "Category=Smoke"
undefinedMTP filters — TUnit
MTP筛选 — TUnit
TUnit uses with a path-based syntax:
--treenode-filter--treenode-filter "/<Assembly>/<Namespace>/<ClassName>/<TestName>"Wildcards () are supported in any segment. Filter operators can be appended to test names for property-based filtering.
*| Operator | Meaning |
|---|---|
| Wildcard match |
| Exact property match (e.g., |
| Exclude property value |
| AND (combine conditions) |
| OR (within a segment, requires parentheses) |
Examples (TUnit):
bash
undefinedTUnit使用和基于路径的语法:
--treenode-filter--treenode-filter "/<Assembly>/<Namespace>/<ClassName>/<TestName>"任意段均支持通配符。可在测试名称后附加筛选运算符,实现基于属性的筛选。
*| 运算符 | 含义 |
|---|---|
| 通配符匹配 |
| 属性精确匹配(例如: |
| 排除指定属性值 |
| 与(组合条件) |
| 或(在同一段中使用,需加括号) |
示例(TUnit):
bash
undefinedAll tests in a class
运行指定类中的所有测试
dotnet run --treenode-filter "///LoginTests/*"
dotnet run --treenode-filter "///LoginTests/*"
A specific test
运行特定测试
dotnet run --treenode-filter "///*/AcceptCookiesTest"
dotnet run --treenode-filter "///*/AcceptCookiesTest"
By namespace prefix (wildcard)
按命名空间前缀筛选(通配符)
dotnet run --treenode-filter "//MyProject.Tests.Api//"
dotnet run --treenode-filter "//MyProject.Tests.Api//"
By custom property
按自定义属性筛选
dotnet run --treenode-filter "////[Category=Smoke]"
dotnet run --treenode-filter "////[Category=Smoke]"
Exclude by property
按属性排除
dotnet run --treenode-filter "////[Category!=Slow]"
dotnet run --treenode-filter "////[Category!=Slow]"
OR across classes
或条件:多个类
dotnet run --treenode-filter "///(LoginTests)|(SignupTests)/*"
dotnet run --treenode-filter "///(LoginTests)|(SignupTests)/*"
Combined: namespace + property
组合条件:命名空间+属性
dotnet run --treenode-filter "//MyProject.Tests.Integration///[Priority=Critical]"
undefineddotnet run --treenode-filter "//MyProject.Tests.Integration///[Priority=Critical]"
undefinedValidation
验证项
- Test platform (VSTest or MTP) was correctly identified
- Test framework (MSTest, xUnit, NUnit, TUnit) was correctly identified
- Correct invocation was used for the detected platform and SDK version
dotnet test - Filter expressions used the syntax appropriate for the platform and framework
- Test results were clearly reported to the user
- 已正确识别测试平台(VSTest或MTP)
- 已正确识别测试框架(MSTest、xUnit、NUnit、TUnit)
- 针对检测到的平台和SDK版本使用了正确的调用方式
dotnet test - 筛选表达式使用了适用于当前平台和框架的语法
- 已向用户清晰报告测试结果
Common Pitfalls
常见陷阱
| Pitfall | Solution |
|---|---|
Missing | Tests won't be discovered. Add |
Using VSTest | xUnit v3 on MTP uses |
Passing MTP args without | Before .NET 10, MTP args must go after |
Using | On .NET 10+, MTP args are passed directly: |
| Multi-TFM project runs tests for all frameworks | Use |
| Requires .NET 10+ SDK. On older SDKs, use |
TUnit | TUnit is MTP-only and requires |
| 陷阱 | 解决方案 |
|---|---|
VSTest项目中缺少 | 测试无法被发现。添加 |
在MTP上的xUnit v3中使用VSTest的 | MTP上的xUnit v3使用 |
在.NET SDK 8/9中未通过 | .NET 10之前,MTP参数必须在 |
在.NET SDK 10+中使用 | .NET 10+中,MTP参数可直接传递: |
| 多目标框架项目运行所有框架的测试 | 使用 |
| 需要.NET 10+ SDK。在旧版SDK中,使用 |
TUnit的 | TUnit仅支持MTP,需使用 |