---
title: Code Coverage in Datadog
description: Learn how to report and use code coverage in Datadog.
breadcrumbs: Docs > Test Optimization in Datadog > Code Coverage in Datadog
---

# Code Coverage in Datadog

{% callout %}
# Important note for users on the following Datadog sites: app.ddog-gov.com

{% alert level="danger" %}
This product is not supported for your selected [Datadog site](https://docs.datadoghq.com/getting_started/site). ().
{% /alert %}

{% /callout %}

{% alert level="warning" %}
This Test Optimization feature is legacy. Use the new dedicated [Code Coverage](https://docs.datadoghq.com/code_coverage/) product instead.
{% /alert %}

## Overview{% #overview %}

Code coverage is a measure of the total code coverage percentage that a module or session exercises.

Ensure that [Test Optimization](https://docs.datadoghq.com/tests/) is already set up for your language.

## Report code coverage{% #report-code-coverage %}

{% tab title="JavaScript/TypeScript" %}
### Compatibility{% #compatibility %}

- `dd-trace>=4.45.0` and `dd-trace>=5.21.0`.
- `jest>=24.8.0`, only when run with `jest-circus`.
- `mocha>=5.2.0`.
- `cucumber-js>=7.0.0`.
- `vitest>=2.0.0`.

{% alert level="danger" %}
**Note**: The DataDog Tracer does not generate code coverage. If your tests are run with code coverage enabled, `dd-trace` reports it under the `test.code_coverage.lines_pct` tag for your test sessions automatically.
{% /alert %}

#### Mocha/Cucumber-js{% #mochacucumber-js %}

Only [`Istanbul`](https://istanbul.js.org/) code coverage is supported for `mocha` and `cucumber-js`.

To report total code coverage from your `mocha` and `cucumber-js` test sessions, install [`nyc`](https://github.com/istanbuljs/nyc) and wrap your test commands:

1. Install `nyc`:

```
npm install --save-dev nyc
```
Wrap your test commands with `nyc`:
```json
{
  "scripts": {
    "test": "mocha",
    "coverage": "nyc npm run test"
  }
}
```

#### Jest{% #jest %}

Jest includes Istanbul by default, so you don't need to install `nyc`. Simply pass `--coverage`:

```json
{
  "scripts": {
    "coverage": "jest --coverage"
  }
}
```

The only supported [`coverageProvider`](https://jestjs.io/docs/configuration#coverageprovider-string) is `babel`, which is the default.

#### Vitest{% #vitest %}

Vitest requires extra dependencies for running with code coverage. See [vitest docs](https://vitest.dev/guide/coverage.html) for more information. After the dependencies are installed, pass `--coverage` to your test command:

```json
{
  "scripts": {
    "coverage": "vitest run --coverage"
  }
}
```

After modifying your test commands, run your tests with the new `coverage` command:

```
NODE_OPTIONS="-r dd-trace/ci/init" DD_ENV=ci DD_SERVICE=my-javascript-service npm run coverage
```

{% /tab %}

{% tab title=".NET" %}
### Compatibility{% #compatibility %}

- `dd-trace>=2.31.0`.

When code coverage is available, the Datadog Tracer (v2.31.0 or later) reports it under the `test.code_coverage.lines_pct` tag for your test sessions.

If you are using [Coverlet](https://github.com/coverlet-coverage/coverlet) to compute your code coverage, indicate the path to the report file in the `DD_CIVISIBILITY_EXTERNAL_CODE_COVERAGE_PATH` environment variable when running `dd-trace`. The report file must be in the OpenCover or Cobertura formats. Alternatively, you can enable the Datadog Tracer's built-in code coverage calculation with the `DD_CIVISIBILITY_CODE_COVERAGE_ENABLED=true` environment variable.

**Note**: `DD_CIVISIBILITY_EXTERNAL_CODE_COVERAGE_PATH` is only used when the command instrumented by `dd-trace ci run` is `dotnet test`, `dotnet vstest`, or `vstest.console`. For example, the results in the variable are ignored when running `dd-trace ci run -- coverlet TestAssembly.dll --target dotnet --targetargs "test TestAssembly.dll"`.

### Advanced options{% #advanced-options %}

The Datadog Tracer's built-in code coverage has support for both `Coverlet` and `VS Code Coverage` options through the `.runsettings` file.

#### File structure{% #file-structure %}

```xml
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
    <DataCollectionRunSettings>
        <DataCollectors>
            <DataCollector friendlyName="DatadogCoverage">
                <Configuration>
                    <!-- Datadog Code Coverage settings -->
                    ...
                </Configuration>
            </DataCollector>
        </DataCollectors>
    </DataCollectionRunSettings>
</RunSettings>
```

#### Coverlet options{% #coverlet-options %}

| Option             | Summary                                                                              |
| ------------------ | ------------------------------------------------------------------------------------ |
| ExcludeByAttribute | Exclude methods, classes or assemblies decorated with attributes from code coverage. |
| ExcludeByFile      | Exclude specific source files from code coverage.                                    |
| Exclude            | Exclude from code coverage analysis using filter expressions.                        |

##### Attributes{% #attributes %}

You can exclude a method, an entire class, or assembly from code coverage by creating and applying the `ExcludeFromCodeCoverage` attribute present in the `System.Diagnostics.CodeAnalysis` namespace.

Exclude additional attributes with the `ExcludeByAttribute` property and the short name of the attribute (the type name without the namespace).

##### Source files{% #source-files %}

Exclude specific source files from code coverage with the `ExcludeByFile` property.

- Use a single or multiple paths, separated by comma.
- Use the file path or directory path with a wildcard (`*`), for example: `dir1/*.cs`.

##### Filters{% #filters %}

Filters provide fine-grained control over what gets excluded using **filter expressions** with the following syntax:

`[<ASSEMBLY_FILTER>]<TYPE_FILTER>`

**Wildcards** are supported:

- `*` => matches zero or more characters
- `?` => the prefixed character is optional

**Examples**:

- `[*]*` => Excludes all types in all assemblies (nothing is instrumented)
- `[coverlet.*]Coverlet.Core.Coverage` => Excludes the `Coverage` class in the `Coverlet.Core` namespace belonging to any assembly that matches `coverlet.*` (for example, `coverlet.core`)
- `[*]Coverlet.Core.Instrumentation.*` => Excludes all types belonging to the `Coverlet.Core.Instrumentation` namespace in any assembly
- `[coverlet.*.tests?]*` => Excludes all types in any assembly starting with `coverlet.` and ending with `.test` or `.tests` (the `?` makes the `s` optional)
- `[coverlet.*]*,[*]Coverlet.Core*\` => Excludes assemblies matching `coverlet.*` and excludes all types belonging to the `Coverlet.Core` namespace in any assembly

#### VS code coverage options{% #vs-code-coverage-options %}

See [Customize code coverage analysis](https://learn.microsoft.com/en-us/visualstudio/test/customizing-code-coverage-analysis?view=vs-2022) in the Microsoft documentation for additional information.

| Option             | Summary                                                                               |
| ------------------ | ------------------------------------------------------------------------------------- |
| Attributes\Exclude | Exclude methods, classes, or assemblies decorated with attributes from code coverage. |
| Sources\Exclude    | Exclude specific source files from code coverage.                                     |

#### Runsettings example{% #runsettings-example %}

```xml
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
    <DataCollectionRunSettings>
        <DataCollectors>
            <DataCollector friendlyName="DatadogCoverage">
                <Configuration>
                    <!-- Coverlet configuration -->
                    <ExcludeByAttribute>CompilerGeneratedAttribute</ExcludeByAttribute>
                    <ExcludeByFile>**/Fibonorial.cs</ExcludeByFile>
                    <Exclude>[myproject.*.tests?]*</Exclude>

                    <!-- VS Code Coverage configuration -->
                    <CodeCoverage>
                        <Attributes>
                            <Exclude>
                                <Attribute>^System\.ObsoleteAttribute$</Attribute>
                            </Exclude>
                        </Attributes>
                        <Sources>
                            <Exclude>
                                <Source>^MyFile\.cs$</Source>
                            </Exclude>
                        </Sources>
                    </CodeCoverage>
                </Configuration>
            </DataCollector>
        </DataCollectors>
    </DataCollectionRunSettings>
</RunSettings>
```

{% /tab %}

{% tab title="Java" %}
### Compatibility{% #compatibility %}

- `dd-trace-java >= 1.24.2`.

When code coverage is available, the Datadog Tracer reports it under the `test.code_coverage.lines_pct` tag for your test sessions.

[Jacoco](https://www.eclemma.org/jacoco/) is supported as a code coverage library.

If your project already has Jacoco configured, the Datadog Tracer instruments it and reports the coverage data to Datadog automatically.

Otherwise, you can configure the tracer to add Jacoco to your test runs at runtime. Use `DD_CIVISIBILITY_JACOCO_PLUGIN_VERSION` environment variable to specify which [version of Jacoco](https://mvnrepository.com/artifact/org.jacoco/org.jacoco.agent) you want to have injected (for example: `DD_CIVISIBILITY_JACOCO_PLUGIN_VERSION=0.8.11`).
{% /tab %}

{% tab title="Python" %}
### Compatibility{% #compatibility %}

- `dd-trace>=2.5.0`.
- `Python>=3.7`.
- `coverage>=4.4.2`.
- `pytest>=3.0.0`.
- `pytest-cov>=2.7.0`.
- `unittest>=3.8`.
- Only [`coverage.py`](https://github.com/nedbat/coveragepy) and [`pytest-cov`](https://github.com/pytest-dev/pytest-cov) code coverage are supported.

When tests are instrumented with [`coverage.py`](https://github.com/nedbat/coveragepy) or [`pytest-cov`](https://github.com/pytest-dev/pytest-cov), the Datadog Tracer reports code coverage under the `test.code_coverage.lines_pct` tag for your test sessions automatically.

To report total code coverage from your test sessions with [`coverage.py`](https://github.com/nedbat/coveragepy), follow these steps:

1. Install `coverage`:

```
python3 -m pip install coverage
```
Run your test with the new `coverage` command:
```
DD_ENV=ci DD_SERVICE=my-python-service coverage run -m pytest
```

Alternatively, to report total code coverage from your test sessions with [`pytest-cov`](https://github.com/pytest-dev/pytest-cov), follow these steps:

1. Install `pytest`:

```
python3 -m pip install pytest
```
Install `pytest-cov`:
```
python3 -m pip install pytest-cov
```
Run your test by appending the `--cov` flag to your `pytest` command:
```
DD_ENV=ci DD_SERVICE=my-python-service pytest --cov
```

{% /tab %}

{% tab title="Ruby" %}
### Compatibility{% #compatibility %}

- `datadog-ci-rb>=1.7.0`
- `simplecov>=0.18.0`.

{% alert level="danger" %}
**Note**: The DataDog library does not generate total code coverage. If your tests are run with code coverage enabled, `datadog-ci-rb` reports it under the `test.code_coverage.lines_pct` tag for your test sessions automatically.
{% /alert %}

If your project has [simplecov](https://github.com/simplecov-ruby/simplecov) configured, the datadog-ci-rb library instruments it and reports the coverage data to Datadog automatically under the `test.code_coverage.lines_pct` tag for your test sessions.

This feature is enabled by default. Use `DD_CIVISIBILITY_SIMPLECOV_INSTRUMENTATION_ENABLED` environment variable to disable this feature (for example: `DD_CIVISIBILITY_SIMPLECOV_INSTRUMENTATION_ENABLED=0`).
{% /tab %}

{% tab title="Go" %}
### Compatibility{% #compatibility %}

- `go test -cover`

{% alert level="danger" %}
**Note**: The DataDog library does not generate total code coverage. If your tests are run with code coverage enabled, `dd-trace-go` reports it under the `test.code_coverage.lines_pct` tag for your test sessions automatically.
{% /alert %}

If your tests are executed with the `-cover` flag, the Datadog library instruments it and automatically reports the coverage data to Datadog under the `test.code_coverage.lines_pct` tag for your test sessions.
{% /tab %}

{% tab title="Swift" %}
### Compatibility{% #compatibility %}

- `dd-sdk-swift-testing>=2.5.3`.
- `Xcode>=14.3`.

When code coverage is enabled, the Datadog Tracer reports it under the `test.code_coverage.lines_pct` tag for your test sessions.

To enable code coverage for Xcode projects you can follow this guide from Apple: [Enable code coverage in your test plan](https://developer.apple.com/documentation/xcode/determining-how-much-code-your-tests-cover#Enable-code-coverage-in-your-test-plan).

For SPM tests, add the `--enable-code-coverage` parameter to your `swift test` invocation.
{% /tab %}

{% tab title="JUnit Report Uploads" %}
### Compatibility{% #compatibility %}

- `datadog-ci>=2.17.2`.

You can upload a code coverage percentage value when using JUnit Report uploads:

```shell
datadog-ci junit upload --service <service_name> --report-measures=test.code_coverage.lines_pct:85 <path>
```

In this example, `85` is the percentage of lines covered by your tests and needs to be generated with a different tool.

The code coverage report needs to be generated in a different process, otherwise the JUnit report uploads will not generate code coverage reports. The reported metric name must be `test.code_coverage.lines_pct`.
{% /tab %}

## Graph code coverage{% #graph-code-coverage %}

Reported code coverage is reported as `@test.code_coverage.lines_pct`, which represents the total percentage in the facet, and can be plotted as any other measure facet in the CI Visibility Explorer.

{% image
   source="https://datadog-docs.imgix.net/images/continuous_integration/graph_code_coverage.c8b6aef07428e30c0a1d18e058c55afe.png?auto=format"
   alt="" /%}

## Test Session coverage tab{% #test-session-coverage-tab %}

Reported code coverage also appears on the **Coverage** tab in a test session's details page:

{% image
   source="https://datadog-docs.imgix.net/images/continuous_integration/code_coverage_tab.eafff8bdde0489b7ed03ab1ba9bff952.png?auto=format"
   alt="" /%}

## Export your graph{% #export-your-graph %}

You can export your graph to a [dashboard](https://docs.datadoghq.com/dashboards) or a [notebook](https://docs.datadoghq.com/notebooks), and create a [monitor](https://docs.datadoghq.com/monitors) based on it by clicking the **Export** button:

{% image
   source="https://datadog-docs.imgix.net/images/continuous_integration/code_coverage_export_to.4fad03f8b747989bc1920cffdaf49ad1.png?auto=format"
   alt="" /%}

## Add a monitor{% #add-a-monitor %}

Get alerted whenever code coverage for your service drops below a certain threshold by creating a [CI Test Monitor](https://docs.datadoghq.com/monitors/types/ci/#maintain-code-coverage-percentage):

{% image
   source="https://datadog-docs.imgix.net/images/continuous_integration/code_coverage_monitor.abc6e24dad8b15c81015ff4c99b15ec4.png?auto=format"
   alt="" /%}

## See your branch's code coverage evolution{% #see-your-branchs-code-coverage-evolution %}

You can also see the code coverage's evolution on the [Branch Overview page](https://docs.datadoghq.com/continuous_integration/tests/developer_workflows#branch-overview) and check whether it's improving or worsening:

{% image
   source="https://datadog-docs.imgix.net/images/continuous_integration/code_coverage_branch_view.c2e740fdd8332921bc6bf368eb437f20.png?auto=format"
   alt="" /%}

## Show code coverage change of a pull request{% #show-code-coverage-change-of-a-pull-request %}

The pull request's [test summary comment](https://docs.datadoghq.com/tests/developer_workflows/#test-summaries-in-github-pull-requests) shows the code coverage change of a GitHub pull request compared to the default branch.

## Test Impact Analysis and total code coverage{% #test-impact-analysis-and-total-code-coverage %}

[Test Impact Analysis](https://docs.datadoghq.com/tests/test_impact_analysis) does **not** automatically provide total code coverage measurements, even though it requires *per test* code coverage to function.

## Further reading{% #further-reading %}

- [Troubleshoot faster with the GitLab Source Code integration in Datadog](https://www.datadoghq.com/blog/gitlab-source-code-integration)
- [Learn about Test Optimization](https://docs.datadoghq.com/tests)
- [Learn about CI Monitors](https://docs.datadoghq.com/monitors/types/ci)
