Write your first k6 test script
Create a small k6 script that calls QuickPizza, checks the response, and pauses between iterations. The next milestone runs it locally.
Generate a test script with k6 new
Use k6 new to create a starter script.
Run the following command in a directory on your machine (for example, a new folder for this journey, or any existing project):
k6 new script.jsThis creates script.js in the current directory. You can use another name, but keep the .js or .ts extension.
Tip
k6 newsupports different templates for different use cases. The defaultminimaltemplate is a great starting point. To learn more, refer to Create a test script using the CLI.
Customize the script
Open script.js in your code editor and replace the contents with:
import http from 'k6/http';
import { check, sleep } from 'k6';
export const options = {
iterations: 10,
};
export default function () {
const res = http.get('https://quickpizza.grafana.com');
check(res, { 'status was 200': (r) => r.status === 200 });
sleep(1);
}Save the file. The script has three parts:
- Imports: The
httpmodule enables you to make HTTP requests, whilecheckvalidates responses andsleepintroduces delays between iterations to simulate real-world usage. - Options: The
iterations: 10configuration tells k6 to execute the default function 10 times. - Default function: This is the entry point for your test. Each iteration sends a GET request to the QuickPizza demo app, verifies the response status is 200, and then pauses for 1 second.
If you encounter script issues, refer to the k6 documentation on writing tests.
In the next milestone, you’ll run this test script locally to see k6 in action and view the test results.