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:

  1. Open baseline.js and update the import statement to include check:

    JavaScript
    import http from 'k6/http';
    import { check, sleep } from 'k6';
  2. In the default function, assign the HTTP response to a variable and add checks after the request:

    JavaScript
    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 true or false. You can add as many checks as you need. Common checks include status codes, response body content, and response headers.

  3. Save the file. Your complete baseline.js should now look like this:

    JavaScript
    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 checks line showing the total pass rate across all check assertions.

In the next milestone, you run this test and learn how to read the results.

More to explore (optional)

At this point in your path, you can explore the following topics:

Checks

Inspect checks in Grafana Cloud k6


page 4 of 10