| name | dotnet-test |
| description | .NET test execution patterns and diagnostics. Use when running tests, analyzing test failures, or configuring test options. |
| allowed-tools | Read, Grep, Glob, Bash |
.NET Test Execution
Basic Test Commands
dotnet test
dotnet test tests/MyApp.Tests/MyApp.Tests.csproj
dotnet test --no-build
dotnet test --no-restore
Test Filtering
By Name
dotnet test --filter "FullyQualifiedName~OrderService"
dotnet test --filter "Name=CreateOrder_ValidInput_ReturnsOrder"
dotnet test --filter "DisplayName~Create Order"
By Category/Trait
dotnet test --filter "Category=Unit"
dotnet test --filter "Category!=Integration"
dotnet test --filter "Category=Unit&Priority=High"
dotnet test --filter "Category=Unit|Category=Integration"
By Class/Namespace
dotnet test --filter "ClassName=OrderServiceTests"
dotnet test --filter "FullyQualifiedName~MyApp.Tests.Services"
Complex Filters
dotnet test --filter "Category=Unit&Category!=Slow"
dotnet test --filter "FullyQualifiedName~Order&Category!=Integration"
Test Output
Verbosity Levels
dotnet test --verbosity quiet
dotnet test -v q
dotnet test --verbosity normal
dotnet test --verbosity detailed
dotnet test -v d
dotnet test --verbosity diagnostic
Logger Options
dotnet test --logger "console;verbosity=detailed"
dotnet test --logger trx
dotnet test --logger "junit;LogFileName=results.xml"
dotnet test --logger "html;LogFileName=results.html"
dotnet test --logger trx --logger "console;verbosity=detailed"
Results Directory
dotnet test --results-directory ./TestResults
Code Coverage
Collect Coverage
dotnet test --collect:"XPlat Code Coverage"
dotnet test /p:CollectCoverage=true
dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura
dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=\"opencover,cobertura\"
Coverage Thresholds
dotnet test /p:CollectCoverage=true /p:Threshold=80
dotnet test /p:CollectCoverage=true /p:ThresholdType=line /p:Threshold=80
Coverage Reports
dotnet tool install -g dotnet-reportgenerator-globaltool
reportgenerator -reports:coverage.cobertura.xml -targetdir:coveragereport
Parallel Execution
dotnet test --parallel
dotnet test -- RunConfiguration.MaxCpuCount=4
dotnet test -- RunConfiguration.DisableParallelization=true
Test Timeouts
dotnet test -- RunConfiguration.TestSessionTimeout=60000
[Fact(Timeout = 5000)]
public void SlowTest() { }
[Test, Timeout(5000)]
public void SlowTest() { }
Configuration Files
runsettings
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<RunConfiguration>
<MaxCpuCount>4</MaxCpuCount>
<ResultsDirectory>./TestResults</ResultsDirectory>
<TestSessionTimeout>600000</TestSessionTimeout>
</RunConfiguration>
<DataCollectionRunSettings>
<DataCollectors>
<DataCollector friendlyName="XPlat Code Coverage">
<Configuration>
<Format>cobertura</Format>
<Exclude>[*]*.Migrations.*</Exclude>
</Configuration>
</DataCollector>
</DataCollectors>
</DataCollectionRunSettings>
</RunSettings>
dotnet test --settings test.runsettings
Test Failure Analysis
Common Failure Patterns
| Pattern | Cause | Fix |
|---|
| Assert.Equal failed | Expected != Actual | Check logic, verify test data |
| NullReferenceException | Null not handled | Add null checks, verify setup |
| TimeoutException | Test too slow | Optimize or increase timeout |
| ObjectDisposedException | Using disposed object | Fix lifetime management |
| InvalidOperationException | Invalid state | Check test setup/order |
Debugging Failed Tests
dotnet test --filter "FullyQualifiedName~FailingTest" -v d
dotnet test --blame
dotnet test --blame-hang --blame-hang-timeout 60s
Watch Mode
dotnet watch test
dotnet watch --project tests/MyApp.Tests test
dotnet watch test --filter "Category=Unit"
CI/CD Integration
Exit Codes
| Code | Meaning |
|---|
| 0 | All tests passed |
| 1 | Tests failed |
| 2 | Command line error |
CI Examples
- task: DotNetCoreCLI@2
inputs:
command: test
arguments: '--configuration Release --logger trx'
- run: dotnet test --configuration Release --logger "trx;LogFileName=test-results.trx"
See test-filtering.md for advanced filtering patterns.