Testing with Feature Flags
What you'll learn: How to test both enabled and disabled feature flag states to ensure all code paths work correctly.
The Challenge of Dual Realities
Feature flags create multiple "versions" of your application running in the same codebase. When a flag is on, one code path executes; when it's off, another does. This means you can't just test the "happy path" anymore — you must verify that both realities work.
Think of it like testing a light switch: you need to confirm the room works both with lights on and with lights off. Neither state should cause the room to catch fire.
Testing Both States
Your test suite must cover:
1. Flag ON tests
Verify the new feature works as expected when enabled:
def test_new_checkout_flow_enabled():
with feature_flag('new_checkout', enabled=True):
result = process_checkout(cart)
assert result.uses_new_flow == True
assert result.payment_successful
2. Flag OFF tests
Verify the old code path still works when disabled:
def test_old_checkout_flow_still_works():
with feature_flag('new_checkout', enabled=False):
result = process_checkout(cart)
assert result.uses_legacy_flow == True
assert result.payment_successful
3. Toggle tests
Test that switching the flag mid-session doesn't break things (when applicable).
Integration and End-to-End Testing
Don't forget to test flags in realistic scenarios with targeting rules, user segments, and interactions with other flags. Your CI pipeline should run tests for critical combinations of flag states.
Key Takeaway: Testing with feature flags requires running tests for both ON and OFF states of each flag, ensuring all code paths remain functional regardless of flag configuration.