Basic tests in Postman

sridevi

Postman is one of the most powerful tools for testing API’s. We can use postman to write simple automated scripts to test the API’s.
In this post, we will explore some of the postman features which are helpful for writing a simple test very easily and quickly without much coding.

Test Editor
When we launch postman we will see Tests section in request header area.

postman1

 

Snippets
On the right side of the screen, we can see code snippets.
Clicking on any of the snippets titles adds the code snippets to the test editor area. This makes writing basic tests very easy and fast.

Viewing Results
Postman runs a test every time we send a request. The Results are displayed in the Tests section under Response area. The result shows the number of tests passed and failed. In the snippets what we have added in tests are true , then the test result will be passed or else failed.

Writing a simple test
Let’s start writing a simple test using the POST API for one of our projects to get a clear understanding of how things work. Results we can see in the Test section of Response area.

For this test, we will check if status code returned is 200 or not.

– Set the URL: https://dl8ry28hkl.execute-api.us-east-1.amazonaws.com/dev/login
– Set Content-type header to “application/json”
– Pass the email_id and password in request parameter within body
{
     “data”:{
     “email_id”: “xxx@gmail.com“,
     “password”:”yo”,
     “login_mode”:0,
}
}
– Scroll down in snippets area and add a snippet titled Status code: status code is 200 to test editor.

Postman 2

– Submit the request.
– Switch to Tests header under response area where we can see the results.

Postman 3

You can see that status code test is passed.
We will run one more test just to see how the result will be when test is failed

Now change the status code to 201.

Postman 4

Now run the test, we can see that test is failed from the below results.

Postman 5

The postman clearly shows that test got failed in tests tab.

More Snippets

Postman provides some more snippets which we can use for basic API testing. Some of them listed below.
To check content-type header is present or not :
tests[“Content-Type is present”] = postman.getResponseHeader(“Content-Type”);

Response time is less than XX ms :

tests[“Response time is less than 200ms”] = responseTime < 200;

For checking successful post requests :
tests[“Successful POST request”] = responseCode.code === 201 || responseCode.code === 202;

If Response body contains any particular string or not :
tests[“Body matches string”] = responseBody.has(“string_you_want_to_search”);

How to run Collections

Collections are a great way to group together all the requests under a folder.  We can give a name to a collection and save it to the left of the postman editor.
Collection Runner is a tool used to run the multiple requests saved under collections. We can run the collections any number of times if we want.

Below is the screenshot of collection runner:

Postman 6

The requests within the collections are run in the order they are saved. It runs test for each request and shows the test report.It will save the history of test runs so that at later stages we can check them.
Need to run all the use cases, now it’s super easy through collection runner we have to just select the collection we want to test and say START TEST.
Collection runner runs in a separate window and thus we can make any changes to the saved collections or requests in the main Postman. While the collection runner is running the changes made to the collections in the main postman are updated in the collections runner as well.

Starting a run:

Running a collection test is simple. Select the collection name and click START TEST. The only collection name is mandatory.
-Collections/Folder – All the requests within a collection are executed
-Environment – Optional
-Iterations – The number of times the collection has to be run.
-Data – (Optional) Load a CSV or JSON data file

Hit the Start button and postman will start displaying results of the run.

References :

https://www.getpostman.com/docs/
https://seesparkbox.com/foundry/api_testing_with_postman

 

about the author

sridevi