Using LaunchDarkly in Automated Testing featured image

Feature flags enables you to deliver software faster by removing dependencies between teams, reducing the scope of changes to be merged and tested, lessening the number of test environments, and enabling testing in production.

Feature flags can also be a key part of of your automated testing flow. Ideally, adding feature flags gives you another tool to reduce the friction of an automated test and speed up testing so your build process is as fast as possible.

How do you configure your automated tests to use LaunchDarkly?

There are different approaches to using feature flags in testing and specifically in automated testing. The list below will give you a few ideas to how to incorporate feature flags into your tests.

Reading flag values from files

This is our recommended approach for testing with feature flags. With this method you configure your SDK to read from a file instead of connecting to LaunchDarkly. Reading from a file allows you to run tests in a local environment without connecting to an external network.

The SDK configuration can be abstracted further using a wrapper around the SDK which can determine whether flag values will be fixed or in a pre-defined state instead of building a real LDClient. Using wrappers around a LaunchDarkly SDK is a very common practice that provides many benefits such as standardization, extending capabilities using API, and more.

You might use this for testing known common settings, or testing for things that you know will be important to particular users. For example, if you have a client on an older version of Java, you can test so that your changes don’t cause any problems, without needing to create new routing to a test instance every time.

Mocking the LaunchDarkly client

Mocking means creating a fake object for testing the functionality without dependency on external objects. Here's an example. Mocking will allow you to fully control the returned values from flags without any connectivity to LaunchDarkly.

For example, you might mock the responses to a web page as you’re working on it locally, to remove any risk of something accidentally going live.

Using LaunchDarkly API to setup test conditions before the test is run

In this method you will define a set of LaunchDarkly environments that are dedicated to testing, and use the LaunchDarkly API are used to set user targets and flag toggles for these environments.

Using the Relay Proxy in offline mode

With this method, the relay proxy can run as a separate component loading flag values from a file without connecting to LaunchDarkly. While being a more complicated method to implement, the relay offline mode can be used in situations where the team is already considering the use of relay proxy and would like to configure their test environment to mirror production.

As with all integrations, each of these methods requires some effort to integrate feature flags into your testing suite however we believe that the benefits from using feature flags will outbalance the effort.

Finally, If you want to hear more about the benefits of using LaunchDarkly in test automation, watch this Galaxy talk from Northwestern Mutual describing their test automation journey with feature flags.

Related Content

More about Best Practices

January 20, 2022