# Setting Up Test Coverage Profiling for C#

In order 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.

TIP

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

# Visual Studio / TFS / MSTest Code Coverage

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:

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

Afterwards, you can upload the resulting XML file to Teamscale. Please refer to the article on external uploads for details. In the REST API of the upload, specify VS_COVERAGE as value of the format parameter.

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.

# Manual tests

There are many C# profilers for which no special mapping files are needed. We do, however, have a custom low-overhead profiler. It is specially suited for the Test Gap analysis as it only records which methods were executed, not which particular lines. This allows it to have virtually no impact on performance. We have successfully used it to profile in several production environments. Since only minimal information is recorded at runtime, Teamscale needs the PDB files generated during the build to interpret the coverage files.

# How to Set Up Test Gap Analysis (TGA) with Mapping Files

Teamscale allows you to upload the PDB files ahead of time, e.g., when they are created during the build process. Then, when uploading coverage files, it will use the mapping files to interpret the coverage information. Since every time you build your application, new mapping files are generated, Teamscale needs to know, which mapping files to use when you upload coverage. Thus, during the upload of the mapping files, you must provide a version string (any unique string will do, e.g., a revision number). You must again provide the exact same version string when uploading coverage files that belong to those mapping files.

# Uploading PDB files

The following is an example of how to upload PDB files using curl:

curl --request POST \
--user bob:e434mikm35d \
--form "file=@./assembly.pdb" \
localhost:8080/api/projects/PROJECTID/external-analysis/dotnet-debug-info?t=branch:timestamp&version=myVersion

The branch:timestamp must be the branch name and commit timestamp in milliseconds of the commit which was processed by the build to generate the mapping file.

# Uploading coverage results

The following is an example of how to upload CQSE profiler coverage files using curl:

curl --request POST \
--user bob:e434mikm35d \
--form "report=@./coverage.txt" \
localhost:8080/api/projects/PROJECTID/external-analysis/dotnet-ephemeral-trace?version=myVersion&message=myMessage&partition=myPartition