API Testing with Postman
Postman is a great tool to use in api documentation and tests which I was using for a time, but only for manuel testing purposes. Recently I tried a simple collection run in Postman and realized it was easier than writing api tests in code.
Below you can find a simple introduction about using Postman for api test automation and how to integrate these tests to continous integration.
Collection
If you are already familiar with using Postman to send requests, then start with gathering your requests in a collection. They can be in a logical order if they need to be chained. Or the if the endpoints serve different purposes unrelated from each other then dont mind the order.
I used a sample Petstore server for this demo.
Write test scripts
It is possible to write your assertions with javascript in Postman Tests tab. You can use the snippets Postman offers which appears when you click the little arrow next to Tests editor.
Possible assertions are as below but not limited to:
- The response code
- Expected string in response
- Response time
Extracting data and chaining collection
It is possible to manage variables within environment. After adding an enviroment and defining variables you can extract a response value with tests script and keep it in variable to use in chaining collection.
//assign petid to variable
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable(“petid”, jsonData.id)
Run tests in Collection Runner
Now it’s time to run api tests in Postman Collection Runner. Before running tests you can edit or rename your requests and even set an interation time to run collection. But it’s not possible to disable a request. I think this feature would be very useful.
Run Postman collection in console
If you want to run your collection outside of Postman GUI then you can use Newman.
https://www.npmjs.com/package/newman
For this you should first install node.js and be sure you also have npm package manager within. Because below command is to install newman via npm.
npm install -g newman
There are two ways to run your collection in command line.
1-export your collection as JSON file and use below command.
newman run examples/sample-collection.json
2-get your collection link and use below command.
newman run https://www.getpostman.com/collections/{collectionId}
When you run your collection in command line the result will be like something below.
Run Postman collection in Teamcity CI
Teamcity is a CI tool with simple GUI to create a pipeline (to build,test and deploy code) and as I know it is free with restricted agent count and configuration. To run Postman collection in Teamcity all you need to do is install newman-reporter-teamcity in the Teamcity agent. This is needed to see test report in teamcity as pass fail count.
Below command is to intall this extension to Teamcity agent.
npm install -g newman-reporter-teamcity
After installation set up your configuration and arrange build step with Command Line runner.
Thanks for reading👩💻
I hope these short instructions helps someone out there. And I would like to know if you have better or worse experiences in Postman.