Skip to content

Commit b7a0733

Browse files
[test optimization] [SDTEST-996] Add instructions for test session name (#26807)
* add test session name instructions * troubleshooting nad better examples * Apply suggestions from code review Co-authored-by: Brett Blue <[email protected]> --------- Co-authored-by: Brett Blue <[email protected]>
1 parent 19a4ae4 commit b7a0733

File tree

2 files changed

+66
-15
lines changed

2 files changed

+66
-15
lines changed

Diff for: content/en/tests/setup/javascript.md

+48-15
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Supported test frameworks:
3333
| Test Framework | Version | Notes |
3434
|---|---|---|
3535
| Jest | >= 24.8.0 | Only `jsdom` (in the `jest-environment-jsdom` package) and `node` (in the `jest-environment-node` package) are supported as test environments. Custom environments like `@jest-runner/electron/environment` in `jest-electron-runner` are not supported.<br><br>Only [`jest-circus`][1] is supported as [`testRunner`][2].<br><br>[`test.concurrent`](#jests-testconcurrent) is not supported. |
36-
| Mocha | >= 5.2.0 | Mocha >= 9.0.0 has [partial support](#known-limitations). |
36+
| Mocha | >= 5.2.0 |
3737
| Cucumber | >= 7.0.0 |
3838
| Cypress | >= 6.7.0 |
3939
| Playwright | >= 1.18.0 |
@@ -79,10 +79,10 @@ For more information, see the [JavaScript Tracer installation documentation][4].
7979

8080
{{< tabs >}}
8181
{{% tab "Jest/Mocha" %}}
82-
Set the `NODE_OPTIONS` environment variable to `-r dd-trace/ci/init`. Run your tests as you normally would, specifying the environment where the tests are run in the `DD_ENV` environment variable. For example, set `DD_ENV` to `local` when running tests on a developer workstation, or `ci` when running them on a CI provider:
82+
Set the `NODE_OPTIONS` environment variable to `-r dd-trace/ci/init`. Run your tests as you normally would, optionally specifying a name for your test session with `DD_TEST_SESSION_NAME`:
8383

8484
```bash
85-
NODE_OPTIONS="-r dd-trace/ci/init" DD_ENV=ci DD_SERVICE=my-javascript-app yarn test
85+
NODE_OPTIONS="-r dd-trace/ci/init" DD_TEST_SESSION_NAME=unit-tests yarn test
8686
```
8787

8888
**Note**: If you set a value for `NODE_OPTIONS`, make sure it does not overwrite `-r dd-trace/ci/init`. This can be done using the `${NODE_OPTIONS:-}` clause:
@@ -137,10 +137,10 @@ For more information about custom measures, see the [Add Custom Measures Guide][
137137
{{% /tab %}}
138138

139139
{{% tab "Playwright" %}}
140-
Set the `NODE_OPTIONS` environment variable to `-r dd-trace/ci/init`. Run your tests as you normally would, specifying the environment where the tests are run in the `DD_ENV` environment variable. For example, set `DD_ENV` to `local` when running tests on a developer workstation, or `ci` when running them on a CI provider:
140+
Set the `NODE_OPTIONS` environment variable to `-r dd-trace/ci/init`. Run your tests as you normally would, optionally specifying a name for your test session with `DD_TEST_SESSION_NAME`:
141141

142142
```bash
143-
NODE_OPTIONS="-r dd-trace/ci/init" DD_ENV=ci DD_SERVICE=my-javascript-app yarn test
143+
NODE_OPTIONS="-r dd-trace/ci/init" DD_TEST_SESSION_NAME=e2e-tests yarn test:e2e
144144
```
145145

146146
**Note**: If you set a value for `NODE_OPTIONS`, make sure it does not overwrite `-r dd-trace/ci/init`. This can be done using the `${NODE_OPTIONS:-}` clause:
@@ -220,10 +220,9 @@ The format of the annotations is the following, where `$TAG_NAME` is a *string*
220220
{{% /tab %}}
221221

222222
{{% tab "Cucumber" %}}
223-
Set the `NODE_OPTIONS` environment variable to `-r dd-trace/ci/init`. Run your tests as you normally would, specifying the environment where the tests are run in the `DD_ENV` environment variable. For example, set `DD_ENV` to `local` when running tests on a developer workstation, or `ci` when running them on a CI provider:
224-
223+
Set the `NODE_OPTIONS` environment variable to `-r dd-trace/ci/init`. Run your tests as you normally would, optionally specifying a name for your test session with `DD_TEST_SESSION_NAME`:
225224
```bash
226-
NODE_OPTIONS="-r dd-trace/ci/init" DD_ENV=ci DD_SERVICE=my-javascript-app yarn test
225+
NODE_OPTIONS="-r dd-trace/ci/init" DD_TEST_SESSION_NAME=integration-tests yarn test:integration
227226
```
228227

229228
**Note**: If you set a value for `NODE_OPTIONS`, make sure it does not overwrite `-r dd-trace/ci/init`. This can be done using the `${NODE_OPTIONS:-}` clause:
@@ -311,7 +310,7 @@ module.exports = defineConfig({
311310
e2e: {
312311
setupNodeEvents(on, config) {
313312
// your previous code is before this line
314-
require('dd-trace/ci/cypress/plugin')(on, config)
313+
return require('dd-trace/ci/cypress/plugin')(on, config)
315314
}
316315
}
317316
})
@@ -375,7 +374,7 @@ If you already defined a `pluginsFile`, initialize the instrumentation with:
375374
{{< code-block lang="javascript" filename="cypress/plugins/index.js" >}}
376375
module.exports = (on, config) => {
377376
// your previous code is before this line
378-
require('dd-trace/ci/cypress/plugin')(on, config)
377+
return require('dd-trace/ci/cypress/plugin')(on, config)
379378
}
380379
{{< /code-block >}}
381380

@@ -420,10 +419,10 @@ module.exports = (on, config) => {
420419
{{< /code-block >}}
421420

422421

423-
Run your tests as you normally do, specifying the environment where test are being run (for example, `local` when running tests on a developer workstation, or `ci` when running them on a CI provider) in the `DD_ENV` environment variable. For example:
422+
Run your tests as you normally would, optionally specifying a name for your test session with `DD_TEST_SESSION_NAME`:
424423

425424
{{< code-block lang="shell" >}}
426-
DD_ENV=ci DD_SERVICE=my-ui-app npm test
425+
DD_TEST_SESSION_NAME=ui-tests yarn test:ui
427426
{{< /code-block >}}
428427

429428

@@ -493,10 +492,10 @@ If the browser application being tested is instrumented using [Browser Monitorin
493492

494493
`vitest` and `dd-trace` require Node.js>=18.19 or Node.js>=20.6 to work.
495494

496-
Set the `NODE_OPTIONS` environment variable to `--import dd-trace/register.js -r dd-trace/ci/init`. Run your tests as you normally would, specifying the environment where the tests are run in the `DD_ENV` environment variable. For example, set `DD_ENV` to `local` when running tests on a developer workstation, or `ci` when running them on a CI provider:
495+
Set the `NODE_OPTIONS` environment variable to `--import dd-trace/register.js -r dd-trace/ci/init`. Run your tests as you normally would, optionally specifying a name for your test session with `DD_TEST_SESSION_NAME`:
497496

498497
```bash
499-
NODE_OPTIONS="--import dd-trace/register.js -r dd-trace/ci/init" DD_ENV=ci DD_SERVICE=my-javascript-app yarn test
498+
NODE_OPTIONS="--import dd-trace/register.js -r dd-trace/ci/init" DD_TEST_SESSION_NAME=smoke-tests yarn test:smoke
500499
```
501500

502501
**Note**: If you set a value for `NODE_OPTIONS`, make sure it does not overwrite `--import dd-trace/register.js -r dd-trace/ci/init`. This can be done using the `${NODE_OPTIONS:-}` clause:
@@ -533,6 +532,7 @@ jobs:
533532
my-job:
534533
name: Run tests
535534
runs-on: ubuntu-latest
535+
# Invalid NODE_OPTIONS
536536
env:
537537
NODE_OPTIONS: -r dd-trace/ci/init
538538
steps:
@@ -599,6 +599,12 @@ For more information, see [Code Coverage][6].
599599

600600
The following is a list of the most important configuration settings that can be used with the tracer.
601601

602+
`test_session.name`
603+
: Use it to identify a group of tests, such as `integration-tests`, `unit-tests` or `smoke-tests`.<br/>
604+
**Environment variable**: `DD_TEST_SESSION_NAME`<br/>
605+
**Default**: (CI job name + test command)<br/>
606+
**Example**: `unit-tests`, `integration-tests`, `smoke-tests`
607+
602608
`service`
603609
: Name of the service or library under test.<br/>
604610
**Environment variable**: `DD_SERVICE`<br/>
@@ -730,7 +736,7 @@ The payload to be published is a dictionary `<string, string|number>` of tags or
730736
When the test start and end channels are in your code, run your testing framework like you normally do, including the following environment variables:
731737

732738
```shell
733-
NODE_OPTIONS="-r dd-trace/ci/init" DD_ENV=ci DD_SERVICE=my-custom-framework-tests yarn run-my-test-framework
739+
NODE_OPTIONS="-r dd-trace/ci/init" DD_TEST_SESSION_NAME=custom-tests yarn run-my-test-framework
734740
```
735741

736742

@@ -795,6 +801,33 @@ forEach([
795801

796802
When you use this approach, both the testing framework and Test Optimization can tell your tests apart.
797803

804+
### Test session name `DD_TEST_SESSION_NAME`
805+
806+
Use `DD_TEST_SESSION_NAME` to define the test session name for your tests (`test_session.name` tag). Use this to identify a group of tests. Examples of values for this tag would be:
807+
808+
- `unit-tests`
809+
- `integration-tests`
810+
- `smoke-tests`
811+
- `flaky-tests`
812+
- `ui-tests`
813+
- `backend-tests`
814+
815+
If `DD_TEST_SESSION_NAME` is not specified, the default value used is a combination of:
816+
817+
- CI job name
818+
- Command used to run the tests (such as `yarn test`)
819+
820+
The test session name should be unique within a repository to help you distinguish different groups of tests.
821+
822+
#### When to use `DD_TEST_SESSION_NAME`
823+
824+
If your tests are run with commands that include a dynamic string, such as:
825+
826+
- `yarn test --temp-dir=/var/folders/t1/rs2htfh55mz9px2j4prmpg_c0000gq/T`
827+
- `pnpm vitest --temp-dir=/var/folders/t1/rs2htfh55mz9px2j4prmpg_c0000gq/T`
828+
829+
The default value for the test session name will be unstable. It is recommended to use `DD_TEST_SESSION_NAME` in this case.
830+
798831
## Further reading
799832

800833
{{< partial name="whats-next/whats-next.html" >}}

Diff for: content/en/tests/troubleshooting/_index.md

+18
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,24 @@ It is likely that the [test case configuration][13] is unstable because one or m
153153

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

156+
## Session history, performance or code coverage tab only show a single execution
157+
158+
This is likely caused by an unstable test session fingerprint. The default value for the test session fingerprint includes the test command, so an unstable test command might cause the test session fingerprint to be unstable. Some examples of unstable test commands include:
159+
160+
- `yarn test --temp-dir=/var/folders/t1/rs2htfh55mz9px2j4prmpg_c0000gq/T`
161+
- `mvn test --temp-dir=/var/folders/t1/rs2htfh55mz9px2j4prmpg_c0000gq/T`
162+
- `bundle exec rspec --temp-dir=/var/folders/t1/rs2htfh55mz9px2j4prmpg_c0000gq/T`
163+
- `dotnet test --results-directory /var/folders/t1/rs2htfh55mz9px2j4prmpg_c0000gq/T`
164+
165+
This can be solved by using the `DD_TEST_SESSION_NAME` environment variable. Use `DD_TEST_SESSION_NAME` to identify a group of tests. Example values for this tag include:
166+
167+
- `unit-tests`
168+
- `integration-tests`
169+
- `smoke-tests`
170+
- `flaky-tests`
171+
- `ui-tests`
172+
- `backend-tests`
173+
156174
## Further reading
157175

158176
{{< partial name="whats-next/whats-next.html" >}}

0 commit comments

Comments
 (0)