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.
Vitest requires extra dependencies for running with code coverage. See vitest docs for more information. After the dependencies are installed, pass --coverage to your test command:
{"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
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 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.
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).
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
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 you want to have injected (for example: DD_CIVISIBILITY_JACOCO_PLUGIN_VERSION=0.8.11).
When tests are instrumented with coverage.py or 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, follow these steps:
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, follow these steps:
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:
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.
If your project has 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).
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.
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.
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.
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.