Skip to content

Setting Up Test Coverage Profiling for C#

To use the Test Gap analysis, you’ll need to upload test coverage to Teamscale.

Uploading Coverage

If you already have existing coverage files have a look at Uploading Test Coverage to Teamscale.

Step-by-step Tutorial

In our .NET TGA tutorial we outline the steps needed to set up TGA for a concrete sample project.

How to collect coverage will depend on how you run your tests:

Tests Run by dotnet test

Use Coverlet. Example:

sh
dotnet add package coverlet.collector
dotnet test --collect:"XPlat Code Coverage"

After your tests are finished, you can find the coverage file coverage.cobertura.xml in the TestResults directory. Upload it to Teamscale with format COBERTURA.

Tests Run by MSBuild

Use AltCover's MSBuild integration.

In the AltCover.Collect task, please add the attribute LcovReport="/PATH/TO/lcov.info". This causes AltCover to write the coverage data to the specified path in LCOV format. Upload the lcov.info file to Teamscale with format LCOV.

Windows Tests Run on a Separate Test Environment

If the tests you want to profile are run outside your build pipeline and under Windows, you should use our .NET profiler. Follow our tutorial to learn how to use the profiler.

Manual tests

Please refer to our tutorial for .NET applications.

Tests Run in a Docker Container

AltCover allows recording test coverage for all .NET variants and operating systems, including inside Linux Docker containers.

Follow this process to instrument your application and collect coverage:

  1. build your application
  2. instrument the application during the build
  3. deploy your application to your test environment and run your tests
  4. collect coverage

To instrument your application, run this in your build pipeline:

sh
altcover --save --inplace --eager --inputDirectory /PATH/TO/YOUR/ASSEMBLIES --linecover --portable

This replaces your assemblies with instrumented ones. It also adds some additional files and folders to the input directory you specified. You must deploy these to your test environment together with your assemblies.

To collect coverage after your tests, run this on your test environment:

sh
altcover runner --collect -r /PATH/TO/YOUR/ASSEMBLIES/ON/TEST/ENVIRONMENT -l lcov.info

Upload the lcov.info file to Teamscale with format LCOV.

CodeCoverage.exe deprecated

Deprecated

Microsoft has deprecated CodeCoverage.exe. Use one of the above-mentioned alternatives instead.

By default, TFS / Azure DevOps will make a .coverage file available to you if you enable code coverage collection in your builds. This guide shows you how to get that coverage to show up in Teamscale.

Since the original .coverage file is in binary format, it needs to be converted to XML before the upload to Teamscale happens. To perform the conversion, Microsoft's CodeCoverage.exe utility tool can be used. It is available as part of the Microsoft.CodeCoverage nuget package.

To convert your result.coverage file to result.xml, you can call the CodeCoverage.exe tool like this:

powershell
CodeCoverage.exe analyze /output:result.xml result.coverage

Afterwards, you can upload the resulting XML file to Teamscale with format VS_COVERAGE.

Merging Coverage

You can also use the above tool to merge multiple .coverage files into one .xml file, which will lead to faster upload processing.