Add checks to your script
A fast response isn’t useful if it returns an error. Checks let you validate that responses are correct, not just quick, by asserting conditions on every response. Unlike traditional test assertions, a failed check doesn’t stop the test. Instead, k6 tracks the pass rate so you can see how often correctness degrades under load.
This is a critical step that many new users skip. Without checks, your baseline only measures speed. With checks, it also measures correctness, giving you a complete picture of system health.
To add checks to your baseline script, complete the following steps:
Open
baseline.jsand update the import statement to includecheck:import http from 'k6/http'; import { check, sleep } from 'k6';In the default function, assign the HTTP response to a variable and add checks after the request:
export default function () { const res = http.get('https://quickpizza.grafana.com'); check(res, { 'status is 200': (r) => r.status === 200, 'response body is not empty': (r) => r.body.length > 0, }); sleep(1); }Each check has a descriptive name and a function that returns
trueorfalse. You can add as many checks as you need. Common checks include status codes, response body content, and response headers.Save the file. Your complete
baseline.jsshould now look like this:import http from 'k6/http'; import { check, sleep } from 'k6'; export const options = { stages: [ { duration: '30s', target: 20 }, { duration: '1m', target: 20 }, { duration: '30s', target: 0 }, ], }; export default function () { const res = http.get('https://quickpizza.grafana.com'); check(res, { 'status is 200': (r) => r.status === 200, 'response body is not empty': (r) => r.body.length > 0, }); sleep(1); }When you run this test, the summary output includes a
checksline showing the total pass rate across all check assertions.
In the next milestone, you run this test and learn how to read the results.
At this point in your path, you can explore the following topics: