While there is an code editor built into the k6 web application, it has limited debugging abilities. Using k6 locally, you can actually execute your test scripts on a small scale to quickly see how the script executes.
Building on tip 1 above, you want to avoid streaming your results to our cloud using -o cloud and avoid running the test using the cloud service. Debugging locally is beneficial for two reasons:
- Tests that run in or stream to our cloud will count against any limits you may have
- Execution is slower when streaming or executing in the cloud. We want debugging to be a fast iterative process.
When debugging, you'll likely be making many changes as you work through your scripts to ensure they work as expected. The data sent or run from the cloud won't be of much value, so just keep it local until you need to run a larger test.
It's likely that you've configured Virtual Users and/or duration in your script. adding the flags -i 1 -u 1 will instruct k6 to execute 1 iteration with 1 Virtual User.
Note: 1 Virtual User and 1 iteration is also the default execution for k6. If you have not defined any VUs or iterations in your test, k6 will execute with 1.
Sometimes you need to understand more details about the requests being sent and response received. Using --http-debug as a flag allows you to do just that. You can also print full response bodies by using --http-debug="full"
Note: If your test script has a large number of HTTP requests, this will produce a large output.
Sometimes it's just easier to print some information to the terminal window. Feel free to use console.log(); to print useful information to the terminal window. Perhaps you want to examine some JSON returned in a response, a specific response body, or even just know if you've correctly entered/exited loops or IF statements.
To take the above a step further, consider the following snippet of code. We are making a GET request, saving the response to res and then logging the complete response object. Now we can examine it to find exactly what we may be looking for to adapt our test script.