---
title: Add Custom Measures To Your Tests
description: Learn how to use custom measures in your tests.
breadcrumbs: >-
  Docs > Test Optimization in Datadog > Test Optimization Guides > Add Custom
  Measures To Your Tests
---

# Add Custom Measures To Your Tests

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

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

{% /callout %}

## Overview{% #overview %}

Before you begin, make sure that [Test Optimization](https://docs.datadoghq.com/tests/) is already set up for your language. This guide walks you through adding and using custom measures for your tests.

## Add the custom measure to your test{% #add-the-custom-measure-to-your-test %}

Add the custom measure to your test. The native instrumentation allows you to use the programmatic API:

{% tab title="JavaScript/TypeScript" %}

```javascript
  it('sum function can sum', () => {
    const testSpan = require('dd-trace').scope().active()
    testSpan.setTag('test.memory.rss', process.memoryUsage().rss)
    // test continues normally
    // ...
  })
```

{% /tab %}

{% tab title="Java" %}
To add custom metrics, include the [`opentracing-util`](https://mvnrepository.com/artifact/io.opentracing/opentracing-util) library as a compile-time dependency to your project.

```java
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
// ...
```

{% /tab %}

{% tab title="Python" %}

```python
from ddtrace import tracer
import os, psutil

# Declare `ddspan` as argument to your test
def test_simple_case(ddspan):
    # Set your tags
    process = psutil.Process()
    ddspan.set_tag("test.memory.rss", process.memory_info().rss)
    # test continues normally
    # ...
```

{% /tab %}

{% tab title=".NET" %}

```csharp
// inside your test
var scope = Tracer.Instance.ActiveScope; // from Datadog.Trace;
if (scope != null) {
    scope.Span.SetTag("test.memory.usage", 1e8);
}
// test continues normally
// ...
```

{% /tab %}

{% tab title="Ruby" %}

```ruby
require 'datadog/ci'

# inside your test
Datadog::CI.active_test&.set_tag('test.memory.usage', 1e8)
# test continues normally
# ...
```

{% /tab %}

{% tab title="Swift" %}

```swift
import DatadogSDKTesting

// inside your test
DDTest.current?.setTag(key: "test.memory.usage", value: 1e8)
// test continues normally
// ...
```

{% /tab %}

{% tab title="JUnit Report Uploads" %}
For `datadog-ci`, use the `DD_MEASURES` environment variable or `--measures` CLI argument:

```gdscript3
DD_MEASURES="test.memory.usage:1000" datadog-ci junit upload --service my-service --measures test.request.rate:30 report.xml
```

{% /tab %}

## Create a facet{% #create-a-facet %}

Create a facet for the custom measure you added to the test by navigating to the [**Test Runs** page](https://app.datadoghq.com/ci/test-runs) and clicking **+ Add** on the facet list.

{% image
   source="https://datadog-docs.imgix.net/images/continuous_integration/facet_creation.572ac594319dc73205c68e32e84b8950.png?auto=format"
   alt="" /%}

Make sure that the type of facet is **Measure**, which represents a numerical value:

{% image
   source="https://datadog-docs.imgix.net/images/continuous_integration/measure_creation.bc39e65cc7eebdf41980cb703674e453.png?auto=format"
   alt="" /%}

Click **Add** to start using your custom measure.

## Graph the evolution of your measure{% #graph-the-evolution-of-your-measure %}

Plot the evolution of your measure across time by selecting the **Timeseries** visualization:

{% image
   source="https://datadog-docs.imgix.net/images/continuous_integration/plot_measure.a261714f6e32d554623cd88e12b01ae7.png?auto=format"
   alt="" /%}

For example, you can use this visualization to track the evolution of the memory usage in your tests.

## Export your graph{% #export-your-graph %}

You can export your graph to a [dashboard](https://docs.datadoghq.com/dashboards) or a [notebook](https://docs.datadoghq.com/notebooks), and create a [monitor](https://docs.datadoghq.com/monitors) based on it by clicking the **Export** button.

{% image
   source="https://datadog-docs.imgix.net/images/continuous_integration/export_measure.f4d1e591eeb0da11868b8dbd9bdd7a21.png?auto=format"
   alt="" /%}

## Add a monitor{% #add-a-monitor %}

Get alerted if the value of your measure goes above or below a certain threshold by creating a [CI Tests Monitor](https://app.datadoghq.com/monitors/create/ci-tests).

{% image
   source="https://datadog-docs.imgix.net/images/continuous_integration/monitor_measure.22772a7a5e6852c5bfdad0da993bf0dd.png?auto=format"
   alt="" /%}

For example, you can use this type of alert to inform you about the memory usage reaching a certain threshold.

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

- [Learn about Test Optimization](https://docs.datadoghq.com/tests)
- [Learn about CI Monitors](https://docs.datadoghq.com/monitors/types/ci)
