run-tests

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Run .NET Tests

运行.NET测试

Detect the test platform and framework, run tests, and apply filters using
dotnet test
.
使用
dotnet test
检测测试平台与框架、运行测试并应用筛选条件。

When 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

输入项

InputRequiredDescription
Project or solution pathNoPath to the test project (.csproj) or solution (.sln). Defaults to current directory.
Filter expressionNoFilter expression to select specific tests
Target frameworkNoTarget framework moniker to run against (e.g.,
net8.0
)
输入项是否必填描述
项目或解决方案路径测试项目(.csproj)或解决方案(.sln)的路径。默认使用当前目录。
筛选表达式用于选择特定测试的筛选表达式
目标框架要运行测试的目标框架标识符(例如:
net8.0

Workflow

工作流程

Quick Reference

快速参考

PlatformSDKCommand pattern
VSTestAny
dotnet test [<path>] [--filter <expr>] [--logger trx]
MTP8 or 9
dotnet test [<path>] -- <MTP_ARGS>
MTP10+
dotnet test --project <path> <MTP_ARGS>
Detection files to always check (in order):
global.json
.csproj
Directory.Build.props
Directory.Packages.props
平台SDK版本命令格式
VSTest任意版本
dotnet test [<path>] [--filter <expr>] [--logger trx]
MTP8或9
dotnet test [<path>] -- <MTP_ARGS>
MTP10+
dotnet test --project <path> <MTP_ARGS>
必须检查的检测文件(按顺序):
global.json
.csproj
Directory.Build.props
Directory.Packages.props

Step 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
.csproj
file and
Directory.Build.props
/
Directory.Packages.props
(for centrally managed dependencies) and look for:
Package or SDK referenceFramework
MSTest
(metapackage, recommended) or
<Sdk Name="MSTest.Sdk">
MSTest
MSTest.TestFramework
+
MSTest.TestAdapter
MSTest (also valid for v3/v4)
xunit
,
xunit.v3
,
xunit.v3.mtp-v1
,
xunit.v3.mtp-v2
,
xunit.v3.core.mtp-v1
,
xunit.v3.core.mtp-v2
xUnit
NUnit
+
NUnit3TestAdapter
NUnit
TUnit
TUnit (MTP only)
读取
.csproj
文件以及
Directory.Build.props
/
Directory.Packages.props
(用于集中管理依赖项),并查找以下内容:
包或SDK引用对应框架
MSTest
(元包,推荐)或
<Sdk Name="MSTest.Sdk">
MSTest
MSTest.TestFramework
+
MSTest.TestAdapter
MSTest(同样适用于v3/v4版本)
xunit
,
xunit.v3
,
xunit.v3.mtp-v1
,
xunit.v3.mtp-v2
,
xunit.v3.core.mtp-v1
,
xunit.v3.core.mtp-v2
xUnit
NUnit
+
NUnit3TestAdapter
NUnit
TUnit
TUnit(仅支持MTP)

Detecting the test platform

检测测试平台

The detection logic depends on the .NET SDK version. Run
dotnet --version
to determine it.
检测逻辑取决于.NET SDK版本。运行
dotnet --version
确定版本。
.NET SDK 10+
.NET SDK 10+
On .NET 10+, the
global.json
test.runner
setting is the authoritative source:
  • If
    global.json
    contains
    "test": { "runner": "Microsoft.Testing.Platform" }
    MTP
  • If
    global.json
    has
    "runner": "VSTest"
    , or no
    test
    section exists → VSTest
Important: On .NET 10+,
<TestingPlatformDotnetTestSupport>
alone does not switch to MTP. The
global.json
runner setting takes precedence. If the runner is VSTest (or unset), the project uses VSTest regardless of
TestingPlatformDotnetTestSupport
.
在.NET 10+中,
global.json
test.runner
设置是权威依据
  • global.json
    包含
    "test": { "runner": "Microsoft.Testing.Platform" }
    MTP
  • global.json
    包含
    "runner": "VSTest"
    ,或无
    test
    配置段 → VSTest
重要提示:在.NET 10+中,仅设置
<TestingPlatformDotnetTestSupport>
不会切换到MTP。
global.json
中的runner设置优先级更高。若runner为VSTest(或未设置),无论
TestingPlatformDotnetTestSupport
如何配置,项目均使用VSTest。
.NET SDK 8 or 9
.NET SDK 8或9
On older SDKs, check these signals in priority order:
1. Check the
<TestingPlatformDotnetTestSupport>
MSBuild property.
Look in the
.csproj
,
Directory.Build.props
, and
Directory.Packages.props
. If set to
true
in any of these files, the project uses MTP.
Critical: Always read
Directory.Build.props
and
Directory.Packages.props
if they exist. MTP properties are frequently set there instead of in the
.csproj
, so checking only the project file will miss them.
2. Check project-level signals:
SignalPlatform
<Sdk Name="MSTest.Sdk">
as project SDK
MTP by default
<UseMicrosoftTestingPlatformRunner>true</UseMicrosoftTestingPlatformRunner>
MTP runner (xUnit)
<EnableMSTestRunner>true</EnableMSTestRunner>
MTP runner (MSTest)
<EnableNUnitRunner>true</EnableNUnitRunner>
MTP runner (NUnit)
Microsoft.Testing.Platform
package referenced directly
MTP
TUnit
package referenced
MTP (TUnit is MTP-only)
Note: The presence of
Microsoft.NET.Test.Sdk
does 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.
Key distinction: VSTest is the classic platform that uses
vstest.console
under the hood. Microsoft.Testing.Platform (MTP) is the newer, faster platform. Both can be invoked via
dotnet test
, but their filter syntax and CLI options differ.
在旧版SDK中,按以下优先级检查信号:
1. 检查
<TestingPlatformDotnetTestSupport>
MSBuild属性
。查看
.csproj
Directory.Build.props
以及
Directory.Packages.props
。若任意文件中设置为
true
,则项目使用MTP
关键提示:若存在
Directory.Build.props
Directory.Packages.props
,务必检查这些文件。MTP属性通常在这些文件中设置,而非
.csproj
,仅检查项目文件会遗漏配置。
2. 检查项目级信号
信号对应平台
<Sdk Name="MSTest.Sdk">
作为项目SDK
默认使用MTP
<UseMicrosoftTestingPlatformRunner>true</UseMicrosoftTestingPlatformRunner>
MTP运行器(xUnit)
<EnableMSTestRunner>true</EnableMSTestRunner>
MTP运行器(MSTest)
<EnableNUnitRunner>true</EnableNUnitRunner>
MTP运行器(NUnit)
直接引用
Microsoft.Testing.Platform
MTP
引用
TUnit
MTP(TUnit仅支持MTP)
注意:存在
Microsoft.NET.Test.Sdk
并不一定意味着使用VSTest。部分框架(如MSTest)为了兼容性会间接引入该包,即使已启用MTP。请勿仅以此包作为判断依据——务必优先检查上述MTP信号。
核心区别:VSTest是传统平台,底层使用
vstest.console
;Microsoft.Testing.Platform(MTP)是更新、更快的平台。两者均可通过
dotnet test
调用,但筛选语法和CLI选项不同。

Step 2: Run tests

步骤2:运行测试

VSTest (any .NET SDK version)

VSTest(任意.NET SDK版本)

bash
dotnet test [<PROJECT> | <SOLUTION> | <DIRECTORY> | <DLL> | <EXE>]
Common flags:
FlagDescription
--framework <TFM>
Target a specific framework in multi-TFM projects (e.g.,
net8.0
)
--no-build
Skip build, use previously built output
--filter <EXPRESSION>
Run selected tests (see Step 3)
--logger trx
Generate TRX results file
--collect "Code Coverage"
Collect code coverage using Microsoft Code Coverage (built-in, always available)
--blame
Enable blame mode to detect tests that crash the host
--blame-crash
Collect a crash dump when the test host crashes
--blame-hang-timeout <duration>
Abort test if it hangs longer than duration (e.g.,
5min
)
-v <level>
Verbosity:
quiet
,
minimal
,
normal
,
detailed
,
diagnostic
bash
dotnet test [<PROJECT> | <SOLUTION> | <DIRECTORY> | <DLL> | <EXE>]
常用参数:
参数描述
--framework <TFM>
在多目标框架项目中指定特定框架(例如:
net8.0
--no-build
跳过构建,使用之前构建的输出
--filter <EXPRESSION>
运行选定的测试(见步骤3
--logger trx
生成TRX结果文件
--collect "Code Coverage"
使用Microsoft Code Coverage(内置,始终可用)收集代码覆盖率
--blame
启用追责模式,检测导致宿主崩溃的测试
--blame-crash
当测试宿主崩溃时收集崩溃转储
--blame-hang-timeout <duration>
若测试超时未响应则终止(例如:
5min
-v <level>
日志详细程度:
quiet
minimal
normal
detailed
diagnostic

MTP with .NET SDK 8 or 9

.NET SDK 8或9下的MTP

With
<TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>
,
dotnet test
bridges to MTP but uses VSTest-style argument parsing. MTP-specific arguments must be passed after
--
:
bash
dotnet test [<PROJECT> | <SOLUTION> | <DIRECTORY> | <DLL> | <EXE>] -- <MTP_ARGUMENTS>
当设置
<TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>
时,
dotnet test
会桥接到MTP,但使用VSTest风格的参数解析。MTP专属参数必须在
--
之后传递:
bash
dotnet test [<PROJECT> | <SOLUTION> | <DIRECTORY> | <DLL> | <EXE>] -- <MTP_ARGUMENTS>

MTP with .NET SDK 10+

.NET SDK 10+下的MTP

With the
global.json
runner set to
Microsoft.Testing.Platform
,
dotnet test
natively understands MTP arguments without
--
:
bash
dotnet test
    [--project <PROJECT_OR_DIRECTORY>]
    [--solution <SOLUTION_OR_DIRECTORY>]
    [--test-modules <EXPRESSION>]
    [<MTP_ARGUMENTS>]
Examples:
bash
undefined
global.json
中的runner设置为
Microsoft.Testing.Platform
时,
dotnet test
原生支持MTP参数,无需
--
分隔:
bash
dotnet test
    [--project <PROJECT_OR_DIRECTORY>]
    [--solution <SOLUTION_OR_DIRECTORY>]
    [--test-modules <EXPRESSION>]
    [<MTP_ARGUMENTS>]
示例:
bash
undefined

Run 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):
FlagDescription
--no-build
Skip build, use previously built output
--framework <TFM>
Target a specific framework in multi-TFM projects
--diagnostic
Enable diagnostic logging for the test platform
--diagnostic-output-directory <DIR>
Directory for diagnostic log output
Extension-dependent flags (require the corresponding extension package to be registered):
FlagRequiresDescription
--filter <EXPRESSION>
Framework-specific (not all frameworks support this)Run selected tests (see Step 3)
--report-trx
Microsoft.Testing.Extensions.TrxReport
Generate TRX results file
--report-trx-filename <FILE>
Microsoft.Testing.Extensions.TrxReport
Set TRX output filename
--blame-hang-timeout <duration>
Microsoft.Testing.Extensions.HangDump
Abort test if it hangs longer than duration (e.g.,
5min
)
--blame-crash
Microsoft.Testing.Extensions.CrashDump
Collect a crash dump when the test host crashes
--coverage
Microsoft.Testing.Extensions.CodeCoverage
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+中,可直接传递。
内置参数(始终可用):
参数描述
--no-build
跳过构建,使用之前构建的输出
--framework <TFM>
在多目标框架项目中指定特定框架
--diagnostic
启用测试平台的诊断日志
--diagnostic-output-directory <DIR>
诊断日志输出目录
依赖扩展的参数(需注册对应扩展包):
参数依赖包描述
--filter <EXPRESSION>
框架专属(并非所有框架都支持)运行选定的测试(见步骤3
--report-trx
Microsoft.Testing.Extensions.TrxReport
生成TRX结果文件
--report-trx-filename <FILE>
Microsoft.Testing.Extensions.TrxReport
设置TRX输出文件名
--blame-hang-timeout <duration>
Microsoft.Testing.Extensions.HangDump
若测试超时未响应则终止(例如:
5min
--blame-crash
Microsoft.Testing.Extensions.CrashDump
当测试宿主崩溃时收集崩溃转储
--coverage
Microsoft.Testing.Extensions.CodeCoverage
使用Microsoft Code Coverage收集代码覆盖率
部分框架(如MSTest)默认捆绑常用扩展。其他框架可能需要显式引用扩展包。若参数未被识别,请检查项目是否引用了对应扩展包。

Alternative MTP invocations

MTP的其他调用方式

MTP test projects are standalone executables. Beyond
dotnet test
, they can be run directly:
bash
undefined
MTP测试项目是独立可执行文件。除
dotnet test
外,还可直接运行:
bash
undefined

Build 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:
OperatorMeaning
=
Exact match
!=
Not exact match
~
Contains
!~
Does not contain
Combinators:
|
(OR),
&
(AND). Parentheses for grouping:
(A|B)&C
Supported properties by framework:
FrameworkProperties
MSTest
FullyQualifiedName
,
Name
,
ClassName
,
Priority
,
TestCategory
xUnit
FullyQualifiedName
,
DisplayName
,
Traits
NUnit
FullyQualifiedName
,
Name
,
Priority
,
TestCategory
An expression without an operator is treated as
FullyQualifiedName~<value>
.
Examples (VSTest):
bash
undefined
bash
dotnet test --filter <EXPRESSION>
表达式语法:
<Property><Operator><Value>[|&<Expression>]
运算符:
运算符含义
=
精确匹配
!=
不精确匹配
~
包含
!~
不包含
组合符:
|
(或)、
&
(与)。可使用括号分组:
(A|B)&C
各框架支持的属性:
框架支持属性
MSTest
FullyQualifiedName
Name
ClassName
Priority
TestCategory
xUnit
FullyQualifiedName
DisplayName
Traits
NUnit
FullyQualifiedName
Name
Priority
TestCategory
无运算符的表达式会被解析为
FullyQualifiedName~<value>
示例(VSTest):
bash
undefined

Run 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"
undefined
dotnet test --filter "ClassName=MyNamespace.ClassA|ClassName=MyNamespace.ClassB"
undefined

MTP filters — MSTest and NUnit

MTP筛选 — MSTest和NUnit

MSTest and NUnit on MTP use the same
--filter
syntax
as VSTest (same properties, operators, and combinators). The only difference is how the flag is passed:
bash
undefined
MTP上的MSTest和NUnit使用与VSTest相同的
--filter
语法
(相同属性、运算符和组合符)。唯一区别是参数传递方式:
bash
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"
undefined
dotnet test --filter "Name~LoginTest"
undefined

MTP filters — xUnit (v3)

MTP筛选 — xUnit(v3)

xUnit v3 on MTP uses framework-specific filter flags instead of the generic
--filter
expression:
FlagDescription
--filter-class "name"
Run all tests in a given class
--filter-not-class "name"
Exclude all tests in a given class
--filter-method "name"
Run a specific test method
--filter-not-method "name"
Exclude a specific test method
--filter-namespace "name"
Run all tests in a namespace
--filter-not-namespace "name"
Exclude all tests in a namespace
--filter-trait "name=value"
Run tests with a matching trait
--filter-not-trait "name=value"
Exclude tests with a matching trait
Multiple values can be specified with a single flag:
--filter-class Foo Bar
.
bash
undefined
MTP上的xUnit v3使用框架专属筛选参数,而非通用的
--filter
表达式:
参数描述
--filter-class "name"
运行指定类中的所有测试
--filter-not-class "name"
排除指定类中的所有测试
--filter-method "name"
运行特定测试方法
--filter-not-method "name"
排除特定测试方法
--filter-namespace "name"
运行指定命名空间中的所有测试
--filter-not-namespace "name"
排除指定命名空间中的所有测试
--filter-trait "name=value"
运行匹配特征的测试
--filter-not-trait "name=value"
排除匹配特征的测试
单个参数可指定多个值:
--filter-class Foo Bar
bash
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"
undefined
dotnet test --filter-namespace "MyApp.Tests.Integration" --filter-trait "Category=Smoke"
undefined

MTP filters — TUnit

MTP筛选 — TUnit

TUnit uses
--treenode-filter
with a path-based syntax:
--treenode-filter "/<Assembly>/<Namespace>/<ClassName>/<TestName>"
Wildcards (
*
) are supported in any segment. Filter operators can be appended to test names for property-based filtering.
OperatorMeaning
*
Wildcard match
=
Exact property match (e.g.,
[Category=Unit]
)
!=
Exclude property value
&
AND (combine conditions)
|
OR (within a segment, requires parentheses)
Examples (TUnit):
bash
undefined
TUnit使用
--treenode-filter
和基于路径的语法:
--treenode-filter "/<Assembly>/<Namespace>/<ClassName>/<TestName>"
任意段均支持通配符
*
。可在测试名称后附加筛选运算符,实现基于属性的筛选。
运算符含义
*
通配符匹配
=
属性精确匹配(例如:
[Category=Unit]
!=
排除指定属性值
&
与(组合条件)
|
或(在同一段中使用,需加括号)
示例(TUnit):
bash
undefined

All 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]"
undefined
dotnet run --treenode-filter "//MyProject.Tests.Integration///[Priority=Critical]"
undefined

Validation

验证项

  • Test platform (VSTest or MTP) was correctly identified
  • Test framework (MSTest, xUnit, NUnit, TUnit) was correctly identified
  • Correct
    dotnet test
    invocation was used for the detected platform and SDK version
  • 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

常见陷阱

PitfallSolution
Missing
Microsoft.NET.Test.Sdk
in a VSTest project
Tests won't be discovered. Add
<PackageReference Include="Microsoft.NET.Test.Sdk" />
Using VSTest
--filter
syntax with xUnit v3 on MTP
xUnit v3 on MTP uses
--filter-class
,
--filter-method
, etc. — not the VSTest expression syntax
Passing MTP args without
--
on .NET SDK 8/9
Before .NET 10, MTP args must go after
--
:
dotnet test -- --report-trx
Using
--
for MTP args on .NET SDK 10+
On .NET 10+, MTP args are passed directly:
dotnet test --report-trx
(using
--
still works but is unnecessary)
Multi-TFM project runs tests for all frameworksUse
--framework <TFM>
to target a specific framework
global.json
runner setting ignored
Requires .NET 10+ SDK. On older SDKs, use
<TestingPlatformDotnetTestSupport>
MSBuild property instead
TUnit
--treenode-filter
not recognized
TUnit is MTP-only and requires
dotnet run
, not
dotnet test
with VSTest
陷阱解决方案
VSTest项目中缺少
Microsoft.NET.Test.Sdk
测试无法被发现。添加
<PackageReference Include="Microsoft.NET.Test.Sdk" />
在MTP上的xUnit v3中使用VSTest的
--filter
语法
MTP上的xUnit v3使用
--filter-class
--filter-method
等参数,而非VSTest的表达式语法
在.NET SDK 8/9中未通过
--
传递MTP参数
.NET 10之前,MTP参数必须在
--
之后传递:
dotnet test -- --report-trx
在.NET SDK 10+中使用
--
传递MTP参数
.NET 10+中,MTP参数可直接传递:
dotnet test --report-trx
(使用
--
仍有效,但无必要)
多目标框架项目运行所有框架的测试使用
--framework <TFM>
指定特定框架
global.json
中的runner设置未生效
需要.NET 10+ SDK。在旧版SDK中,使用
<TestingPlatformDotnetTestSupport>
MSBuild属性替代
TUnit的
--treenode-filter
未被识别
TUnit仅支持MTP,需使用
dotnet run
,而非VSTest的
dotnet test