互換性
dd-trace>=3.20.0。jest>=24.8.0、jest-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:
- Install
nyc:
npm install --save-dev nyc
- 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"
}
}
- 新しい
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).
互換性
コードカバレッジが利用できる場合、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 ファイルを通して Coverlet と VS 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>
互換性
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.py と pytest-cov のみがサポートされています。
テストに coverage.py または pytest-cov がインスツルメンテーションされると、Datadog トレーサーはテストセッションの test.code_coverage.lines_pct タグでコードカバレッジを自動的に報告します。
coverage.py を使ってテストセッションから総コードカバレッジを報告するには、次の手順に従います。
coverage をインストールします。
python3 -m pip install coverage
- 新しい
coverage コマンドでテストを実行します。
DD_ENV=ci DD_SERVICE=my-python-service coverage run -m pytest
または、pytest-cov を使ってテストセッションから総コードカバレッジを報告するには、次の手順に従います。
pytest をインストールします。
python3 -m pip install pytest
pytest-cov をインストールします。
python3 -m pip install pytest-cov
pytest コマンドに --cov フラグを追加してテストを実行します。
DD_ENV=ci DD_SERVICE=my-python-service pytest --cov
互換性
JUnit レポートのアップロードでは、コードカバレッジパーセンテージの値をアップロードできます。
datadog-ci junit upload --service <service_name> --report-measures=test.code_coverage.lines_pct:85 <path>
この例では、85 はテストがカバーする行の割合であり、別のツールで生成する必要があります。
コードカバレッジレポートは別のプロセスで生成する必要があります。そうしないと、JUnit レポートのアップロードでコードカバレッジレポートが生成されません。報告されるメトリクス名は test.code_coverage.lines_pct でなければなりません。