• Home
  • Integrations
  • SDKs
  • Guides
  • API docs
No results for ""
EXPAND ALL

EDIT ON GITHUB

Test data sources

Read time: 2 minutes
Last edited: Oct 26, 2021

Overview

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

Using 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 server-side 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 users, or set up more complicated behavior similar to the ways you can configure a flag in the LaunchDarkly dashboard. This can be useful in testing application code that expects flags to be different for different users.

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

Server-side SDKs

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

  • .NET (server-side)
  • Go
  • Java

.NET (server-side)

To configure the SDK to use a test data source:

1using LaunchDarkly.Sdk.Server.Integrations;
2
3var td = TestData.DataSource();
4// You can set any initial flag states here with td.Update
5
6var config = Configuration.Builder("sdk-key")
7 .DataSource(td)
8 .Build();
9var client = new LdClient(config);

To set a flag to a specific value:

1td.Update(td.Flag("flag-key-1").VariationForAllUsers(false);

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

1// This flag is true for the user key "some-user-key" and false for everyone else
2td.Update(td.Flag("flag-key-2")
3 .VariationForUser("some-user-key", true)
4 .FallthroughVariation(false));
5
6// This flag returns the string variation "green" for users who have the custom
7// attribute "admin" with a value of true, and "red" for everyone else.
8td.Update(td.Flag("flag-key-3")
9 .Variations(LdValue.Of("red"), LdValue.Of("green"))
10 .FallthroughVariation(0)
11 .IfMatch(UserAttribute.ForName("admin"), LdValue.Of(true))
12 .ThenReturn(1));

To learn more, read TestData.

Go

To configure the SDK to use a test data source:

1import (
2 ld "gopkg.in/launchdarkly/go-server-sdk.v5"
3 "gopkg.in/launchdarkly/go-server-sdk.v5/testhelpers/ldtestdata"
4 "gopkg.in/launchdarkly/go-sdk-common.v2/lduser"
5 "gopkg.in/launchdarkly/go-sdk-common.v2/ldvalue"
6)
7
8td := ldtestdata.DataSource()
9// You can set any initial flag states here with td.Update
10
11config := ld.Config{
12 DataSource: td,
13}
14client := ld.MakeCustomClient("sdk-key", config, 0)

To set a flag to a specific value:

1td.Update(td.Flag("flag-key-1").VariationForAllUsers(false))

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

1// This flag is true for the user key "some-user-key" and false for everyone else
2td.Update(td.Flag("flag-key-2").
3 VariationForUser("some-user-key", true).
4 FallthroughVariation(false));
5
6// This flag returns the string variation "green" for users who have the custom
7// attribute "admin" with a value of true, and "red" for everyone else.
8td.Update(td.Flag("flag-key-3").
9 Variations(ldvalue.String("red"), ldvalue.String("green")).
10 FallthroughVariationIndex(0).
11 IfMatch(lduser.UserAttribute("admin"), ldvalue.Bool(true)).
12 ThenReturnIndex(1));

To learn more, read ldtestdata.

Java

To configure the SDK to use a test data source:

1using com.launchdarkly.sdk.*;
2using com.launchdarkly.sdk.server.*;
3using com.launchdarkly.sdk.server.integrations.*;
4
5TestData td = TestData.dataSource();
6// You can set any initial flag states here with td.update
7
8LDConfig config = new LDConfig.Builder()
9 .dataSource(td)
10 .build();
11LDClient client = new LDClient(sdkKey, config);

To set a flag to a specific value:

1td.update(td.flag("flag-key-1").variationForAllUsers(false));

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

1// This flag is true for the user key "some-user-key" and false for everyone else
2td.update(td.flag("flag-key-2")
3 .variationForUser("some-user-key", true)
4 .fallthroughVariation(false));
5
6// This flag returns the string variation "green" for users who have the custom
7// attribute "admin" with a value of true, and "red" for everyone else.
8td.update(td.flag("flag-key-3")
9 .variations(LDValue.of("red"), LDValue.of("green"))
10 .fallthroughVariation(0)
11 .ifMatch(UserAttribute.forName("admin"), LDValue.of(true))
12 .thenReturn(1));

To learn more, read TestData.