Skip to content
TestLine Menu
TestLine
QA process

Feature flags and testing: how to test features hidden behind a flag

What to test for every flag, how to set flags in automated tests, how to limit the number of combinations and why old flags are technical debt.

author
Artur Machowski, Senior QA Engineer
published
reading
5 min

Short answer

You test a feature behind a feature flag in both flag states: on, to check the new feature, and off, to check that the old path works as before. Add three things that are easy to forget: switching the flag during a user’s session, behaviour when the flag service doesn’t respond, and gradual rollout to part of the users. Don’t test every combination of flags, only those that affect the same feature. Remove flags once fully rolled out, because every old flag is another combination to test.

What to test for every flag

scenariowhat to check
flag onthe new feature against its acceptance criteria
flag offthe old path unchanged, no traces of the new feature in the UI or API
switched during a sessiona user halfway through a process doesn’t lose data or see an error
flag service unavailablethe app uses a safe default value
rollout to part of the usersthe right people see the feature, others don’t; the same person sees the same thing on every visit
data created with the flag onstill works after the flag is turned off

The last point is the one most often skipped. If a customer created something in the new feature and the flag is turned off, their data must not disappear or break the old path.

Flag combinations

With 10 flags there are 1,024 combinations. Testing them all makes no sense. Sensible rules:

  • each flag in both states, with the others set as in production;
  • combinations only for flags that affect the same path, e.g. a new cart and a new payment flow;
  • a list of current flags with an owner and a planned removal date.

Flags in automated tests

Each test sets the flags it needs instead of relying on the state of the environment. An example in Playwright, where the app reads flags from a cookie that only works on staging:

test("new cart: discount code", async ({ page, context }) => {
  await context.addCookies([
    { name: "ff_new_cart", value: "on", url: process.env.BASE_URL! },
  ]);
  await page.goto("/cart");
  // ... new cart scenario
});

It’s worth running the same suite in CI for both states of the flags currently being rolled out. When a flag is removed, you remove that test variant too.

Old flags are debt

A flag that has been on for everyone for six months is dead code and an extra path that someone will one day switch off by accident. Rules:

  • every flag has an owner and a removal date;
  • after full rollout and 2-4 weeks of stability, the flag is removed;
  • removing a flag is a normal sprint task, with a test after removal.

Feature flags are one of the things that let you release often without being blocked by testing. More in how to test with weekly releases.

How I can help

As part of QA support I test features behind flags at your release pace, and as part of test automation I set up E2E tests so they switch flags themselves. Describe how you use flags in the contact form.

Frequently asked questions

How do I test a feature behind a feature flag?

In both states: with the flag on you check the new feature, with it off you check that the old path works as before. Add switching the flag while the app is in use and the behaviour when the flag service doesn't respond.

Do I need to test every combination of flags?

No. With 10 flags there are over a thousand combinations. Test each flag in both states with the others in their production state, and combinations only for flags that affect the same feature or path.

How do I set flags in automated tests?

Through the flag service API, a header, a cookie or a parameter available only in the test environment. Each test sets the flags it needs instead of relying on the global state of the environment.

What happens when the flag service is down?

The app should fall back to default values, usually the safe ones, i.e. the feature turned off. It's worth testing this by blocking the connection to the flag service in the test environment.

When should feature flags be removed?

When the feature is on for everyone and stable, usually after 2-4 weeks. Every old flag is an extra path in the code and an extra combination to test.

Got a release in a few weeks?

Describe your project in the form. I'll reply with questions about scope or an initial test plan.