How to get value of --config flag during k6 test run

35 Views Asked by At

I want to add a guard clause to my k6 test to ensure a --config flag has been set.

How can I read the value of the --config flag passed into k6 during a test run?

1

There are 1 best solutions below

0
knittl On

I don't think there is a way to detect that specifically --config was used to set an option, but it is possible to read the final, consolidated options from within your scripts:

import exec from 'k6/execution';

export const options = {
  stages: [
    { duration: '5s', target: 100 },
    { duration: '5s', target: 50 },
  ],
};

export default function () {
  console.log(exec.test.options.scenarios.default.stages[0].target); // 100
}

Reading exec.test.options will show the actual option value used by your script, per order of precedence:

  1. First, k6 uses the option’s default value.
  2. Next, k6 uses the options set in a configuration file via the --config flag.
  3. Then, k6 uses the script value (if set).
  4. After, k6 uses the environment variable (if set).
  5. Finally, k6 takes the value from the CLI flag (if set).

If you want to check whether --config was used, you set a specific option only in this config file (e.g. a tag) and then check the consolidated options for its value.