k6 – PostMan (PostBot AI) & GitHub Actions

Do you have API testing in your project? Great! Its time for you to think a bit wider and implement the Early Performance Testing.

Spoiler Alert: You don’t have to write a single line of code for the tests 🙂

Sounds interesting? Let’s begin!

In this article we will see how to kickstart implementing the Early Performance Testing using k6 but with the tests written in Postman. Make sure you have the below tools installed in your machine –

  1. Postman Desktop App & a free account – https://www.postman.com/downloads/
  2. Visual Studio Code – https://code.visualstudio.com/download
  3. NodeJS – https://nodejs.org/en
  4. k6 Open Source – https://k6.io/open-source/
  5. Newman – https://www.npmjs.com/package/newman
  6. postman-to-k6 – https://www.npmjs.com/package/@apideck/postman-to-k6

We will be using the website http://reqres.in for testing purpose. You can use the API endpoints from your project.

Step 1.1: Install k6 Open Source

Step 1.2: Install Postman Desktop App

Step 2: Create a free account – Licencing is not required for this solution. Thanks to Postman 🙂

Step 3: Create a new collection and name it as “Demo Collection”

Step 4: Add the below requests

GET : Get Users – https://reqres.in/api/users?page=2

POST: Create Users – https://reqres.in/api/users

Step 5: Open the GET request and hit Send. Make sure you are getting a valid response with status code 200

Step 6: Now go to Tests tab and click on the PostBot button (highlighted in yellow circle below). Once you click it you will be able to see PostBot Chat window

Step 7: In the PostBot (AI) enter this – “Can you generate me tests for this get request?” and hit enter

Boom!!! You will be able to see that the PostBot (AI) created valid tests for the Get request

You can always add more tests to it until it suits your requirement and also, if you are comfortable, you can add your custom tests through JS.

Step 8: Now that the tests are created, save the request and click Send button again. If you navigate to the Test Results tab, you should be able to see the below test results –

Repeat Steps: Open POST request and repeat the steps from 5 to 8

Step 9: You have 2 requests, Get Users and Create User and its corresponding tests. Its time for us to add k6 flavour to it. To do this, we need to open on Visual Studio Code.

Step 10: Open a new Terminal in VS Code. Now run the below command to start with –

npm init -y

Step 11: Create a new folder and add the Postman Collection to it. In order to run Postman Collections via command line, we need to install NewMan package from npmjs. Run the below command to install as dev dependency –

npm i newman –save-dev

Step 12: Great job! Now, in the package.json, add the below scripts –

“scripts”: {
“postman:tests”: “newman run ./collections/Demo.postman_collection.json”,
“convert-to-k6”: “postman-to-k6 ./collections/Demo.postman_collection -o k6-script.js”,
“performance-tests”: “k6 run k6-script.js –summary-trend-stats=\”avg,p(90),p(99.9),p(99.99),count\” –out json=test.csv”
},

Step 13: Run the below command to run Postman collections via command line using Newman –

npm run postman:tests

Step 14: In order to run the Postman collections using k6, we need to convert the Postman collections file to k6 JS file. To do this, we have to make use of postman-to-k6 node package.

npm i @apideck/postman-to-k6

Step 15: Now run the below command to convert the Postman collection to k6-script.js file

npm run convert-to-k6

Step 16: Congratulations for coming till this way!!! Now, its time for us to run the k6 Script. To do this we need to run the below command –

npm run performance-tests

Step 17: This will run your Postman API Tests which you generated with AI and does the performance testing using k6

You can always enhance on top of it by defining thresholds etc.,

Step 18: Finally, integrate the tests with GitHub Actions. Add the below load-tests.yml code into github/workflows folder. This contains the code to run against 50 virtual users.

name: Main Workflow
on: [push]
jobs:
build:
name: Run k6 test
runs-on: ubuntu-latest
steps:
– name: Checkout
uses: actions/checkout@v4
– name: Run k6 local test
uses: grafana/k6-action@v0.3.1
with:
filename: k6-script.js
flags: –vus 50 –duration 10s

GitHub Repo: https://github.com/grajk88/postman-k6-github-demo

GitHub Actions: https://github.com/grajk88/postman-k6-github-demo/actions

Demo:

Conclusion:

API Early Performance Testing with basic tests are always suggested to monitor the API state at various conditions. This will give more confidence and enable us to track performance related issues much faster and in early stages. This can also be integrated with the GitHub Actions and run at specific scheduled timings.

This concludes our article and I hope you enjoyed this!!

Happy Testing!!!

Leave a comment

Website Powered by WordPress.com.

Up ↑