이 페이지는 아직 한국어로 제공되지 않으며 번역 작업 중입니다. 번역에 관한 질문이나 의견이 있으시면 언제든지 저희에게 연락해 주십시오.

Test Optimization is not available in the selected site () at this time.

Compatibility

Supported test frameworks:

Test FrameworkVersion
JUnit 4>= 4.10
JUnit 5>= 5.3
TestNG>= 6.4
Spock>= 2.0
Cucumber>= 5.4.0
Karate>= 1.0.0
Scalatest>= 3.0.8
Scala MUnit>= 0.7.28

If your test framework is not supported, you can try instrumenting your tests using Manual Testing API.

Supported build systems:

Build SystemVersion
Gradle>= 2.0
Maven>= 3.2.1

Other build systems, such as Ant or Bazel, are supported with the following limitations:

  • Automatic coverage configuration and reporting is not supported.
  • When building a multi-module project, every module is reported in a separate trace.

Setup

You may follow interactive setup steps on the Datadog site or the instructions below.

Configuring the Datadog Java Tracer varies depending on your CI provider.

We support auto-instrumentation for the following CI providers:

CI ProviderAuto-Instrumentation method
GitHub ActionsDatadog Test Visibility Github Action
JenkinsUI-based configuration with Datadog Jenkins plugin
GitLabDatadog Test Visibility GitLab Script
CircleCIDatadog Test Visibility CircleCI Orb

If you are using auto-instrumentation for one of these providers, you can skip the rest of the setup steps below.

GitHub Actions나 CircleCI와 같이 기본 작업자 노드에 액세스하지 않고 클라우드 CI 공급자를 사용할 경우, 라이브러리를 구성해 에이전트리스 모드로 사용하세요. 이 모드를 이용하려면 다음 환경 변수를 설정하세요.

DD_CIVISIBILITY_AGENTLESS_ENABLED=true (필수)
에이전트리스 모드 활성화 또는 비활성화
기본값: false
DD_API_KEY (필수)
테스트 결과를 업로드하는 데 사용되는 Datadog API 키
기본값: (empty)

추가로 데이터를 보낼 Datadog 사이트를 구성하세요.

DD_SITE (필수)
결과를 업로드할 Datadog 사이트
기본값: datadoghq.com

Jenkins 또는 자체 관리형 GitLab CI와 같은 온프레미스 CI 공급자에서 테스트를 실행하는 경우, 에이전트 설치 지침에 따라 각 작업자 노드에 Datadog 에이전트를 설치합니다. 자동으로 테스트 결과를 로그기본 호트스 메트릭과 연결할 수 있기 때문에 이 옵션을 추천합니다.

쿠버네티스 실행기를 사용하는 경우 Datadog에서는 Datadog 연산자를 사용할 것을 권고합니다. 연산자에는 Datadog 허용 제어기가 포함되어 있어 빌드 파드에 자동으로 추적기 라이브러리를 삽입합니다. 참고: Datadog 연산자를 사용할 경우 허용 제어기가 작업을 해주기 때문에 추적기 라이브러리를 다운로드 받고 삽입할 필요가 없습니다. 따라서 아래 해당 단계를 건너뛰어도 됩니다. 그러나 테스트 가시화 기능을 사용할 때 필요한 파드의 환경 변수나 명령줄 파라미터는 설정해야 합니다.

