Auto-instrumentation runs on the CI executor and does not automatically apply to tests in a separate container. Before using it for containerized tests, see Tests in Containers.
If the auto-instrumentation step configures the process that runs your tests, you can skip the rest of the setup steps below.
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:
Set these variables before starting the test process. For parallel test runners, set them on the parent process so every worker inherits them.
DD_CIVISIBILITY_AGENTLESS_ENABLED=true selects Agentless mode. DD_API_KEY provides authentication but does not enable Agentless mode.
DD_CIVISIBILITY_AGENTLESS_ENABLED=true (Required for Agentless mode)
Enables Agentless mode to send test results directly to Datadog. Default: false
DD_API_KEY (Required for Agentless mode)
The Datadog API key used to authenticate test result uploads. Default: (empty)
If you use a Datadog site other than US1, set the following variable:
DD_SITE (Optional for Agentless mode)
The Datadog site to upload results to. Default: datadoghq.com
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.
fromddtraceimporttracer# Declare `ddspan` as argument to your testdeftest_simple_case(ddspan):# Set your tagsddspan.set_tag("test_owner","my_team")# test continues normally# ...
これらのタグに対してフィルターや group by フィールドを作成するには、まずファセットを作成する必要があります。タグの追加について詳しくは、Python カスタムインスツルメンテーションのドキュメントのタグの追加セクションを参照してください。
テストへのカスタム測定値の追加
タグと同様に、テストにカスタム測定値を追加するには、現在アクティブなスパンを使用します。
fromddtraceimporttracer# Declare `ddspan` as an argument to your testdeftest_simple_case(ddspan):# Set your tagsddspan.set_tag("memory_allocations",16)# test continues normally# ...
fromddtrace.ext.test_visibilityimportapiimportpathlibimportsysif__name__=="__main__":# Enable the Test Optimization serviceapi.enable_test_visibility()# Discover itemsapi.TestSession.discover("manual_test_api_example","my_manual_framework","1.0.0")test_module_1_id=api.TestModuleId("module_1")api.TestModule.discover(test_module_1_id)test_suite_1_id=api.TestSuiteId(test_module_1_id,"suite_1")api.TestSuite.discover(test_suite_1_id)test_1_id=api.TestId(test_suite_1_id,"test_1")api.Test.discover(test_1_id)# A parameterized test with codeowners and a source filetest_2_codeowners=["team_1","team_2"]test_2_source_info=api.TestSourceFileInfo(pathlib.Path("/path/to_my/tests.py"),16,35)parametrized_test_2_a_id=api.TestId(test_suite_1_id,"test_2",parameters='{"parameter_1": "value_is_a"}')api.Test.discover(parametrized_test_2_a_id,codeowners=test_2_codeowners,source_file_info=test_2_source_info,resource="overriden resource name A",)parametrized_test_2_b_id=api.TestId(test_suite_1_id,"test_2",parameters='{"parameter_1": "value_is_b"}')api.Test.discover(parametrized_test_2_b_id,codeowners=test_2_codeowners,source_file_info=test_2_source_info,resource="overriden resource name B")test_3_id=api.TestId(test_suite_1_id,"test_3")api.Test.discover(test_3_id)test_4_id=api.TestId(test_suite_1_id,"test_4")api.Test.discover(test_4_id)# Start and execute itemsapi.TestSession.start()api.TestModule.start(test_module_1_id)api.TestSuite.start(test_suite_1_id)# test_1 passes successfullyapi.Test.start(test_1_id)api.Test.mark_pass(test_1_id)# test_2's first parametrized test succeeds, but the second fails without attaching exception infoapi.Test.start(parametrized_test_2_a_id)api.Test.mark_pass(parametrized_test_2_a_id)api.Test.start(parametrized_test_2_b_id)api.Test.mark_fail(parametrized_test_2_b_id)# test_3 is skippedapi.Test.start(test_3_id)api.Test.mark_skip(test_3_id,skip_reason="example skipped test")# test_4 fails, and attaches exception infoapi.Test.start(test_4_id)try:raise(ValueError("this test failed"))except:api.Test.mark_fail(test_4_id,exc_info=api.TestExcInfo(*sys.exc_info()))# Finish suites and modulesapi.TestSuite.finish(test_suite_1_id)api.TestModule.finish(test_module_1_id)api.TestSession.finish()
unit-tests、integration-tests、smoke-tests などのテストグループを識別します。 デフォルト: CI ジョブ名とテストコマンド、または CI ジョブ名が利用できない場合はテストコマンド。 例: unit-tests、integration-tests、smoke-tests
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