Before each scenario

Hi Team,

I want to run specific actions only once before every scenario. Is there a way to handle it or specific executors I can use?

The setup() function run only before the test, but for my scenario, I want to run only once for every scenario defined on the test.

User Scenario:
When I run a test for 30 mins, that 30 mins only specific action should run. Some action will run before the scenario and after the scenario.
Example :
Login → Action A → Action B → Logout
without running all 4 actions for 30 mins. I want Login to run only once before each user scenario, and action A and Action B will run for 30 mins at last logout action will happen.

Similar to LoadRunner, Vuser Init run before the scenario, the Action part will run for the defined period, and the last Vuser End part will trigger.
Workaround: We can use a loop for action A and action B, but I want some guidance from the K6 expertise.

This is not something you can easily do with k6 at the moment, sorry. Depending on your specific use case, there might be workarounds you can do with multiple scenarios and the k6/execution API, but it’s not always possible.

It’s something we plan to improve and add to k6 eventually. Hopefully soon, though there are some other prerequisites we have to do first, so no promises. You can follow these issues for updates:

Any of these will make what you want possible, though I am not sure which one we will implement first, so I just listed them all.

1 Like