쿠버네티스를 사용하지 않거나 Datadog 허용 제어기를 사용할 수 없고 CI 공급자가 컨테이너 기반 실행기를 사용하는 경우, 추적기를 실행하는 빌드 컨테이너에서 환경 변수 DD_TRACE_AGENT_URL(기본값 http://localhost:8126)를 해당 컨테이너 내에서 액세스할 수 있는 엔드포인트로 설정합니다. 참고: 빌드 내에서 localhost를 사용하면 기본 작업자 노드나 에이전트를 실행하는 컨테이너를 참조하지 않고 컨테이너 자체를 참조합니다.

DD_TRACE_AGENT_URL 은 프로토콜과 포트(예: http://localhost:8126)를 포함하고 DD_AGENT_HOSTDD_TRACE_AGENT_PORT보다 우선하며, CI Visibility를 위해 Datadog 에이전트의 URL을 설정하는 데 권장되는 설정 파라미터입니다.

Datdog 에이전트에 연결하는 데 아직 문제가 있다면 에이전트리스 모드를 사용해 보세요. 참고: 이 방법을 사용할 경우 테스트가 로그인프라스트럭처 메트릭과 상관 관계를 수립하지 않습니다.

Downloading tracer library

You only need to download the tracer library once for each server.

If the tracer library is already available locally on the server, you can proceed directly to running the tests.

Declare DD_TRACER_FOLDER variable with the path to the folder where you want to store the downloaded tracer JAR:

export DD_TRACER_FOLDER=... // e.g. ~/.datadog

Run the command below to download the tracer JAR to the specified folder:

wget -O $DD_TRACER_FOLDER/dd-java-agent.jar 'https://dtdg.co/latest-java-tracer'

You can run the java -jar $DD_TRACER_FOLDER/dd-java-agent.jar command to check the version of the tracer library.

Running your tests

Set the following environment variables to configure the tracer:

DD_CIVISIBILITY_ENABLED=true (Required)
Enables the Test Optimization product.
DD_ENV (Required)
Environment where the tests are being run (for example: local when running tests on a developer workstation or ci when running them on a CI provider).
DD_SERVICE (Required)
Name of the service or library being tested.
DD_TRACER_FOLDER (Required)
Path to the folder where the downloaded Java Tracer is located.
MAVEN_OPTS=-javaagent:$DD_TRACER_FOLDER/dd-java-agent.jar (Required)
Injects the tracer into the Maven build process.

Run your tests as you normally do (for example: mvn test or mvn verify).

Make sure to set the DD_TRACER_FOLDER variable to the path where you have downloaded the tracer.

Run your tests using the org.gradle.jvmargs system property to specify the path to the Datadog Java Tracer JAR.

When specifying tracer arguments, include the following:

  • Enable Test Optimization by setting the dd.civisibility.enabled property to true.
  • Define the environment where the tests are being run using the dd.env property (for example: local when running tests on a developer workstation or ci when running them on a CI provider).
  • Define the name of the service or library being tested in the dd.service property.

For example:

./gradlew cleanTest test -Dorg.gradle.jvmargs=\
-javaagent:$DD_TRACER_FOLDER/dd-java-agent.jar=\
dd.civisibility.enabled=true,\
dd.env=ci,\
dd.service=my-java-app

Specifying org.gradle.jvmargs in the command line overrides the value specified elsewhere. If you have this property specified in a gradle.properties file, be sure to replicate the necessary settings in the command line invocation.

Set the following environment variables to configure the tracer:

DD_CIVISIBILITY_ENABLED=true (Required)
Enables Test Optimization.
DD_ENV (Required)
Environment where the tests are being run (for example: local when running tests on a developer workstation or ci when running them on a CI provider).
DD_SERVICE (Required)
Name of the service or library being tested.
DD_TRACER_FOLDER (Required)
Path to the folder where the downloaded Java Tracer is located.
JAVA_TOOL_OPTIONS=-javaagent:$DD_TRACER_FOLDER/dd-java-agent.jar (Required)
Injects the tracer into the JVMs that execute your tests.

Run your tests as you normally do.

Configuration

Default configuration values work well in most cases.

However, if there is a need to fine-tune the tracer’s behavior, Datadog Tracer configuration options can be used.

Collecting Git metadata

Datadog은 Git 정보를 사용하여 테스트 결과를 시각화하고 리포지토리, 브랜치, 커밋별로 그룹화합니다. Git 메타데이터는 CI 공급자 환경 변수와 프로젝트 경로의 로컬 .git 폴더(사용 가능한 경우)에서 테스트 계측으로 자동 수집합니다.

지원되지 않는 CI 공급자이거나 .git 폴더가 없는 상태에서 테스트를 실행하는 경우, 환경 변수를 사용하여 Git 정보를 수동으로 설정할 수 있습니다. 해당 환경 변수는 자동 탐지된 정보보다 우선합니다. 다음 환경 변수를 설정하여 Git 정보를 제공합니다.

DD_GIT_REPOSITORY_URL
코드가 저장된 리포지토리 URL입니다. HTTP, SSH URL이 모두 지원됩니다.
예시: git@github.com:MyCompany/MyApp.git, https://github.com/MyCompany/MyApp.git
DD_GIT_BRANCH
테스트 중인 Git 브랜치입니다. 대신 태그 정보를 제공하는 경우 비워 둡니다.
예시: develop
DD_GIT_TAG
테스트 중인 Git 태그입니다(해당되는 경우). 대신 브랜치 정보를 제공하는 경우 비워 둡니다.
예시: 1.0.1
DD_GIT_COMMIT_SHA
전체 커밋 해시입니다.
예시: a18ebf361cc831f5535e58ec4fae04ffd98d8152
DD_GIT_COMMIT_MESSAGE
커밋 메시지입니다.
예시: Set release number
DD_GIT_COMMIT_AUTHOR_NAME
커밋 작성자 이름입니다.
예시: John Smith
DD_GIT_COMMIT_AUTHOR_EMAIL
커밋 작성자 이메일입니다.
예시: john@example.com
DD_GIT_COMMIT_AUTHOR_DATE
ISO 8601 형식의 커밋 작성자 날짜입니다.
예시: 2021-03-12T16:00:28Z
DD_GIT_COMMIT_COMMITTER_NAME
커밋 커미터 이름입니다.
예시: Jane Smith
DD_GIT_COMMIT_COMMITTER_EMAIL
커밋 커미터 이메일입니다.
예시: jane@example.com
DD_GIT_COMMIT_COMMITTER_DATE
ISO 8601 형식의 커밋 커미터 날짜입니다.
예시: 2021-03-12T16:00:28Z

Extensions

The tracer exposes a set of APIs that can be used to extend its functionality programmatically.

Adding custom tags to tests

To add custom tags include opentracing-util library as a compile-time dependency to your project.

You can then add custom tags to your tests by using the active span:

import io.opentracing.Span;
import io.opentracing.util.GlobalTracer;

// ...
// inside your test
final Span span = GlobalTracer.get().activeSpan();
if (span != null) {
  span.setTag("test_owner", "my_team");
}
// test continues normally
// ...

To create filters or group by fields for these tags, you must first create facets.

For more information about adding tags, see the Adding Tags section of the Java custom instrumentation documentation.

Adding custom measures to tests

Just like tags, you can add custom measures to your tests by using the current active span:

import io.opentracing.Span;
import io.opentracing.util.GlobalTracer;

// ...
// inside your test
final Span span = GlobalTracer.get().activeSpan();
if (span != null) {
  span.setTag("test.memory.usage", 1e8);
}
// test continues normally
// ...

For more information about custom measures, see the Add Custom Measures guide.

Using manual testing API

If you use one of the supported testing frameworks, the Java Tracer automatically instruments your tests and sends the results to the Datadog backend.

If you are using a framework that is not supported, or an ad-hoc testing solution, you can harness the manual testing API, which also reports test results to the backend.

To use the manual testing API, add the dd-trace-api library as a compile-time dependency to your project.

Domain model

The API is based around four concepts: test session, test module, test suite, and test.

Test session

A test session represents a project build, which typically corresponds to execution of a test command issued by a user or by a CI script.

To start a test session, call datadog.trace.api.civisibility.CIVisibility#startSession and pass the name of the project and the name of the testing framework you used.

When all your tests have finished, call datadog.trace.api.civisibility.DDTestSession#end, which forces the library to send all remaining test results to the backend.

Test module

A test module represents a smaller unit of work within a project build, typically corresponding to a project module. For example, a Maven submodule or Gradle subproject.

To start a test mode, call datadog.trace.api.civisibility.DDTestSession#testModuleStart and pass the name of the module.

When the module has finished building and testing, call datadog.trace.api.civisibility.DDTestModule#end.

Test Suite

A test suite comprises a set of tests that share common functionality. They can share a common initialization and teardown, and can also share some variables. A single suite usually corresponds to a Java class that contains test cases.

Create test suites in a test module by calling datadog.trace.api.civisibility.DDTestModule#testSuiteStart and passing the name of the test suite.

Call datadog.trace.api.civisibility.DDTestSuite#end when all the related tests in the suite have finished their execution.

Test

A test represents a single test case that is executed as part of a test suite. Usually it corresponds to a method that contains testing logic.

Create tests in a suite by calling datadog.trace.api.civisibility.DDTestSuite#testStart and passing the name of the test.

Call datadog.trace.api.civisibility.DDTest#end when a test has finished execution.

Code Example

The following code represents a simple usage of the API:

package com.datadog.civisibility.example;

import datadog.trace.api.civisibility.CIVisibility;
import datadog.trace.api.civisibility.DDTest;
import datadog.trace.api.civisibility.DDTestModule;
import datadog.trace.api.civisibility.DDTestSession;
import datadog.trace.api.civisibility.DDTestSuite;
import java.lang.reflect.Method;

// the null arguments in the calls below are optional startTime/endTime values:
// when they are not specified, current time is used
public class ManualTest {
    public static void main(String[] args) throws Exception {
        DDTestSession testSession = CIVisibility.startSession("my-project-name", "my-test-framework", null);
        testSession.setTag("my-tag", "additional-session-metadata");
        try {
            runTestModule(testSession);
        } finally {
            testSession.end(null);
        }
    }

    private static void runTestModule(DDTestSession testSession) throws Exception {
        DDTestModule testModule = testSession.testModuleStart("my-module", null);
        testModule.setTag("my-module-tag", "additional-module-metadata");
        try {
            runFirstTestSuite(testModule);
            runSecondTestSuite(testModule);
        } finally {
            testModule.end(null);
        }
    }

    private static void runFirstTestSuite(DDTestModule testModule) throws Exception {
        DDTestSuite testSuite = testModule.testSuiteStart("my-suite", ManualTest.class, null);
        testSuite.setTag("my-suite-tag", "additional-suite-metadata");
        try {
            runTestCase(testSuite);
        } finally {
            testSuite.end(null);
        }
    }

    private static void runTestCase(DDTestSuite testSuite) throws Exception {
        Method myTestCaseMethod = ManualTest.class.getDeclaredMethod("myTestCase");
        DDTest ddTest = testSuite.testStart("myTestCase", myTestCaseMethod, null);
        ddTest.setTag("my-test-case-tag", "additional-test-case-metadata");
        ddTest.setTag("my-test-case-tag", "more-test-case-metadata");
        try {
            myTestCase();
        } catch (Exception e) {
            ddTest.setErrorInfo(e); // pass error info to mark test case as failed
        } finally {
            ddTest.end(null);
        }
    }

    private static void myTestCase() throws Exception {
        // run some test logic
    }

    private static void runSecondTestSuite(DDTestModule testModule) {
        DDTestSuite secondTestSuite = testModule.testSuiteStart("my-second-suite", ManualTest.class, null);
        secondTestSuite.setSkipReason("this test suite is skipped"); // pass skip reason to mark test suite as skipped
        secondTestSuite.end(null);
    }
}

Always call datadog.trace.api.civisibility.DDTestSession#end at the end so that all the test info is flushed to Datadog.

Best practices

Deterministic test parameters representation

Test Optimization works best when the test parameters are deterministic and stay the same between test runs. If a test case has a parameter that varies between test executions (such as a current date, a random number, or an instance of a class whose toString() method is not overridden), some of the product features may not work as expected. For example, the history of executions may not be available, or the test case may not be classified as flaky even if it exhibits flakiness.

The best way to fix this is to make sure that the test parameters are the same between test runs.

In JUnit 5, this can also be addressed by customizing the string representation of the test parameters without changing their values. To do so, use org.junit.jupiter.api.Named interface or change the name parameter of the org.junit.jupiter.params.ParameterizedTest annotation:

@ParameterizedTest
@MethodSource("namedArguments")
void parameterizedTest(String s, Date d) {
   // The second parameter in this test case is non-deterministic.
   // In the argument provider method it is wrapped with Named to ensure it has a deterministic name.
}

static Stream<Arguments> namedArguments() {
    return Stream.of(
            Arguments.of(
                    "a string",
                    Named.of("current date", new Date())),
            Arguments.of(
                    "another string",
                    Named.of("a date in the future", new Date(System.currentTimeMillis() + TimeUnit.DAYS.toMillis(1))))
    );
}
@ParameterizedTest(name = "[{index}] {0}, a random number from one to ten")
@MethodSource("randomArguments")
void anotherParameterizedTest(String s, int i) {
  // The second parameter in this test case is non-deterministic.
  // The name of the parameterized test is customized to ensure it has a deterministic name.
}

static Stream<Arguments> randomArguments() {
    return Stream.of(
            Arguments.of("a string", ThreadLocalRandom.current().nextInt(10) + 1),
            Arguments.of("another string", ThreadLocalRandom.current().nextInt(10) + 1)
    );
}

Troubleshooting

The tests are not appearing in Datadog after enabling Test Optimization in the tracer

Verify that the tracer is injected into your build process by examining your build’s logs. If the injection is successful, you can see a line containing DATADOG TRACER CONFIGURATION. If the line is not there, make sure that the environment variables used to inject and configure the tracer are available to the build process. A common mistake is to set the variables in a build step and run the tests in another build step. This approach may not work if the variables are not propagated between build steps.

Ensure that you are using the latest version of the tracer.

Verify that your build system and testing framework are supported by Test Optimization. See the list of supported build systems and test frameworks.

Ensure that the dd.civisibility.enabled property (or DD_CIVISIBILITY_ENABLED environment variable) is set to true in the tracer arguments.

Try running your build with tracer debug logging enabled by setting the DD_TRACE_DEBUG environment variable to true. Check the build output for any errors that indicate tracer misconfiguration, such as an unset DD_API_KEY environment variable.

Tests or source code compilation fails when building a project with the tracer attached

By default, Test Optimization runs Java code compilation with a compiler plugin attached.

The plugin is optional, as it only serves to reduce the performance overhead.

Depending on the build configuration, adding the plugin can sometimes disrupt the compilation process.

If the plugin interferes with the build, disable it by adding dd.civisibility.compiler.plugin.auto.configuration.enabled=false to the list of -javaagent arguments (or by setting DD_CIVISIBILITY_COMPILER_PLUGIN_AUTO_CONFIGURATION_ENABLED=false environment variable).

Builds fails because dd-javac-plugin-client artifact cannot be found

It is possible that the Java compiler plugin injected into the build is not available if the build uses a custom artifactory storage or if it is run in offline mode.

If this is the case, you can disable plugin injection by adding dd.civisibility.compiler.plugin.auto.configuration.enabled=false to the list of -javaagent arguments (or by setting the DD_CIVISIBILITY_COMPILER_PLUGIN_AUTO_CONFIGURATION_ENABLED environment variable to false).

The plugin is optional, as it only serves to reduce the performance overhead.

Tests fail when building a project with the tracer attached

In some cases attaching the tracer can break tests, especially if they run asserts on the internal state of the JVM or instances of third-party libraries’ classes.

While the best approach is such cases is to update the tests, there is also a quicker option of disabling the tracer’s third-party library integrations.

The integrations provide additional insights into what happens in the tested code and are especially useful in integration tests, to monitor things like HTTP requests or database calls. They are enabled by default.

To disable a specific integration, refer to the Datadog Tracer Compatibility table for the relevant configuration property names. For example, to disable OkHttp3 client request integration, add dd.integration.okhttp-3.enabled=false to the list of -javaagent arguments.

To disable all integrations, augment the list of -javaagent arguments with dd.trace.enabled=false (or set DD_TRACE_ENABLED=false environment variable).

Further reading