Ease your automated api testing using PostMan
Nirmal kumar
Posted on May 23, 2024
With fast emerging technology and architecture, building & testing apis are an inevitable task for every developer.
If you are not yet aware about PostMan, please do check. It will help your api development life easier.
Frequent issues I face as an api developer are,
- Api consumer will shout that the deployed api is not up and running.
- If it's running, api consumer will report that it is producing irrelevant response.
If the number of api is less in numbers, it should be okay if we manually debug and fix the root cause. But in cloud native Microservices architecture, we will end up managing 100s of apis that belong to our domain. Imagine the number of apis to manage, if we have multiple environments such as dev, qa, uat, training, staging, performance, production, disaster recovery, etc.,
As an api producer/developer, I want the api consumer to be happy. If any issues, I would want to resolve it super quick.
This is where PostMan comes into picture where they provide an easy option to write unit test cases directly in their GUI for each of my api, using which I can run automated tests in seconds and identify which one is not up & running and why.
This helps me to resolve issues quicker and focus on actual business problem.
Example:
Below are simple step by step approach on how to write test cases for an api in postman,
Once we have postman installed, up and running in our machine, add a new collection & enter details about our api endpoint and enter request parameters.
Here I am using default postman HTTP GET REST endpoint for demo purpose, it doesn't have any input parameters.
Click 'Send' button in postman, it will make a HTTP GET request to the mentioned endpoint and we can see the response in PostMan bottom section 'body' tab.
As an Api producer, after we deploy our api to an environment we either use Open Api documentation portal ex: Swagger UI if we use Asp.Net Core WebApi or PostMan to verify whether our deployed api is working smoothly for every scenario. It's a manual process.
We can go to 'Scripts' tab in PostMan to write our test cases in 'Post-Response' sub section which will be automatically executed when we trigger the endpoint manually by clicking 'Send' button or we run the entire collection of apis.
Now, let's go deeper into test case.
pm.test("Response status code is 200", function () {
pm.expect(pm.response.code).to.equal(200);
});
- PostMan uses chai.js syntax to write BDD.
- PostMan uses pm object to access request/response objects.
- 'test' is a javascript test function.
- "Response status code is 200" is test name which will be printed in test results window.
- 'pm.expect' is assertion to verify the test.
- 'pm.response.code' access the Http Status Code of received response back from api.
- '.to.equal' follows Chai.js BDD syntax
- '200' is expected result.
Below are few more test cases for the same endpoint,
pm.test("Response time is within an acceptable range", function () {
pm.expect(pm.response.responseTime).to.be.below(500);
});
pm.test("Response has the required fields - args, headers, and url", function () {
const responseData = pm.response.json();
pm.expect(responseData).to.be.an('object');
pm.expect(responseData).to.have.property('args');
pm.expect(responseData).to.have.property('headers');
pm.expect(responseData).to.have.property('url');
});
pm.test("Headers contain all the expected fields", function () {
const responseData = pm.response.json();
pm.expect(responseData.headers).to.be.an('object');
pm.expect(responseData.headers).to.include.all.keys(
'x-forwarded-proto',
'x-forwarded-port',
'host',
'x-amzn-trace-id',
'user-agent',
'accept',
'cache-control',
'postman-token',
'accept-encoding'
);
});
pm.test("Validate the schema of the response JSON", function () {
const responseData = pm.response.json();
pm.expect(responseData).to.have.property('args').that.is.an('object');
pm.expect(responseData).to.have.property('headers').that.is.an('object');
pm.expect(responseData).to.have.property('url').that.is.a('string');
});
We can verify the test results in bottom section,
With this test setup in postman, I can verify my any number of deployed api in any environment within a minute and identify which one is not working and why.
Below is a sample of how I run an entire collection of api with it's unit test cases,
Note: We can schedule these tests, run it from cli or even integrate with our CI devops pipeline.
Entire collection test results:
Note: It ran in 2seconds. All I need is 2-5 seconds to identify which of my api is making trouble.
PostMan is so powerful. It has many features like api performance testing, api monitoring, api observability, etc., I will come back with an another blog later.
Thank you for reading! :-)
Posted on May 23, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.