---
url: /howto/setting-up-profiler-tga/cs.md
---

# Setting Up Test Coverage Profiling for C\#

To use the [**Test Gap analysis**](../../../reference/test-gap-analysis/#goal-of-test-gap-analysis), you’ll need to upload test coverage to Teamscale.

:::tip Uploading Coverage
If you already have existing coverage files have a look at [Uploading Test Coverage to Teamscale](../../uploading-external-data/).
:::

:::tip Step-by-step Tutorial
In our [.NET TGA tutorial](../../../tutorial/setting-up-tga-dotnet/) 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:

\[\[toc]]

## Tests Run by `dotnet test`

Use [Coverlet](https://github.com/coverlet-coverage/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](../../uploading-external-data/) with format `COBERTURA`.

## Tests Run by MSBuild

Use [AltCover's MSBuild integration](https://github.com/SteveGilham/altcover/wiki/Examples-of-use#direct-msbuild-use).

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](../../uploading-external-data/) 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.
See the [.NET Profiler reference](../../../reference/coverage-profilers/teamscale-dotnet-profiler/) for setup instructions.

## Manual tests

Please refer to our [.NET Profiler reference](../../../reference/coverage-profilers/teamscale-dotnet-profiler/).

## Tests Run in a Docker Container

[AltCover](https://github.com/SteveGilham/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](../../uploading-external-data/) with format `LCOV`.

## CodeCoverage.exe&#x20;

::: warning 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](https://www.nuget.org/packages/Microsoft.CodeCoverage/).

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](../../uploading-external-data/#upload-from-azure-devops) with format `VS_COVERAGE`.

:::tip 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.
:::
