It sounds like you want to use Scenarios.
With it, you can have automation code in separate files from the main entry-point script (i.e. the one you use with k6 run
). You would specify which exported function should be run by the VUs in each scenario using the exec
property. This exported function can then be configured to first call your setup function before calling whichever other functions you want.
For example:
import { test1, test1setup } from "../cypress/tests/PackingProcess.js";
import { test2, test2setup } from "../cypress/tests/Slam.js";
export const options = {
scenarios: {
test1: {
executor: 'constant-vus',
vus: 10,
duration: '10m',
exec: 'test1main'
},
test2: {
executor: 'constant-vus',
vus: 10,
duration: '10m',
exec: 'test2main'
},
}
}
export function test1main() {
test1setup();
test1();
}
export function test2main() {
test2setup();
test2();
}
If what you’ve got is actually two separate k6 scripts that you can run directly (with k6 run
) and you now want to run these side-by-side as part of the same test, then you might need to make some slight changes.
If each file has its own export function setup()
(one of the VU test lifecycle stages) then you’ll need to import these with an alias that allows you to distinguish between them:
import test1, { setup as test1setup } from "./test1.js";
import test2, { setup as test2setup } from "./test2.js";
export const options = {
scenarios: {
test1: {
executor: 'constant-vus',
vus: 1,
duration: '1s',
exec: 'test1main'
},
test2: {
executor: 'constant-vus',
vus: 1,
duration: '1s',
exec: 'test2main'
},
}
}
export function test1main() {
test1setup();
test1();
}
export function test2main() {
test2setup();
test2();
}
The export default function
will also need a name. This is what test1.js
looks like:
export function setup() {
console.log('setup1');
}
export default function test1() {
console.log('test1');
}
This allows you to k6 run test.js
as well as k6 run main.js
when you want to run multiple scenarios in parallel, each with their own VU settings. Do note that with the above, the setup()
function would be called by every VU and not just once which is what the exported setup()
function would have been doing - it might not work as you expect it to.
I may have misunderstood what you are trying to achieve but hopefully this helps a bit nonetheless 