In certain use cases information about the current test execution state inside your test scripts can be really useful.
The k6/execution module exposes various details about the current execution state, such as the name of the currently executed scenario or how many VUs are currently active and many more. The module provides test execution information via three properties:
|instance||Meta information and execution details on the currently running k6 instance|
|scenario||Meta information and execution details about the current running scenario|
|vu||Meta information and execution details about the current vu and iteration|
k6 v0.34.0 introduced k6/execution module. If you are using an earlier version of k6, where the module is not available, refer to __VU and __ITER section.
_VU and _ITER (discouraged)
⚠️ __VU and __ITER are both global variables with execution context information that k6 makes available to the test script.
A numeric counter with the current iteration number for a specific VU. Zero-based.
Current VU number in use. The value is assigned incrementally for each new VU instance, starting from one. The variable will be 0 while executing the setup and teardown functions.
Different test behaviors and parameterizations can be accomplished by making use of the execution context variables. A typical use case would be a load test simulating different users performing a login flow.
If you're running tests in k6 Cloud you will also have additional environment variables that will tell you on which server, load zone and distribution of the test you are currently executing. You can find more details and examples here.