No results for

Powered byAlgolia

What is a check?

Checks are like assertions, but differ in that they don't halt the execution. Instead, they 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 of a test based on checks.

Check for HTTP response code returned

Checks are great for codifying assertions relating to HTTP requests/responses, such as making sure the response code is an HTTP 2xx:

1import { check } from 'k6';
2import http from 'k6/http';
3
4export default function () {
5 const res = http.get('http://test.k6.io/');
6 check(res, {
7 'is status 200': (r) => r.status === 200,
8 });
9}

Check for text in response body returned

Sometimes, even an HTTP 200 response can contain an error message. In these situations, consider adding a check to verify the response body, like this:

1import { check } from 'k6';
2import http from 'k6/http';
3
4export default function () {
5 const res = http.get('http://test.k6.io/');
6 check(res, {
7 'verify homepage text': (r) =>
8 r.body.includes('Collection of simple web-pages suitable for load testing'),
9 });
10}

Check for response body size

If you want to verify that the response to a request that k6 sends is of a certain size, you can use a check for that like this:

1import { check } from 'k6';
2import http from 'k6/http';
3
4export default function () {
5 const res = http.get('http://test.k6.io/');
6 check(res, {
7 'body size is 11,105 bytes': (r) => r.body.length == 11105,
8 });
9}

Check output

When you run a script including checks, you can see the outcome of the check calls in the following output:

$ k6 run script.js
...
✓ is status 200
...
checks.........................: 100.00% ✓ 10
data_received..................: 11 kB 12 kB/s

In the output above you can see that our check "is status 200" was successful 100% of the times it was called.

Adding multiple checks

You may also add multiple checks within a single check() statement, like this:

1import { check } from 'k6';
2import http from 'k6/http';
3
4export default function () {
5 const res = http.get('http://test.k6.io/');
6 check(res, {
7 'is status 200': (r) => r.status === 200,
8 'body size is 11,105 bytes': (r) => r.body.length == 11105,
9 });
10}

When this test is executed, the output will look like this:

$ k6 run checks.js
...
✓ is status 200
✓ body size is 11,105 bytes
...
checks.........................: 100.00% ✓ 20
data_received..................: 11 kB 20 kB/s

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.

Checks in k6 Cloud Results

In k6 Cloud Results Checks are available in their own tab for analysis.

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.

k6 Cloud Checks Tab

See also