Search code examples
load-testingk6

How to stop duplicating scripts in K6?


I have to write like 20 different scripts in K6 for an application. And most of these scripts contains common functionalities like login, choose some options, etc...

So is there a better way to write K6 scripts without duplicating these common functionalities? Can we implement common methods in somewhere and execute it inside the default function or something similar to that?


Solution

  • You can write your own module contains common functionalities then import them:

    $ cat index.js
    import { hello_world } from './modules/module.js';
    
    export default function() {
        hello_world();
    }
    $ cat module.js
    export function hello_world() {
        console.log("Hello world");
    }
    

    You can read here for more details.