Introduction

K6 is a powerful self-hosted load testing tool that can be run in Docker and configured to simulate API requests, including authentication, POST requests, and JSON payloads.

This guide provides a step-by-step tutorial on setting up K6 using Docker Compose and gradually moving from a simple request to a more complex Postman-like simulation.

Step 1, Install Docker and Docker Compose

Before running K6 in Docker, ensure that Docker and Docker Compose are installed on your system. If not, install them using:

Verify the installation:

Step 2, Create a Simple K6 Test Script

To start with a basic example, create a new file named script.js with the following content:

This script sends a simple GET request to https://test-api.example.com and validates the response status.

Step 3, Create the Docker Compose File

Create a docker-compose.yml file to configure the K6 test execution:

Step 4, Run the Simple K6 Load Test

To start the K6 test, run:

This will execute the basic GET request test inside a Docker container.

Step 5, Expanding to a More Complex API Test (Simulating Postman)

Next, create a new test script called script-payload.js that simulates Postman requests with Bearer Token authentication, POST requests, and JSON payloads.

Example: script-payload.js

Modify docker-compose.yml to use script-payload.js:

Run the more advanced test with:

Step 6, Understanding the K6 Test Summary

After running the test, you will see a summary like this:

Step 7: Running Different HTTP Methods (GET, PUT, DELETE)

Modify script-payload.js for different request types:

GET Request:

PUT Request:

DELETE Request:

To save results in JSON format, modify docker-compose.yml:

After execution, copy the results from the container:

Conclusion

With this setup, K6 can fully simulate Postman requests within Docker Compose, starting from a simple GET request and progressing to authenticated API testing with payloads.

This structured approach ensures a smooth learning curve for automated API performance testing.

Leave A Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.