How to run only certain groups of requests

I know that there are multiple open issues on github about being able to run test suit. However, I also find a post in Give example in docs how to only launch a certain group of requests · Issue #584 · grafana/k6 · GitHub that seems it will meet my need to run a group of tests while waiting for better solutions.
Could someone give more details on how to implement the code in issues/584 and how the command line would look like?

Hi @minwu,
I would “expand” on the suggestion by telling you that you can do this not only for groups but for any code, example:

export default function() {
    if (__ENV["part1"]) {
        console.log("this is code of part1");
    }
    if (__ENV["part2"]) {
        console.log("this is code of part2");
    }
}

if you run this with k6 run -e part1=something script.js, you will get

INFO[0000] this is code of part1

you can also run both of them and use env variables such as part2=t k6 run -e part1=somethingelse script.js to get:

INFO[0000] this is code of part1
INFO[0000] this is code of part2

-e just “sets” environmental variables for k6.

I would argue you are probably better of checking for __ENV[part1] == "true" or some other string that you want to expect instead of the my just checking whether there is such not empty variable - setting part1 to “” for example with mine will not execute that code but setting it “false” will as this is just a string with value “false” not the actual boolean false.

Hope this helps :wink:

1 Like

To add on to @mstoykov’s answer and the example in the issue, a small improvement might be defining a single environment variable to specify which groups should be enabled, separated by comma or any other character.

For example:

import { group, sleep } from 'k6';

function mgroup(fn) {
  if (__ENV.K6_GROUPS && __ENV.K6_GROUPS.includes(fn.name)) {
    return group(fn.name, fn);
  }
}

function group1() {
  console.log('running group1');
}

function group2() {
  console.log('running group2');
}

function group3() {
  console.log('running group3');
}

export default function () {
  mgroup(group1);
  mgroup(group2);
  mgroup(group3);
}

Then you could run k6 with K6_GROUPS=group1,group3 k6 run script.js and only group1 and group3 would be executed. This is an alternative to the run -e option that works for any shell command.

2 Likes