Filter Syntax

作者 dotnet0608d8924cd3MIT5.5K 個星標收錄於 2026年10月8日更新於 2026年10月8日儲存庫今天更新

Reference-only filter syntax for VSTest and MTP with MSTest, NUnit, xUnit v3, and TUnit. Load only after the platform/framework is known and a consumer needs to create or translate a filter. Do not load for unfiltered runs or platform detection. Used by run-tests and mtp-hot-reload; never invoke directly.

AI 產生的概覽

VSTest 與 MTP 測試篩選語法參考,涵蓋 MSTest、NUnit、xUnit v3 與 TUnit。

功能
此技能是一份參考文件,說明如何為 VSTest 與 Microsoft Testing Platform(MTP)撰寫測試篩選運算式。它依框架列出運算子、組合符與可用屬性,提供指令範例,並附上從 VSTest 篩選轉換為 xUnit v3 MTP 對等寫法的對照表。它只產出指引內容,不會產生可執行的成品。
適用情境
當測試平台與框架已確定,需要建立或轉換篩選運算式時使用,例如從 VSTest 遷移到 MTP 的過程。不適用於未套用篩選的測試執行或平台偵測。
執行需求
不需要指令碼或工具,僅為純說明性參考。使用前提是熟悉 dotnet test 與相關測試框架。

Test Filter Syntax Reference

Filter syntax depends on the platform and test framework.

VSTest filters (MSTest, xUnit v2, NUnit on VSTest)

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
MSTestFullyQualifiedName, Name, ClassName, Priority, TestCategory
xUnitFullyQualifiedName, DisplayName, Traits
NUnitFullyQualifiedName, Name, Priority, TestCategory

An expression without an operator is treated as FullyQualifiedName~<value>.

Examples (VSTest):

bash
# Run tests whose name contains "LoginTest"dotnet test --filter "Name~LoginTest"
# Run a specific test classdotnet test --filter "ClassName=MyNamespace.MyTestClass"
# Run tests in a categorydotnet test --filter "TestCategory=Integration"
# Exclude a categorydotnet test --filter "TestCategory!=Slow"
# Combine: class AND categorydotnet test --filter "ClassName=MyNamespace.MyTestClass&TestCategory=Unit"
# Either of two classesdotnet test --filter "ClassName=MyNamespace.ClassA|ClassName=MyNamespace.ClassB"

MTP filters — MSTest and 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
# .NET SDK 8/9 (after --)dotnet test -- --filter "Name~LoginTest"
# .NET SDK 10+ (direct)dotnet test --filter "Name~LoginTest"

MTP filters — 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
# .NET SDK 8/9dotnet test -- --filter-class "MyNamespace.LoginTests"
# .NET SDK 10+dotnet test --filter-class "MyNamespace.LoginTests"
# Combine: namespace + traitdotnet test --filter-namespace "MyApp.Tests.Integration" --filter-trait "Category=Smoke"

xUnit v3 query filter language

For complex expressions, use --filter-query with a path-segment syntax:

text
/<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
# xUnit.net v3 MTP — using query language (assembly/namespace/class/method[trait])dotnet test -- --filter-query "/*/*/*IntegrationTests*/*[Category=Smoke]"

MTP filters — TUnit

TUnit uses --treenode-filter with a path-based syntax:

text
--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
# All tests in a classdotnet run --treenode-filter "/*/*/LoginTests/*"
# A specific testdotnet run --treenode-filter "/*/*/*/AcceptCookiesTest"
# By namespace prefix (wildcard)dotnet run --treenode-filter "/*/MyProject.Tests.Api*/*/*"
# By custom propertydotnet run --treenode-filter "/*/*/*/*[Category=Smoke]"
# Exclude by propertydotnet run --treenode-filter "/*/*/*/*[Category!=Slow]"
# OR across classesdotnet run --treenode-filter "/*/*/(LoginTests)|(SignupTests)/*"
# Combined: namespace + propertydotnet run --treenode-filter "/*/MyProject.Tests.Integration/*/*/*[Priority=Critical]"

VSTest → MTP filter translation (for migration)

MSTest, NUnit, and xUnit.net v2 (with YTest.MTP.XUnit2): The VSTest --filter syntax is identical on both VSTest and MTP. No changes needed.

xUnit.net v3 (native MTP): xUnit.net v3 does NOT support the VSTest --filter syntax on MTP. Translate filters using xUnit.net v3's native options:

VSTest --filter syntaxxUnit.net v3 MTP equivalentNotes
FullyQualifiedName~ClassName--filter-class *ClassName*Wildcards required for substring match
FullyQualifiedName=Ns.Class.Method--filter-method Ns.Class.MethodExact match on fully qualified method
Name=MethodName--filter-method *MethodName*Wildcards for substring match
Category=Value (trait)--filter-trait "Category=Value"Filter by trait name/value pair
Complex expressions--filter-query "expr"Uses xUnit.net query filter language (see above)

來源與署名

來源:dotnet/skills位於plugins/dotnet-test/skills/filter-syntax提交0608d89

授權條款: MIT

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架