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.
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
3. Install postman-to-k6 tool
npm install -g postman-to-k6
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
- 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.
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:
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.