To instrument your test suite, prefix your test command with dd-trace ci run, providing the name of the service or library under test as the --dd-service parameter, and the environment where tests are being run (for example, local when running tests on a developer workstation, or ci when running them on a CI provider) as the --dd-env parameter. For example:
dd-trace ci run --dd-service=my-dotnet-app -- VSTest.Console.exe {test_assembly}.dll
All tests are automatically instrumented.
Compatibility with Microsoft.CodeCoverage nuget package
Since Microsoft.CodeCoverage version 17.2.0 Microsoft introduced dynamic instrumentation using the .NET CLR Profiling API enabled by default only on Windows. Datadog’s automatic instrumentation relies on the .NET CLR Profiling API. This API allows only one subscriber (for example, dd-trace). The use of CodeCoverage dynamic instrumentation breaks the automatic test instrumentation.
The solution is to switch from dynamic instrumentation to static instrumentation. Modify your .runsettings file with the following configuration knobs:
<?xml version="1.0" encoding="utf-8"?><RunSettings><DataCollectionRunSettings><DataCollectors><DataCollectorfriendlyName="Code Coverage"><Configuration><CodeCoverage><!-- Switching to static instrumentation (dynamic instrumentation collides with dd-trace instrumentation) --><EnableStaticManagedInstrumentation>True</EnableStaticManagedInstrumentation><EnableDynamicManagedInstrumentation>False</EnableDynamicManagedInstrumentation><UseVerifiableInstrumentation>False</UseVerifiableInstrumentation><EnableStaticNativeInstrumentation>True</EnableStaticNativeInstrumentation><EnableDynamicNativeInstrumentation>False</EnableDynamicNativeInstrumentation> ...
</CodeCoverage></Configuration></DataCollector></DataCollectors></DataCollectionRunSettings></RunSettings>
Configuration settings
You can change the default configuration of the CLI by using command line arguments or environment variables. For a full list of configuration settings, run:
dd-trace ci run --help
The following list shows the default values for key configuration settings:
--dd-service
Name of the service or library under test. Environment variable: DD_SERVICE Default: The repository name Example: my-dotnet-app
--dd-env
Name of the environment where tests are being run. Environment variable: DD_ENV Default: none Examples: local, ci
--agent-url
Datadog Agent URL for trace collection in the form http://hostname:port. Environment variable: DD_TRACE_AGENT_URL Default: http://localhost:8126
test_session.name (only available as an environment variable)
Identifies a group of tests, such as integration-tests, unit-tests or smoke-tests. Environment variable: DD_TEST_SESSION_NAME Default: (CI job name + test command) Example: unit-tests, integration-tests, smoke-tests
You can add custom tags to your tests by using the current active span:
// inside your testvarscope=Tracer.Instance.ActiveScope;// from Datadog.Trace;if(scope!=null){scope.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 .NET custom instrumentation documentation.
Just like tags, you can add custom measures to your tests by using the current active span:
// inside your testvarscope=Tracer.Instance.ActiveScope;// from Datadog.Trace;if(scope!=null){scope.Span.SetTag("memory_allocations",16);}// test continues normally// ...
To create filters or visualizations for these tags, you must first create facets. For more information about adding tags, see the Adding Tags section of the .NET custom instrumentation documentation.
When code coverage is available, the Datadog Tracer (v2.31.0 or later) reports it under the test.code_coverage.lines_pct tag for your test sessions.
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.
Note: When using Test Impact Analysis, the tracer’s built-in code coverage is enabled by default.
You can see the evolution of the test coverage in the Coverage tab of a test session.
For more information about exclusion options, see Code Coverage.
Configure your project to use the Datadog.Trace.BenchmarkDotNet exporter using the DatadogDiagnoser attribute or the WithDatadog() extension method. For example:
ISO 8601 形式のコミットのコミッターの日付。 例: 2021-03-12T16:00:28Z
Custom instrumentation
Note: Your custom instrumentation setup depends on the dd-trace version. To use the custom instrumentation, you must keep the package versions for dd-trace and Datadog.Trace NuGet packages in sync.
To use the custom instrumentation in your .NET application:
Execute dd-trace --version to get the version of the tool.
Add the Datadog.TraceNuGet package with the same version to your application.
In your application code, access the global tracer through the Datadog.Trace.Tracer.Instance property to create new spans.
Note: To use the manual testing API, you must add the Datadog.Trace NuGet package in the target .NET project.
If you use XUnit, NUnit, or MSTest with your .NET projects, Test Optimization automatically instruments them and sends the test results to Datadog. If you use an unsupported testing framework or if you have a different testing mechanism, you can instead use the API to report test results to Datadog.
The API is based around three concepts: test module, test suites, and tests.
Test module
A test module represents the .NET assembly that includes the tests.
To start a test module, call TestModule.Create() and pass the name of the module or .NET assembly name where the tests are located.
When all your tests have finished, call module.Close() or module.CloseAsync(), which forces the library to send all remaining test results to the backend.
Test suites
A test suite comprises a set of tests. They can have a common initialization and teardown methods and share some variables. In .NET, they are usually implemented as a Test class or fixture containing multiple test methods. A test suite can optionally have additional information like attributes or error information.
Create test suites in the test module by calling module.GetOrCreateSuite() and passing the name of the test suite.
Call suite.Close() when all the related tests in the suite have finished their execution.
Tests
Each test runs inside a suite and must end in one of these three statuses: TestStatus.Pass, TestStatus.Fail, or TestStatus.Skip.
A test can optionally have additional information like:
Parameters
Attributes
Error information
Test traits
Benchmark data
Create tests in a suite by calling suite.CreateTest() and passing the name of the test. When a test ends, call test.Close() with one of the predefined statuses.
Code example
The following code represents a simple usage of the API:
Always call module.Close() or module.CloseAsync() at the end so that all the test data is flushed to Datadog.
Best practices
Test session name DD_TEST_SESSION_NAME
Use DD_TEST_SESSION_NAME to define the name of the test session and the related group of tests. Examples of values for this tag would be:
unit-tests
integration-tests
smoke-tests
flaky-tests
ui-tests
backend-tests
If DD_TEST_SESSION_NAME is not specified, the default value used is a combination of the:
CI job name
Command used to run the tests (such as yarn test)
The test session name needs to be unique within a repository to help you distinguish different groups of tests.
When to use DD_TEST_SESSION_NAME
There’s a set of parameters that Datadog checks to establish correspondence between test sessions. The test command used to execute the tests is one of them. If the test command contains a string that changes for every execution, such as a temporary folder, Datadog considers the sessions to be unrelated to each other. For example:
dotnet test --temp-dir=/var/folders/t1/rs2htfh55mz9px2j4prmpg_c0000gq/T
Datadog recommends using DD_TEST_SESSION_NAME if your test commands vary between executions.