Skip to main content

Write your first test

k6 tests are written using JavaScript, making them accessible to developers and easy to integrate into existing projects. In this guide, you’ll learn the basic structure of a k6 test and create your first load test script.

Prerequisites

Before you start:
  • Basic JavaScript knowledge: Familiarity with JavaScript or TypeScript syntax
  • k6 installed: Follow the installation guide if you haven’t already
  • Code editor: VS Code, JetBrains, or any text editor
If you’re unfamiliar with JavaScript, check out k6 Studio to generate tests without writing code.

Basic structure of a k6 test

Every k6 script follows a common structure with these core components:
1

Imports

Import k6 modules or JavaScript libraries to extend functionality:
2

Options (optional)

Configure test execution settings like virtual users and duration:
3

Default function

The main test logic that k6 executes repeatedly:
4

Lifecycle functions (optional)

Code that runs before or after the test:

Create your first test

Let’s create a simple test that makes 10 HTTP GET requests with a 1-second delay between requests.
1

Create a test file

Create a new file named my-first-test.js:
Or use k6’s built-in command to generate a template:
2

Import k6 modules

Add imports at the top of your file:
3

Configure test options

Define how many times the test should run:
4

Write the default function

Add the main test logic:
5

Run your test

Execute the test from your terminal:
You’ll see output showing the test progress and results.

Add checks and validations

Checks allow you to validate responses and track success rates. They don’t stop test execution but provide pass/fail statistics.
Checks are reported in the test summary and show the percentage of passing validations.

Run tests with virtual users

Instead of iterations, you can configure tests to run with multiple virtual users (VUs) for a specified duration.

Using command-line options

This runs the test with 10 virtual users for 30 seconds.

Using script options

Each virtual user runs the default function in a loop for the specified duration. This simulates concurrent users accessing your application.

Ramp up traffic with stages

For more realistic traffic patterns, gradually increase and decrease the number of virtual users:
This creates a more realistic load pattern that gradually increases, maintains, and decreases traffic.

Understanding the init context

Code outside the default function runs once per VU during initialization. Use this for:
  • Loading data from files
  • Defining reusable variables
  • Configuring constants
Avoid expensive operations in the init context that run for every VU. Use setup() for operations that should run only once for the entire test.

Use environment variables

Make your tests flexible by using environment variables:
Run with custom environment variables:

Extending your script

Now that you understand the basics, explore these capabilities:

Multiple requests

Add multiple http.get() or http.post() requests to simulate complex user flows

Thresholds

Set performance requirements with thresholds that fail tests if not met

Custom metrics

Track business-specific metrics using Counter, Gauge, Rate, and Trend

Browser tests

Simulate user interactions with the k6 browser module

Example: Complete test script

Here’s a more complete example with checks, thresholds, and custom metrics:

Next steps

You’ve created your first k6 test! Continue learning:

Understanding Results

Learn how to read and analyze k6 test results

Using k6 Options

Explore all available configuration options

HTTP Requests

Master making HTTP requests with k6

Test Examples

Browse real-world k6 test examples