HTTP Testing and Checks in k6

πŸ“‹ Table of Contents β–Ύ
  1. Making HTTP Calls and Using Checks
  2. Common Mistakes

Most k6 usage focuses on HTTP APIs, and the built-in http module makes it straightforward to send requests and validate responses. Checks, groups and custom metrics help you turn raw responses into meaningful test outcomes.

Making HTTP Calls and Using Checks

The http module supports common methods like GET, POST and PUT, while checks let you assert on status codes, bodies and custom conditions. You can group related requests under named groups to structure your scripts and reports.

import http from 'k6/http';
import { check, group, sleep } from 'k6';

export default function () {
  group('browse products', () => {
    const res = http.get('https://api.example.com/products');
    check(res, {
      'status is 200': (r) => r.status === 200,
      'response time < 800ms': (r) => r.timings.duration < 800,
    });
  });

  sleep(1);
}
Note: Checks contribute to the built-in checks metric, which you can also use in thresholds (for example requiring 99% of checks to pass).
Tip: Use descriptive check names so failures in reports clearly indicate what went wrong.
Warning: Avoid parsing very large response bodies unnecessarily; focus checks on fields that matter for correctness.

Effective use of checks ensures that your performance tests also verify functional behaviour under load.

Common Mistakes

Mistake 1 β€” Skipping checks to β€œfocus only on performance”

This hides failures.

❌ Wrong: Ignoring response status and content.

βœ… Correct: Always assert basic correctness so fast errors do not appear as successes.

Mistake 2 β€” Writing a single giant script with no structure

This hurts maintainability.

❌ Wrong: Putting all logic in one long default function.

βœ… Correct: Use groups and helper functions to organise flows.

🧠 Test Yourself

How do checks improve k6 HTTP tests?