How to exclude scenarios

Hi,

I’m trying to exclude one or several scenarios depending on an env parameter sent to my k6 script.

I have a bunch of scenarios testing API:s, now I have added a GUI scenario with k6-browser. I’d like to be able to run the script like this

k6 run –env INCL_GUI_TESTS=“false” myload.js and by that ignore the GUI-scenario in my “scenario list” and only run the API-scenarios. If I run the script without the env-parameter all scenarios should be executed.

Is there any way to do this?

I’ve checked out Advanced Examples | Grafana k6 documentation but it only describes how to run specific cenario/scenarios not how to exclude scenarios. Following that “guide” I have to list all the scenarios in the k6 calls env parameter that I would like to execute instead of just mention the few (or just like in my example a “switch” for GUI scenarios or not GUI scenarios) I’d like to exclude,

I’ve tried to manipulate the options.scenarios list by accessing the GUI-scenario setting its rate to 0, but of course a scenario cannot have 0 as rate so I get an error message regarding that. I also have tried to set the whole GUI-scenario to {} but as there are mandatory attributes to be set in a scenario I get errors.

Thank you very much for some help with this.

Best regards

Lightlore

The best way I have found to exclude scenarios is to have a rate variable for each scenario. Example scenarios. 1. hitFrontEndApi 2. hitBackendApi. Then you would have two rate environment variables for both of those like callsFrontEnd and callsBackEnd and then during your scenario creation, check if that variable is set to 0 (disabling the scenario) and if it use filter your scenarios by scenarios where the call variable is 0<x. Here is an example I whipped up

import http from 'k6/http';
import { check } from 'k6';

// Environment variables for call rates
const callsApiTest = parseInt(__ENV.CALLS_API_TEST) || 5;
const callsDbTest = parseInt(__ENV.CALLS_DB_TEST) || 0;  // This will be disabled
const callsLoadTest = parseInt(__ENV.CALLS_LOAD_TEST) || 10;

// Helper function to create scenarios - returns empty object if calls = 0
function createScenario(name, calls, execFunction, vus = 10, duration = '5m') {
    if (calls === 0) {
        return {};  // Empty object disables the scenario
    }
    
    return {
        [name]: {
            executor: 'constant-arrival-rate',
            exec: execFunction,
            rate: calls,
            timeUnit: '1m',
            duration: duration,
            preAllocatedVUs: vus,
            tags: {
                testType: name
            }
        }
    };
}

// Export options with scenarios
export const options = {
    scenarios: Object.assign(
        {},
        createScenario('apiTest', callsApiTest, 'apiTest', 5, '3m'),
        createScenario('dbTest', callsDbTest, 'dbTest', 3, '3m'),      // Will be disabled since callsDbTest = 0
        createScenario('loadTest', callsLoadTest, 'loadTest', 15, '5m')
    )
};

// Test functions
export function apiTest() {
    const response = http.get('https://httpbin.org/get');
    check(response, {
        'API status is 200': (r) => r.status === 200,
    });
}

export function dbTest() {
    // This won't run because callsDbTest = 0
    const response = http.get('https://httpbin.org/delay/1');
    check(response, {
        'DB status is 200': (r) => r.status === 200,
    });
}

export function loadTest() {
    const response = http.get('https://httpbin.org/uuid');
    check(response, {
        'Load test status is 200': (r) => r.status === 200,
    });
}

Hi,

Thank you very much for your help, looking at your example I achieved what I wanted by writing the following code (only showing important parts):

// start command:

$env:K6_BROWSER_HEADLESS=“true” ; k6 run --env TEST_DURATION=1m --env INCL_GUI_TESTS=“true” .\myLoad.js

// imports

-

-

const testDuration = __ENV.TEST_DURATION;

let includeGuiTests = false;

if(__ENV.INCL_GUI_TESTS == “true”) {

includeGuiTests = true;

}

// Helper function to create scenarios

function createScenario(testType, name, executor, rate, timeUnit, iterations, VUs, preAllocatedVUs, maxVUs, execFunction) {

if (testType == "gui" && includeGuiTests) {

    return {

        \[name\]: {

            duration: testDuration,

            executor: executor,

            vus: VUs,

            options: {

                browser: {

                    type: 'chromium',

                },

            },

            exec: 'guiTestExec',                

            tags: {

                testType: testType

            }

        }

    };      

} else if (testType == "api") {

    return {

        \[name\]: {

            duration: testDuration,

            executor: 'constant-arrival-rate',

            rate: rate,

            timeUnit: timeUnit,

            preAllocatedVUs: preAllocatedVUs,

            maxVUs: maxVUs,

            exec: execFunction,             

            tags: {

                testType: testType

            }

        }

    };      

}

else {

    return {};      

}

}

.

.

.export let options = {

scenarios: Object.assign( {}, createScenario(‘api’, ‘List’, ‘constant-arrival-rate’, 1, ‘1s’,‘’,‘’,5,10,‘listExec’), createScenario(‘api’, ‘Find’, ‘constant-arrival-rate’, ‘1’, ‘1s’,‘’,‘’,‘5’,‘20’,‘findExec’), createScenario(‘api’, ‘Post’, ‘constant-arrival-rate’, ‘1’, ‘1s’,‘’,‘’,‘5’,‘20’,‘postExec’), createScenario(‘gui’, ‘GuiTest’, ‘constant-vus’, ‘’, ‘’,1,1,‘’,‘’,‘guiTestExec’), )

};

.

.

.

.

export function listExec() {

list();

}

export function findExec() {

find();

}

export function postExec() {

post();

}

export async function guiTestExec() {

guiTest();

}