NashTech Insights

Converting Postman Collection to K6 Script

Dieu Phan
Dieu Phan
Table of Contents

When you do an API test with Postman, and you want to do a performance test as well, you need to export the Postman collection to Postman-to-K6 converter and use the generated K6 script to load-test your own API. In this post, I will share how to convert your Postman collection to a K6 script to do a performance test with K6.

1. Export Postman Collection

Go to the Postman tool and select all test API requests that need to convert to K6 script then export the Postman collection to JSON file.

Export Postman Collection

2. Install Node.js & NPM

You should install the latest version of Node.js which has included NPM 9.6.7 such as Node version: 18.17.1

Install Node.js

3. Install postman-to-k6 tool

The postman-to-k6 tool is used to convert the requests of Postman collections to JavaScript codes which are k6 scripts.  To install globally, we can use the below command.

npm install -g postman-to-k6

Install postman-to-k6 Tool

4. Convert Postman Collection to K6 Script

Run the following command to convert the Postman collection to a K6 script assuming the exported collection is named Postman.postman_collection.json and the env.json includes all environment variables that are exported from Postman.
npx postman-to-k6 Postman.postman_collection.json -e env.json -o k6-script.js

You also convert a Postman collection without an environment to a K6 script as the following command:
npx postman-to-k6 Postman.postman_collection.json -o k6-script.js

Convert Postman Collection to K6 Script
  • If you are using an older version of npm you should use the path ./node_modules/.bin/postman-to-k6 your-postman-file
  • Review and fix the generated script: The Postman-to-K6 tool does not support all the features of Postman. For example, it does not support environment variables or pre-request scripts. You may need to modify the K6 script after it is converted to ensure that it works correctly. For example, you may need to specify the headers and body of the requests.
Converted K6 Scripts

5. Run k6 with the generated script

After converting the Postman collection to the K6 script and fixing the generated script if any, we can run the converted script with K6 to do your performance test successfully. Here is a quick demonstration:

6. Conclusion

In conclusion, Postman is used for API functional testing, but K6 is focused more on API load testing. Many of the Postman features are supported by the postman-to-k6 and k6 tools that can help you turn your API requests in Postman into a k6 script to load-test your API.

Reference:

Dieu Phan

Dieu Phan

I'm a Test Lead with 15+ years of experience in the software testing to manage a team of 7 members at peak time. I have strong experience in Manual tests, API tests, Performance tests, and Security Test in various domains including Finance, Education, E-commerce, Health, Advertisement, Logistic, Law, and Games. I'm expert in creating test coverage in high-level requirement and testing microservices application as well with ISTQB Foundation Certificate.

Leave a Comment

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

Suggested Article

%d bloggers like this: