Checks are like asserts but differ in that they don't halt the execution, instead, they just store the result of the check, pass or fail, and let the script execution continue. Take a look at thresholds for a way to halt the execution. Checks are great for codifying assertions relating to HTTP requests/responses, making sure the response code is 2xx for example:
In the above example, one check was specified but you can add as many as you need in a call to check(). When the above script is run you can see how k6 displays the results of the check calls in the following output:
In the output above you can see that our check "is status 200" was successful 100% of the times it was called.
You may also add multiple checks within a single check() statement, like this:
One important thing to understand regarding checks is that a failed check will not fail the whole load test.
Checks help to keep your code organized and easy to read, but when you're running a load test in a CI test suite you may want to check for error conditions that fail the whole load test. In this case you may want to combine checks with thresholds to get what you want:
The above script declares a custom Rate metric (called "errors") to hold information about the errors we have seen during the test, then it uses a threshold on that custom metric to fail the test when it encounters too many errors. If we replace the "http://httpbin.org" URL with one that will generate an error, k6 will exit with a nonzero exit value, indicating a FAIL result to e.g. a CI system that may have executed it:
As you can see above, the exit code generated by k6 after this run was 99. Any nonzero exit code is commonly interpreted by Un*x shells, CI servers, and monitoring systems as a "failure".
Note also that we use the return value of the check() to decide whether to increment our error rate. When any one of the check conditions inside a check() call fails, check() returns false, which will cause the error rate to be incremented. Only if all check conditions pass will check() return true.
See check() in the script API reference for more details on how check() works.
Here we can quickly see what checks are failing, and upon clicking on any check, 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.