No results for

Powered byAlgolia

Checks validate boolean conditions in your test. Testers often use checks to validate that the system is responding with the expected content. For example, a check could validate that a POST request has a response.status == 201, or that the body is of a certain size.

Checks are similar to what many testing frameworks call an assert, but failed checks do not cause the test to abort or finish with a failed status. Instead, k6 keeps track of the rate of failed checks as the test continues to run

Each check creates a rate metric. To make a check abort or fail a test, you can combine it with a Threshold.

Check for HTTP response code

Checks are great for codifying assertions relating to HTTP requests and responses. For example, this snippet makes sure the HTTP response code is a 200:

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

Sometimes, even an HTTP 200 response contains 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

To verify the size of the response body, you can use a check 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}

See percentage of checks that passed

When a script includes checks, the summary report shows how many of the tests' checks passed:

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

In this example, note that the check "is status 200" succeeded 100% of the times it was called.

Add multiple checks

You can also add multiple checks within a single check() statement:

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 executes, the output will look something 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.

Read more