---
title: Go Tests
description: Datadog, the leading service for cloud-scale monitoring.
breadcrumbs: Docs > Test Optimization in Datadog > Configure Test Optimization > Go Tests
---

> For the complete documentation index, see [llms.txt](https://docs.datadoghq.com/llms.txt).

# Go Tests

{% callout %}
# Important note for users on the following Datadog sites: app.ddog-gov.com, us2.ddog-gov.com

{% alert level="danger" %}
This product is not supported for your selected [Datadog site](https://docs.datadoghq.com/getting_started/site.md). ({% placeholder "user-datadog-site-name" /%}).
{% /alert %}

{% /callout %}

## Compatibility{% #compatibility %}

Supported test frameworks:

- `testing` package

{% alert level="info" %}
If you use Bazel to run Go tests, use the Datadog [Bazel rules for Go tests](https://docs.datadoghq.com/tests/setup/bazel/go.md).
{% /alert %}

## Configuring reporting method{% #configuring-reporting-method %}

To report test results to Datadog, you need to configure the Datadog Go library:

{% tab title="CI Provider with Auto-Instrumentation Support" %}
We support auto-instrumentation for the following CI providers:

| CI Provider    | Auto-Instrumentation method                                                                                                                           |
| -------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- |
| GitHub Actions | [Datadog Test Visibility Github Action](https://github.com/marketplace/actions/configure-datadog-test-visibility)                                     |
| Jenkins        | [UI-based configuration](https://docs.datadoghq.com/continuous_integration/pipelines/jenkins.md#enable-test-optimization) with Datadog Jenkins plugin |
| GitLab         | [Datadog Test Visibility GitLab Script](https://github.com/DataDog/test-visibility-gitlab-script)                                                     |
| CircleCI       | [Datadog Test Visibility CircleCI Orb](https://circleci.com/orbs/registry/orb/datadog/test-visibility-circleci-orb)                                   |

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](https://docs.datadoghq.com/tests/containers.md#choose-an-instrumentation-method).

If the auto-instrumentation step configures the process that runs your tests, you can skip the rest of the setup steps below.
{% /tab %}

{% tab title="Cloud CI Provider" %}
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:

{% alert level="warning" %}
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.
{% /alert %}

{% dl %}

{% dt %}
`DD_CIVISIBILITY_AGENTLESS_ENABLED=true` (Required for Agentless mode)
{% /dt %}

{% dd %}
Enables Agentless mode to send test results directly to Datadog. **Default**: `false`
{% /dd %}

{% dt %}
`DD_API_KEY` (Required for Agentless mode)
{% /dt %}

{% dd %}
The [Datadog API key](https://app.datadoghq.com/organization-settings/api-keys) used to authenticate test result uploads. **Default**: `(empty)`
{% /dd %}

{% /dl %}

If you use a Datadog site other than US1, set the following variable:

{% dl %}

{% dt %}
`DD_SITE` (Optional for Agentless mode)
{% /dt %}

{% dd %}
The [Datadog site](https://docs.datadoghq.com/getting_started/site.md) to upload results to. **Default**: `datadoghq.com` 
{% /dd %}

{% /dl %}

{% /tab %}

{% tab title="On-Premises CI Provider" %}
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](https://docs.datadoghq.com/agent.md). This is the recommended option as it allows you to automatically link test results to [logs](https://docs.datadoghq.com/tracing/other_telemetry/connect_logs_and_traces.md) and [underlying host metrics](https://docs.datadoghq.com/infrastructure.md).

If you are using a Kubernetes executor, Datadog recommends using the [Datadog Operator](https://docs.datadoghq.com/containers/datadog_operator.md). The operator includes [Datadog Admission Controller](https://docs.datadoghq.com/agent/cluster_agent/admission_controller.md) which can automatically [inject the tracer library](https://docs.datadoghq.com/tracing/trace_collection/library_injection_local.md?tab=kubernetes) 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](https://docs.datadoghq.com/tracing/other_telemetry/connect_logs_and_traces.md) and [infrastructure metrics](https://docs.datadoghq.com/infrastructure.md).
{% /tab %}

## Installing Orchestrion{% #installing-orchestrion %}

Orchestrion is a tool to process Go source code at compilation time and automatically insert instrumentation using `dd-trace-go`.

Install `orchestrion` from [https://github.com/datadog/orchestrion](https://github.com/datadog/orchestrion) using the command:

```bash
$ go install github.com/DataDog/orchestrion@latest
```

{% collapsible-section %}
### Optional: project go.mod registration

You can automatically add `orchestrion` to your project's dependencies by running:

```bash
$ orchestrion pin
```

This command has the following effects:

1. Create a new `orchestrion.tool.go` file containing content similar to:

   ```go
   // Code generated by `orchestrion pin`; DO NOT EDIT.
   
   // This file is generated by `orchestrion pin`, and is used to include a blank import of the
   // orchestrion package(s) so that `go mod tidy` does not remove the requirements rom go.mod.
   // This file should be checked into source control.
   
   //go:build tools
   
   package tools
   
   import _ "github.com/DataDog/orchestrion"
   ```

1. Run `go get github.com/DataDog/orchstrion@<current-release>` to make sure the project version corresponds to the one currently being used

1. Run `go mod tidy` to make sure your `go.mod` and `go.sum` files are up-to-date

If you do not run this command, it is done automatically when required. Once done, the version of `orchestrion` used by this project can be controlled directly using the `go.mod` file, as you would control any other dependency.
{% /collapsible-section %}

Orchestrion supports the two latest releases of Go, matching Go's [official release policy](https://go.dev/doc/devel/release#policy). It may function correctly with older Go releases, but Datadog cannot support older releases that do not work.

In addition to this, Orchestrion only supports projects using [Go modules](https://pkg.go.dev/cmd/go#hdr-Modules__module_versions__and_more).

## Instrumenting tests{% #instrumenting-tests %}

To configure the library, set the following environment variables before starting the test process. For parallel test runners, set them on the parent process so every worker inherits them.

{% dl %}

{% dt %}
`DD_CIVISIBILITY_ENABLED=true` (Required)
{% /dt %}

{% dd %}
Enables Test Optimization. **Default**: `false`
{% /dd %}

{% dt %}
`DD_SERVICE` (Optional)
{% /dt %}

{% dd %}
Name of the service or library under test. **Default**: The repository name
{% /dd %}

{% dt %}
`DD_ENV` (Optional)
{% /dt %}

{% dd %}
Name of the environment where tests are being run. **Default**: `(empty)` **Examples**: `local`, `ci`
{% /dd %}

{% dt %}
`DD_CIVISIBILITY_AGENTLESS_ENABLED=true` (Required for Agentless mode)
{% /dt %}

{% dd %}
Enables Agentless mode to send test results directly to Datadog. **Default**: `false`
{% /dd %}

{% dt %}
`DD_API_KEY` (Required for Agentless mode)
{% /dt %}

{% dd %}
The Datadog API key used to authenticate test result uploads. This variable does not enable Agentless mode. **Default**: `(empty)`
{% /dd %}

{% dt %}
`DD_SITE` (Optional for Agentless mode)
{% /dt %}

{% dd %}
The [Datadog site](https://docs.datadoghq.com/getting_started/site.md) to upload test results to. Set this configuration when using a site other than US1. **Default**: `datadoghq.com`
{% /dd %}

{% dt %}
`DD_TRACE_AGENT_URL` (Only when using the Datadog Agent)
{% /dt %}

{% dd %}
The Datadog Agent URL for trace collection, in the form `http://hostname:port`. **Default**: `http://localhost:8126`
{% /dd %}

{% dt %}
`DD_TEST_SESSION_NAME` (Optional)
{% /dt %}

{% dd %}
Identifies a group of tests, such as `unit-tests`, `integration-tests`, or `smoke-tests`. **Default**: The CI job name and test command, or the test command if the CI job name is unavailable. **Example**: `unit-tests`, `integration-tests`, `smoke-tests`
{% /dd %}

{% /dl %}

Prefix your go test command with `orchestrion`:

```bash
$ orchestrion go test -race ./...
```

If you have not run `orchestrion pin`, you may see a message similar to the following appear, as `orchestrion pin` is automatically executed:

```gdscript3
  ╭──────────────────────────────────────────────────────────────────────────────╮
  │                                                                              │
  │  Warning: github.com/DataDog/orchestrion is not present in your go.mod       │
  │  file.                                                                       │
  │  In order to ensure build reliability and reproductibility, orchestrion      │
  │  will now add itself in your go.mod file by:                                 │
  │                                                                              │
  │      1. creating a new file named orchestrion.tool.go                        │
  │      2. running go get github.com/DataDog/orchestrion@v0.9.4                 │
  │      3. running go mod tidy                                                  │
  │                                                                              │
  │  You should commit the resulting changes into your source control system.    │
  │                                                                              │
  ╰──────────────────────────────────────────────────────────────────────────────╯
```

### Alternative{% #alternative %}

*Orchestrion* at the core is a standard Go toolchain `-toolexec` proxy. Instead of using `orchestrion go`, you can also manually provide the `-toolexec` argument to `go` commands that accept it:

```console
$ go build -toolexec 'orchestrion toolexec' .
$ go test -toolexec 'orchestrion toolexec' -race .
```

## Further reading{% #further-reading %}

Additional helpful documentation, links, and articles:

- [Forwarding Environment Variables for Tests in Containers](https://docs.datadoghq.com/continuous_integration/tests/containers.md)
- [Explore Test Results and Performance](https://docs.datadoghq.com/continuous_integration/tests.md)
- [Speed up your test jobs with Test Impact Analysis](https://docs.datadoghq.com/tests/test_impact_analysis/go.md)
- [Troubleshooting CI Visibility](https://docs.datadoghq.com/continuous_integration/troubleshooting.md)
