No results for ""
EXPAND ALL
  • Home
  • API docs

Test data sources

Read time: 14 minutes
Last edited: Aug 16, 2024

Overview

This topic explains how to use the test data source feature for more control over your LaunchDarkly SDKs during testing.

Newer versions of LaunchDarkly SDKs replace users with contexts

A context is a generalized way of referring to the people, services, machines, or other resources that encounter feature flags in your product. Contexts replace another data object in LaunchDarkly: "users." To learn more, read Contexts.

Creating contexts and evaluating flags based on them is supported in the latest major versions of most of our SDKs. For these SDKs, the code samples on this page include the two most recent versions.

About test data sources

LaunchDarkly SDKs must have a data source when they initialize. Usually, this data source is connected to LaunchDarkly. However, when you write unit tests, using a test data source can give you more control over your SDK.

LaunchDarkly supports a test data source for some SDKs. The test data source allows you to mock the behavior of a LaunchDarkly SDK so it has predictable behavior when evaluating flags.

Use the test data source for testing only

The test data source is intended strictly for testing purposes. It cannot be used in production.

You can set a flag's initial state by calling the data source's update method before or after starting the client. When you start the client, it acts as if it has connected to LaunchDarkly and received all of the test data that you have configured. Subsequent calls to update behave as if LaunchDarkly sent an update over a streaming connection.

You can modify targeting rules at any time, including when the SDK is already in use. This allows you to simulate a situation where a feature flag changes values. When you set a flag's state in the test data source, you can either set it to return a single variation for all contexts or users, or set up more complicated behavior similar to the ways you can configure a flag in the Flags list. This can be useful in testing application code that expects flags to be different for different contexts or users.

Details about each SDK's configuration are available in the SDK-specific sections below.

Client-side SDKs

This feature is available in the following client-side SDKs:

  • .NET (client-side)
  • Android

.NET (client-side)

Expand .NET (client-side) code sample

To configure the SDK to use a test data source:

To set a flag to a specific value:

There are other ways you can configure flag behavior using a test data source. Here is an example:

To learn more, read TestData.

Android

Expand Android code sample

This feature is available in versions 4.0 and higher of the Android SDK.

To configure the SDK to use a test data source:

To set a flag to a specific value:

There are other ways you can configure flag behavior using a test data source. Here is an example:

To learn more, read TestData.

Server-side SDKs

This feature is available in the following server-side SDKs:

.NET (server-side)

Expand .NET (server-side) code sample

To configure the SDK to use a test data source:

To set a flag to a specific value:

There are other ways you can configure flag behavior using a test data source. Here is an example:

To learn more, read TestData.

C++ (server-side)

Expand C++ (server-side) code sample

This feature is only available in v2.x of the C++ (server-side) SDK.

To configure the SDK to use a test data source:

Once you are finished using the client, ensure the test data source is freed:

To set a flag to a specific value:

There are other ways you can configure flag behavior using a test data source. Here is an example:

Erlang

Expand Erlang code sample

To configure the SDK to use a test data source:

To set a flag to a specific value:

There are other ways you can configure flag behavior using a test data source. Here is an example:

Go

Expand Go code sample

To configure the SDK to use a test data source:

To set a flag to a specific value:

There are other ways you can configure flag behavior using a test data source. Here is an example:

To learn more, read ldtestdata.

Haskell

Expand Haskell code sample

To configure the SDK to use a test data source:

To set a flag to a specific value:

There are other ways you can configure flag behavior using a test data source. Here is an example:

Java

Expand Java code sample

To configure the SDK to use a test data source:

To set a flag to a specific value:

There are other ways you can configure flag behavior using a test data source. Here is an example:

To learn more, read TestData.

Node.js (server-side)

Expand Node.js (server-side) code sample

To configure the SDK to use a test data source:

To learn more, read TestData.

PHP

Expand PHP code sample

To configure the SDK to use a test data source:

To set a flag to a specific value:

There are other ways you can configure flag behavior using a test data source. Here is an example:

To learn more, read TestData.

Python

Expand Python code sample

To configure the SDK to use a test data source:

To set a flag to a specific value:

There are other ways you can configure flag behavior using a test data source. Here is an example:

To learn more, read TestData.

Ruby

Expand Ruby code sample

To configure the SDK to use a test data source:

To set a flag to a specific value:

There are other ways you can configure flag behavior using a test data source. Here is an example:

To learn more, read TestData.