Hello Friends,
In this post, I am going to share with you how you can use newman client to test your API(s). I am writing this blog in two parts.
Part 1 Objective:
To be able to use newman client to test API(s)
For this we'll do:
- Create an API. You can create the API in any language like .Net, .Net core, php, nodejs. For this sample, I have created an API using NodeJS and express and I have used only the GET API(s) to keep it simple
- Test that API(s) once in postman.
- Setup Asserts
- Export the collection.
- Create a nodejs project or use this github repo
- Setup newman. Checkout package.json in the repo.
- Test the collection using newman
Part 2 Objective:
To integrate newman in Azure DevOps CI\CD so that our API(s) can be tested automatically as part of CI\CD. We'll discuss the steps in next post.
So, let's get started with our first objective.
Here are the steps you can follow:
- Clone the github repo
- Ensure that you have nodejs installed.
- In the folder, which contains package.json, run the following command:
npm install
- Install npx globally, so that you can access local node modules using npx:
npm install -g npx
- Open Terminal and run:
npm run start
- You should have an api running at port 3000.
So, here basically npm reads the package.json and checks out the 'start' in scripts section. This will further read main.js and will have an express js server running at port 3000. Also, you can see the following code which is nothing but an API endpoint.
- So, we have a working API running which can be tested in postman or even directly as this is a get only api.
- Adding Tests/Asserts. You can do this in Tests Section of each API:
- Next, let's talk about the postman collection. I am having a collection added already with name mycollection.json. You can export a collection as shown in the picture below:
- Let's try understanding the collection. So, we can see that postman collection is easily readable
- To add more tests, you can add in the Postman and can re-export the collection
- Open another terminal so that the API is running and run the following command
npm run test
So, if you checkout package.json, I have installed newman and when we run npm run test it is running the newman collection. Checkout the 'test' in package.json under scripts section
- Yay! The tests-report.txt will be generated which will look like this:
This was easy huh! :) We'll do the CI\CD setup in the next post.