filter-syntax
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTest Filter Syntax Reference
测试过滤语法参考
Filter syntax depends on the platform and test framework.
过滤语法取决于使用的平台和测试框架。
VSTest filters (MSTest, xUnit v2, NUnit on VSTest)
VSTest过滤器(基于VSTest的MSTest、xUnit v2、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>表达式语法:
<属性><运算符><值>[|&<表达式>]运算符:
| 运算符 | 含义 |
|---|---|
| 精确匹配 |
| 非精确匹配 |
| 包含 |
| 不包含 |
组合符: (或)、(且),可使用括号分组:
|&(A|B)&C各框架支持的属性:
| 框架 | 支持属性 |
|---|---|
| MSTest | |
| xUnit | |
| NUnit | |
不带运算符的表达式会默认按照规则处理。
FullyQualifiedName~<值>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
undefined基于MTP运行的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
undefined基于MTP运行的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"
undefinedxUnit v3 query filter language
xUnit v3查询过滤语言
For complex expressions, use with a path-segment syntax:
--filter-query/<assemblyFilter>/<namespaceFilter>/<classFilter>/<methodFilter>[traitName=traitValue]Each segment matches against: assembly name, namespace, class name, method name. Use for "match all" in any segment. Documentation: https://xunit.net/docs/query-filter-language
*shell
undefined针对复杂过滤需求,可使用配合路径分段语法实现:
--filter-query/<程序集过滤器>/<命名空间过滤器>/<类过滤器>/<方法过滤器>[特征名=特征值]每个分段分别匹配:程序集名称、命名空间、类名、方法名,任意分段都可使用表示匹配所有。官方文档:https://xunit.net/docs/query-filter-language
*shell
undefinedxUnit.net v3 MTP — using query language (assembly/namespace/class/method[trait])
xUnit.net v3 MTP — 使用查询语言(程序集/命名空间/类/方法[特征])
dotnet test -- --filter-query "///IntegrationTests/*[Category=Smoke]"
undefineddotnet test -- --filter-query "///IntegrationTests/*[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 "/<程序集>/<命名空间>/<类名>/<测试名>"任意分段都支持通配符(),可在测试名后追加过滤运算符实现基于属性的过滤。
*| 运算符 | 含义 |
|---|---|
| 通配符匹配 |
| 属性精确匹配(例如 |
| 排除指定属性值 |
| 且(组合多个条件) |
| 或(仅在同一分段内使用,需要配合括号) |
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]"
undefinedVSTest → MTP filter translation (for migration)
VSTest→MTP过滤器转换(迁移用)
MSTest, NUnit, and xUnit.net v2 (with ): The VSTest syntax is identical on both VSTest and MTP. No changes needed.
YTest.MTP.XUnit2--filterxUnit.net v3 (native MTP): xUnit.net v3 does NOT support the VSTest syntax on MTP. Translate filters using xUnit.net v3's native options:
--filterVSTest | 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 above) |
MSTest、NUnit、xUnit.net v2(配合使用):VSTest的语法在VSTest和MTP上完全一致,无需修改。
YTest.MTP.XUnit2--filterxUnit.net v3(原生MTP):xUnit.net v3在MTP上不支持VSTest的语法,需要使用xUnit.net v3原生选项转换过滤规则:
--filterVSTest | xUnit.net v3 MTP 等效写法 | 说明 |
|---|---|---|
| | 子串匹配需要使用通配符 |
| | 完全限定方法名精确匹配 |
| | 子串匹配需要使用通配符 |
| | 按特征名/值对过滤 |
| 复杂表达式 | | 使用xUnit.net查询过滤语言(见上文) |