Testing with feature flags

To run a specific test with a feature flag enabled you can use the QA::Runtime::Feature class to enable and disable feature flags (via the API).

Note that administrator authorization is required to change feature flags. QA::Runtime::Feature automatically authenticates as an administrator as long as you provide an appropriate access token via GITLAB_QA_ADMIN_ACCESS_TOKEN (recommended), or provide GITLAB_ADMIN_USERNAME and GITLAB_ADMIN_PASSWORD.

Please be sure to include the tag :requires_admin so that the test can be skipped in environments where admin access is not available.

cautionYou are strongly advised to enable feature flags only for a group, project, user, or feature group. This makes it possible to test a feature in a shared environment without affecting other users.

For example, the code below would enable a feature flag named :feature_flag_name for the project created by the test:

RSpec.describe "with feature flag enabled", :requires_admin do
  let(:project) { Resource::Project.fabricate_via_api! }

  before do
    Runtime::Feature.enable(:feature_flag_name, project: project)

  it "feature flag test" do
    # Execute the test with the feature flag enabled.
    # It will only affect the project created in this test.

  after do
    Runtime::Feature.disable(:feature_flag_name, project: project)

Note that the enable and disable methods first set the flag and then check that the updated value is returned by the API.

Similarly, you can enable a feature for a group, user, or feature group:

group = Resource::Group.fabricate_via_api!
Runtime::Feature.enable(:feature_flag_name, group: group)

user = Resource::User.fabricate_via_api!
Runtime::Feature.enable(:feature_flag_name, user: user)

feature_group = "a_feature_group"
Runtime::Feature.enable(:feature_flag_name, feature_group: feature_group)

If no scope is provided, the feature flag is set instance-wide:

# This will affect all users!

Running a scenario with a feature flag enabled

It’s also possible to run an entire scenario with a feature flag enabled, without having to edit existing tests or write new ones.

Please see the QA README for details.