概要

コードカバレッジは、モジュールやセッションが実行するコードの総量のうちのパーセンテージを測定するものです。

Test Visibility がすでにお使いの言語に設定されていることを確認してください。

コードカバレッジを報告する

互換性

  • dd-trace>=3.20.0
  • jest>=24.8.0jest-circus で実行した場合のみ。
  • mocha>=5.2.0, only if all option in nyc is not explicitly set to true.
  • cucumber-js>=7.0.0, only if all option in nyc is not explicitly set to true.
  • Only Istanbul code coverage is supported.

When tests are instrumented with Istanbul, the Datadog Tracer reports code coverage under the test.code_coverage.lines_pct tag for your test sessions automatically. To instrument tests with Istanbul, you can use nyc.

To report total code coverage from your test sessions, follow these steps:

  1. Install nyc:
npm install --save-dev nyc
  1. Wrap your test command with nyc:
{
  "scripts": {
    "test": "mocha",
    "coverage": "nyc npm run test"
  }
}
Note: Jest includes Istanbul by default, so you don't need to install nyc. Simply pass --coverage.
{
  "scripts": {
    "coverage": "jest --coverage"
  }
}
  1. 新しい coverage コマンドでテストを実行します。
NODE_OPTIONS="-r dd-trace/ci/init" DD_ENV=ci DD_SERVICE=my-javascript-service npm run coverage

既知の制限

If the all option is set to true when running nyc (see nyc docs), the total code coverage reported in the test session does not coincide with the value reported by nyc. This is because it does not include uncovered files (the ones that are not touched by your tests).

互換性

  • dd-trace>=2.31.0

コードカバレッジが利用できる場合、Datadog トレーサー (v2.31.0 以降) は、テストセッションの test.code_coverage.lines_pct タグでそれを報告します。

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.

高度なオプション

Datadog トレーサーのビルトインコードカバレッジは、.runsettings ファイルを通して CoverletVS Code Coverage オプションの両方をサポートしています。

ファイル構造

<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
    <DataCollectionRunSettings>
        <DataCollectors>
            <DataCollector friendlyName="DatadogCoverage">
                <Configuration>
                    <!-- Datadog コードカバレッジの設定 -->
                    ...
                </Configuration>
            </DataCollector>
        </DataCollectors>
    </DataCollectionRunSettings>
</RunSettings>

Coverlet オプション

オプションサマリー
ExcludeByAttribute属性で装飾されたメソッド、クラス、またはアセンブリをコードカバレッジから除外します。
ExcludeByFile特定のソースファイルをコードカバレッジから除外します。
除外するフィルター式を使用してコードカバレッジ分析から除外します。
属性

System.Diagnostics.CodeAnalysis ネームスペースにある ExcludeFromCodeCoverage 属性を作成して適用することで、メソッド、クラス全体、またはアセンブリをコードカバレッジから除外することができます。

ExcludeByAttribute プロパティと属性の短い名前 (ネームスペースを除いた型名) で追加の属性を除外します。

ソースファイル

ExcludeByFile プロパティで特定のソースファイルをコードカバレッジから除外します。

  • カンマで区切って、単一または複数のパスを使用します。
  • 例えば dir1/*.cs のように、ワイルドカード (*) を使用したファイルパスまたはディレクトリパスを使用します。
フィルター

フィルターでは、以下の構文のフィルター式を使って、除外するものを細かく制御することができます。

[<ASSEMBLY_FILTER>]<TYPE_FILTER>

ワイルドカードはサポートされています。

  • * => 0 文字以上にマッチします
  • ? => プレフィックス文字はオプションです

:

  • [*]* => すべてのアセンブリのすべての型を除外します (インスツルメンテーションされるものはありません)
  • [coverlet.*]Coverlet.Core.Coverage => coverlet.* にマッチするアセンブリ (例えば coverlet.core) に属する Coverlet.Core ネームスペース内の Coverage クラスを除外します
  • [*]Coverlet.Core.Instrumentation.* => あらゆるアセンブリ内の Coverlet.Core.Instrumentation ネームスペースに属するすべての型を除外します
  • [coverlet.*.tests?]* => coverlet. で始まり、.test または .tests で終わるアセンブリ内のすべての型を除外します (? により s はオプションになります)
  • [coverlet.*]*,[*]Coverlet.Core*\ => coverlet.* にマッチするアセンブリを除外し、あらゆるアセンブリ内の Coverlet.Core ネームスペースに属するすべての型を除外します

VS コードカバレッジオプション

See Customize code coverage analysis in the Microsoft documentation for additional information.

オプションサマリー
Attributes\Exclude属性で装飾されたメソッド、クラス、またはアセンブリをコードカバレッジから除外します。
Sources\Exclude特定のソースファイルをコードカバレッジから除外します。

Runsettings の例

<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
    <DataCollectionRunSettings>
        <DataCollectors>
            <DataCollector friendlyName="DatadogCoverage">
                <Configuration>
                    <!-- Coverlet 構成 -->
                    <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>

互換性

  • 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 is supported as a code coverage library.

プロジェクトにすでに Jacoco が構成されている場合、Datadog トレーサーはそれをインスツルメントし、カバレッジデータを自動的に Datadog に報告します。

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).

互換性

  • dd-trace>=2.5.0.
  • Python>=3.7.
  • coverage>=4.4.2.
  • pytest>=3.0.0.
  • pytest-cov>=2.7.0.
  • unittest>=3.8.
  • コードカバレッジは coverage.pypytest-cov のみがサポートされています。

テストに coverage.py または pytest-cov がインスツルメンテーションされると、Datadog トレーサーはテストセッションの test.code_coverage.lines_pct タグでコードカバレッジを自動的に報告します。

coverage.py を使ってテストセッションから総コードカバレッジを報告するには、次の手順に従います。

  1. coverage をインストールします。
python3 -m pip install coverage
  1. 新しい coverage コマンドでテストを実行します。
DD_ENV=ci DD_SERVICE=my-python-service coverage run -m pytest

または、pytest-cov を使ってテストセッションから総コードカバレッジを報告するには、次の手順に従います。

  1. pytest をインストールします。
python3 -m pip install pytest
  1. pytest-cov をインストールします。
python3 -m pip install pytest-cov
  1. pytest コマンドに --cov フラグを追加してテストを実行します。
DD_ENV=ci DD_SERVICE=my-python-service pytest --cov

互換性

  • datadog-ci>=2.17.2

JUnit レポートのアップロードでは、コードカバレッジパーセンテージの値をアップロードできます。

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

この例では、85 はテストがカバーする行の割合であり、別のツールで生成する必要があります。

コードカバレッジレポートは別のプロセスで生成する必要があります。そうしないと、JUnit レポートのアップロードでコードカバレッジレポートが生成されません。報告されるメトリクス名は 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.

Test Session coverage tab

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

グラフのエクスポート

グラフをダッシュボードノートブックにエクスポートし、Export ボタンをクリックすると、それに基づいてモニターを作成することができます。

モニターの追加

CI テストモニターを作成して、サービスのコードカバレッジが特定のしきい値を下回るとアラートを受けます。

See your branch’s code coverage evolution

You can also see the code coverage’s evolution on the Branch Overview page and check whether it’s improving or worsening:

プルリクエストのコードカバレッジの変化を表示する

The pull request’s test summary comment shows the code coverage change of a GitHub pull request compared to the default branch.

Intelligent Test Runner and total code coverage

Intelligent Test Runner will not automatically provide total code coverage measurements, even though it requires per test code coverage to function.

参考資料

PREVIEWING: evan.li/clarify-agentless