Guide

How to test code behind feature flags

Every flag doubles the paths through your code. Here is how to test both without depending on a live flag service: in-memory providers, parameterized tests, checks at the edge, and cleaning up with the flag.

Every feature flag doubles the paths through a piece of code: the flag-on path and the flag-off path. Testing code behind feature flags means exercising both deliberately, without letting your tests depend on a live flag service.

Four rules that keep flag tests simple

  1. Tests set flag values themselves. A test that reads whatever production or staging currently says will pass today and fail next week.
  2. No network. Swap the provider for an in-memory one so tests are fast and run offline.
  3. Test both sides. The flag-off path is the one that runs today, and the flag-on path is the one you are about to release.
  4. Keep flag checks at the edge. Evaluate the flag in one place, pass a plain value into the logic, and unit tests need no flag setup at all.

Use an in-memory provider

OpenFeature SDKs ship an in-memory provider: a dictionary of flags that behaves like a real provider. Because your code only talks to the OpenFeature client, swapping providers in a test needs no change to the code under test.

# Python
from openfeature import api
from openfeature.provider.in_memory_provider import InMemoryFlag, InMemoryProvider

def test_new_checkout_flow():
    api.set_provider(InMemoryProvider({
        "new-checkout": InMemoryFlag("on", {"on": True, "off": False}),
    }))
    assert checkout_page().startswith("new")
// Node (server SDK)
import { OpenFeature, InMemoryProvider } from '@openfeature/server-sdk';

OpenFeature.setProvider(new InMemoryProvider({
  'new-checkout': { disabled: false, variants: { on: true, off: false }, defaultVariant: 'on' },
}));

The exact constructor shapes differ between SDK versions, so check the OpenFeature documentation for the version you use. The idea is the same in every language: your test, not a server, decides what each flag returns.

Run both paths

When the flag is a simple boolean, parameterize the test over both values so a change that breaks either side fails the build:

import pytest

@pytest.mark.parametrize("enabled", [True, False])
def test_checkout_totals(enabled):
    set_flag("new-checkout", enabled)
    assert checkout(cart).total == 4990   # same total on both paths

For a refactor behind a flag, the strongest test asserts that both paths give the same result for the same input. That is how you prove the new code is a safe replacement before you widen the rollout.

Keep the flag out of the logic

If a flag check sits deep inside a function, every test of that function has to know about the flag. Move the check to the boundary and pass a value in:

// Hard to test: the flag is read inside the pricing logic
function price(cart) {
  const discounted = flags.getBooleanValue('loyalty-discount', false, ctx);
  ...
}

// Easy to test: the logic takes a plain argument
function price(cart, { loyaltyDiscount }) { ... }
price(cart, { loyaltyDiscount: await flags.getBooleanValue('loyalty-discount', false, ctx) });

Integration and end-to-end tests

  • Pin flags per environment. Run end-to-end tests against an environment where you control the flags, and set the values a test needs at the start. A dedicated test environment avoids surprises from rules written for real users.
  • Use the context. If the flag targets users, create a test user whose attributes match the rule, rather than overriding the flag, so the targeting rule itself is tested.
  • Smoke-test the real provider. One test that connects the real provider to a test environment and evaluates a known flag catches wrong keys and bad configuration before a deploy does.

Test the default path

The default you pass to every check is what runs when the flag system cannot answer. For a kill switch it should be the feature running; for a new feature it should be off. A test that uses a provider returning errors, or no provider at all, proves your code still behaves sensibly when flags are unavailable.

Delete the test with the flag

When a rollout finishes and you delete the flag, delete the flag-off test with it. Tests that keep covering a removed path are noise. Putting the flag name in the test makes it easy to find them again, which also helps with cleanup.

Frequently asked questions

How do you test code that uses feature flags?

Replace the flag provider with an in-memory one in tests, set the exact flag values each test needs, and run the test with the flag on and off. Keep the flag check at the edge of the code and pass a plain value into the logic so unit tests need no flag setup.

Should tests call the real feature flag service?

Not for unit tests. They should set their own flag values and run offline. A single smoke test against a test environment is enough to check that keys and configuration are right.

Do I need to test both the flag-on and the flag-off path?

Yes. The off path is what runs today and the on path is what you are about to release, so a regression in either is a real bug. Parameterizing one test over both values is the cheapest way.

What should happen when the flag service is down?

Your code should use the default value you pass to the check. Test that path too, and choose defaults deliberately: a kill switch defaults to the feature running, and a new feature defaults to off.

Flags you can test and trust

Railflag works through the standard OpenFeature API, so tests swap in an in-memory provider with no code changes.

Start for free