Checks are like assertions, but they don't halt execution. Instead, they store the result of the check, pass or fail, and let the script continue. If you want to halt execution, take a look at thresholds (which can include checks).
The following sections show some ways how you can use checks.
Checks are great for codifying assertions relating to HTTP requests and responses. For example, this snippet makes sure the HTTP response code is a 2xx:
Sometimes, even an HTTP 200 response contains an error message. In these situations, consider adding a check to verify the response body, like this:
To verify the size of the response body, you can use a check like this:
When a script includes checks, the summary report shows how many of the tests' checks passed:
In this example, note that the check "is status 200" succeeded 100% of the times it was called.
You may also add multiple checks within a single check() statement:
When this test executes, the output will look something like this:
About Failing Checks
When a check fails, the script will continue executing successfully and will not return a 'failed' exit status. If you need the whole test to fail based on the results of a check, you have to combine checks with thresholds. This is particularly useful in specific contexts, such as integrating k6 into your CI pipelines or receiving alerts when scheduling your performance tests.
Here we can quickly see what checks are failing. When you select a check, you can see the count of passes/failures at given points in the test. You can also add the check to the analysis tab, for further comparison with other metrics.