選択したサイト () では現在 CI Visibility は利用できません。

互換性

サポート対象言語:

言語バージョン
Python 2>= 2.7
Python 3>= 3.6

サポートされているテストフレームワーク:

Test Frameworkバージョン
pytest>= 3.0.0
pytest-benchmark>= 3.1.0
unittest>= 3.7

報告方法の構成

Datadog にテスト結果を報告するには、Datadog Python ライブラリを構成する必要があります。

If you are running tests on an on-premises CI provider, such as Jenkins or self-managed GitLab CI, install the Datadog Agent on each worker node by following the Agent installation instructions. This is the recommended option as it allows you to automatically link test results to logs and underlying host metrics.

If you are using a Kubernetes executor, Datadog recommends using the Datadog Operator. The operator includes Datadog Admission Controller which can automatically inject the tracer library into the build pods. Note: If you use the Datadog Operator, there is no need to download and inject the tracer library since the Admission Controller can do this for you, so you can skip the corresponding step below. However, you still need to make sure that your pods set the environment variables or command-line parameters necessary to enable Test Visibility.

If you are not using Kubernetes or can’t use the Datadog Admission Controller and the CI provider is using a container-based executor, set the DD_TRACE_AGENT_URL environment variable (which defaults to http://localhost:8126) in the build container running the tracer to an endpoint that is accessible from within that container. Note: Using localhost inside the build references the container itself and not the underlying worker node or any container where the Agent might be running in.

DD_TRACE_AGENT_URL includes the protocol and port (for example, http://localhost:8126) and takes precedence over DD_AGENT_HOST and DD_TRACE_AGENT_PORT, and is the recommended configuration parameter to configure the Datadog Agent’s URL for CI Visibility.

If you still have issues connecting to the Datadog Agent, use the Agentless Mode. Note: When using this method, tests are not correlated with logs and infrastructure metrics.

If you are using a cloud CI provider without access to the underlying worker nodes, such as GitHub Actions or CircleCI, configure the library to use the Agentless mode. For this, set the following environment variables:

DD_CIVISIBILITY_AGENTLESS_ENABLED=true (Required)
Enables or disables Agentless mode.
Default: false
DD_API_KEY (Required)
The Datadog API key used to upload the test results.
Default: (empty)

Additionally, configure the Datadog site to which you want to send data.

DD_SITE (Required)
The Datadog site to upload results to.
Default: datadoghq.com

Python トレーサーのインストール

次のコマンドを実行して、Python トレーサーをインストールします。

pip install -U ddtrace

詳細については、Python トレーサーのインストールドキュメントを参照してください。

テストのインスツルメンテーション

pytest の使用

pytest テストのインスツルメンテーションを有効にするには、pytest の実行時に --ddtrace オプションを追加し、DD_SERVICE 環境変数でテスト対象のサービスまたはライブラリの名前を、DD_ENV 環境変数でテストが実行されている環境を指定します (たとえば、開発者ワークステーションでテストを実行する場合は local、CI プロバイダーでテストを実行する場合は ci)。

DD_SERVICE=my-python-app DD_ENV=ci pytest --ddtrace

もし、残りの APM インテグレーションも有効にして flamegraph でより多くの情報を取得したい場合は、--ddtrace-patch-all オプションを追加します。

DD_SERVICE=my-python-app DD_ENV=ci pytest --ddtrace --ddtrace-patch-all

pytest-benchmark の使用

pytest-benchmark でベンチマークテストをインスツルメンテーションするには、pytest 実行時に —ddtrace オプションを付けてベンチマークテストを実行すると、Datadog は自動的に pytest-benchmark からメトリクスを検出します。

def square_value(value):
    return value * value


def test_square_value(benchmark):
    result = benchmark(square_value, 5)
    assert result == 25

unittest の使用

unittest テストのインスツルメンテーションを有効にするには、unittest コマンドの冒頭に ddtrace-run を追加してテストを実行します。

DD_SERVICE 環境変数において、テスト対象のサービスまたはライブラリの名前を必ず指定してください。 また、DD_ENV 環境変数で、テストが実行されている環境を宣言することもできます。

DD_SERVICE=my-python-app DD_ENV=ci ddtrace-run python -m unittest

あるいは、unittest インスツルメンテーションを手動で有効にしたい場合は、patch() を使ってインテグレーションを有効にしてください。

from ddtrace import patch
import unittest
patch(unittest=True)

class MyTest(unittest.TestCase):
    def test_will_pass(self):
        assert True

既知の制限

unittest テスト実行が並列で実行される場合、場合によっては、インスツルメンテーションが損なわれ、テストの可視性に影響を及ぼす可能性があります。

Datadog では、テストの可視性に影響が出ないよう、一度に使用するプロセスを 1 つに制限することを推奨しています。

テストにカスタムタグを追加する

テストの引数に ddspan と宣言することで、テストにカスタムタグを追加することができます。

from ddtrace import tracer

# テストの引数として `ddspan` を宣言します
def test_simple_case(ddspan):
    # タグを設定します
    ddspan.set_tag("test_owner", "my_team")
    # テストは正常に続きます
    # ...

これらのタグに対して、フィルターや group by フィールドを作成するには、まずファセットを作成する必要があります。タグの追加についての詳細は、Python カスタムインスツルメンテーションドキュメントのタグの追加セクションを参照してください。

テストにカスタムメトリクスを追加する

タグと同様に、現在アクティブなスパンを使用して、テストにカスタムメトリクスを追加できます。

from ddtrace import tracer

# テストの引数として `ddspan` を宣言します
def test_simple_case(ddspan):
    # タグを設定します
    ddspan.set_tag("memory_allocations", 16)
    # テストは正常に続きます
    # ...

カスタムメトリクスについては、カスタムメトリクスの追加ガイドを参照してください。

構成設定

以下は、コードか、または環境変数を使用した、トレーサーで使用できる最も重要なコンフィギュレーション設定のリストです。

ddtrace.config.service
テスト中のサービスまたはライブラリの名前。
環境変数: DD_SERVICE
デフォルト: pytest
: my-python-app
ddtrace.config.env
テストが実行されている環境の名前。
環境変数: DD_ENV
デフォルト: none
: localci

次の環境変数を使用して、Datadog Agent の場所を構成できます。

DD_TRACE_AGENT_URL
http://hostname:port の形式のトレース収集用の Datadog Agent URL。
デフォルト: http://localhost:8126

他のすべての Datadog トレーサー構成オプションも使用できます。

Git のメタデータを収集する

Datadog uses Git information for visualizing your test results and grouping them by repository, branch, and commit. Git metadata is automatically collected by the test instrumentation from CI provider environment variables and the local .git folder in the project path, if available.

If you are running tests in non-supported CI providers or with no .git folder, you can set the Git information manually using environment variables. These environment variables take precedence over any auto-detected information. Set the following environment variables to provide Git information:

DD_GIT_REPOSITORY_URL
URL of the repository where the code is stored. Both HTTP and SSH URLs are supported.
Example: git@github.com:MyCompany/MyApp.git, https://github.com/MyCompany/MyApp.git
DD_GIT_BRANCH
Git branch being tested. Leave empty if providing tag information instead.
Example: develop
DD_GIT_TAG
Git tag being tested (if applicable). Leave empty if providing branch information instead.
Example: 1.0.1
DD_GIT_COMMIT_SHA
Full commit hash.
Example: a18ebf361cc831f5535e58ec4fae04ffd98d8152
DD_GIT_COMMIT_MESSAGE
Commit message.
Example: Set release number
DD_GIT_COMMIT_AUTHOR_NAME
Commit author name.
Example: John Smith
DD_GIT_COMMIT_AUTHOR_EMAIL
Commit author email.
Example: john@example.com
DD_GIT_COMMIT_AUTHOR_DATE
Commit author date in ISO 8601 format.
Example: 2021-03-12T16:00:28Z
DD_GIT_COMMIT_COMMITTER_NAME
Commit committer name.
Example: Jane Smith
DD_GIT_COMMIT_COMMITTER_EMAIL
Commit committer email.
Example: jane@example.com
DD_GIT_COMMIT_COMMITTER_DATE
Commit committer date in ISO 8601 format.
Example: 2021-03-12T16:00:28Z

その他の参考資料