/commands/dotnet-cli/dotnet-test-filter
dotnet test --filter: run a single test or a category in .NET
- tool
- .NET CLI
- versions
- .NET 8, .NET 9, .NET 10
- os
- linux, macos, windows
dotnet test --filter "FullyQualifiedName~Adds" builds the test project and runs only the tests whose fully qualified name (Namespace.Class.Method) contains Adds. Filters are expressions of property operator value: = (equals), !=, ~ (contains) and !~, combined with | (or) and & (and). Use a category or trait (Category=Slow) to run a group of tests, and --list-tests to see the names you can filter on.
Run one test and one category
dotnet new xunit -o Calc.Tests > /dev/null
cat > Calc.Tests/UnitTest1.cs <<'EOF'
namespace Calc.Tests;
public class MathTests
{
[Fact]
public void Adds() => Assert.Equal(4, 2 + 2);
[Fact]
public void Subtracts() => Assert.Equal(1, 3 - 2);
[Fact]
[Trait("Category", "Slow")]
public void SlowCheck() => Assert.True(true);
}
EOF
cd Calc.Tests
dotnet build -v q --nologo > /dev/null
dotnet test --no-build --filter "FullyQualifiedName~Adds" | grep -E "^(Passed!|Failed!)" | sed -E 's/, Duration: .*//'
dotnet test --no-build --filter "Category=Slow" | grep -E "^(Passed!|Failed!)" | sed -E 's/, Duration: .*//'
dotnet test --no-build --list-tests | grep -E "^ Calc"Output
Passed! - Failed: 0, Passed: 1, Skipped: 0, Total: 1
Passed! - Failed: 0, Passed: 1, Skipped: 0, Total: 1
Calc.Tests.MathTests.Adds
Calc.Tests.MathTests.Subtracts
Calc.Tests.MathTests.SlowCheckThe first run executed only Adds, the second only the test with the Category trait set to Slow. --list-tests prints every test name without running anything. The summary line also contains the test DLL name and target framework; the sed call above only trims the duration.
Filter syntax
| Filter | Runs |
|---|---|
| FullyQualifiedName~Adds | Tests whose full name contains Adds |
| FullyQualifiedName=Calc.Tests.MathTests.Adds | Exactly one test method |
| FullyQualifiedName~Calc.Tests.MathTests | Every test in one class |
| FullyQualifiedName!~Integration | Everything except tests with Integration in the name |
| DisplayName~Adds | xUnit: by display name (MSTest and NUnit use Name) |
| Category=Slow | xUnit [Trait("Category","Slow")], NUnit [Category("Slow")] |
| TestCategory=Slow | MSTest [TestCategory("Slow")] |
| Category=Slow|Category=Db | Either category (or) |
| FullyQualifiedName~Math&Category!=Slow | Both conditions (and) |
| Adds | Shorthand: no property means FullyQualifiedName~Adds |
Pitfalls
- Quote the expression.
|,&and!are shell operators; without quotes, bash or PowerShell splits or changes the filter. In PowerShell use single quotes or escape!. - Special characters in names. Parentheses, commas and
=inside a value must be escaped with a backslash (\() per the filter syntax; generic or parameterised test names are easiest to match with~on a unique part. - Several projects. In a folder with more than one project or solution file,
dotnet teststops with MSB1011; pass the project or solution path. - Restore failures such as NU1101 Unable to find package happen before any test runs; they come from the package sources, not from the filter.
- Microsoft.Testing.Platform. Projects that opt into the newer test platform pass filters through the test framework's own options; the
--filtersyntax above is the VSTest one used by default.
How to confirm it worked
The summary line, for example Passed! - Failed: 0, Passed: 1, Skipped: 0, Total: 1, shows how many tests ran. Total equal to the number you expected confirms the filter; if dotnet test reports that no test matches the given testcase filter, compare it with the output of dotnet test --list-tests.
FAQ
How do I run one test method by its exact name?
Use the fully qualified name with the equals operator: dotnet test --filter "FullyQualifiedName=Calc.Tests.MathTests.Adds". The ~ operator means contains, so FullyQualifiedName~Adds also matches AddsMany.
Why does dotnet test --filter run no tests?
The expression matched nothing. Run dotnet test --list-tests to see the exact names, check the property name (FullyQualifiedName, Name, Category or Trait names depend on the framework), and quote the whole expression so the shell does not split it on | or &.
Does --filter work with NUnit and MSTest?
Yes. The syntax is the same, but the supported properties differ: MSTest uses TestCategory and Priority, NUnit uses Category and Priority, and xUnit uses traits such as Category defined with [Trait].