Skip to main content

Code Coverage

TUnit includes built-in code coverage support via Microsoft.Testing.Extensions.CodeCoverage, which is automatically included when you install the TUnit meta package. No additional packages are required.

Coverlet is Not Compatible​

If you are migrating from another test framework and currently use Coverlet (coverlet.collector or coverlet.msbuild), you must remove it. TUnit uses Microsoft.Testing.Platform, not VSTest, and Coverlet only works with the legacy VSTest platform.

Remove these from your .csproj:

<!-- Remove these -->
<PackageReference Include="coverlet.collector" Version="x.x.x" />
<PackageReference Include="coverlet.msbuild" Version="x.x.x" />

Running Coverage​

Use the --coverage flag when running your tests:

# Basic coverage collection
dotnet run --configuration Release --coverage

# Specify output location
dotnet run --configuration Release --coverage --coverage-output ./coverage/

# Specify output format (cobertura is the default)
dotnet run --configuration Release --coverage --coverage-output-format cobertura

# Multiple formats
dotnet run --configuration Release --coverage \
--coverage-output-format cobertura \
--coverage-output-format xml

Output Formats​

The Microsoft coverage tool supports multiple output formats:

FormatFlagNotes
Cobertura--coverage-output-format coberturaDefault. Widely supported by CI tools.
XML--coverage-output-format xmlVisual Studio format.

You can specify multiple --coverage-output-format flags to generate several formats in one run.

Viewing Coverage Results​

Coverage files are generated in your test output directory:

TestResults/
├── coverage.cobertura.xml
└── <guid>/
└── coverage.xml

Tools for viewing results:

  • Visual Studio -- Built-in coverage viewer
  • VS Code -- Extensions like "Coverage Gutters"
  • ReportGenerator -- Generate HTML reports: reportgenerator -reports:coverage.cobertura.xml -targetdir:coveragereport
  • CI Tools -- Most CI systems can parse Cobertura format natively

Configuration​

Coverage Settings File​

You can customize coverage behavior (include/exclude modules, etc.) with a settings file:

dotnet run --configuration Release --coverage --coverage-settings coverage.config

CI/CD Integration​

GitHub Actions​

- name: Run tests with coverage
run: dotnet run --project ./tests/MyProject.Tests --configuration Release --coverage

Azure Pipelines​

- task: DotNetCoreCLI@2
inputs:
command: 'run'
arguments: '--configuration Release --coverage --coverage-output $(Agent.TempDirectory)/coverage/'

JetBrains dotCover​

dotCover (in Rider, ReSharper and the dotCover command-line tools) supports per-test coverage for TUnit projects. This requires the JetBrains.dotCover.Framework package, a small Microsoft.Testing.Platform extension that is inert when dotCover is not attached.

When creating a new project from the TUnit template in C#, F# or VB, pass --enable-dotcover (lowercase; the option is case-sensitive):

dotnet new TUnit -n MyTests --enable-dotcover
dotnet new TUnit -n MyTests --language F# --enable-dotcover
dotnet new TUnit -n MyTests --language VB --enable-dotcover

Or add the package to an existing project:

dotnet add package JetBrains.dotCover.Framework

Troubleshooting​

Coverage files not generated?

  • Ensure you are using the TUnit meta package, not just TUnit.Engine.
  • Verify you have a recent .NET SDK installed.

Missing coverage for some assemblies?

Further reading: