PracticesToolsTutorial

Postman Test cases Examples: Learn How to Write API Test cases in Postman

In the respective video Mr. Promode from The Testing Academy is presenting a tutorial for how to write API testing test cases into Postman. 

Firstly one has to go to google chrome and search for reqres.in and open the first link. It shows up a very simple fake API kind of data where we certain get post requests. Here a request is being made in api/user and when the get request is clicked, a bunch of requests appear below. The webpage will return basically a total of 4 pages and 12 elements being a fake data on the website. The user details are also present their including their e-mails, first name and last name etc. 

Now if we talk about what of API test can be tried specially for this kind of API, mostly if someone wants to write certain type of API test and specially if they want to use postman then the person can write cases specially in the current tab being used. 

Postman is a certain type of template functions for example it provide you certain kind of basic structure that how one can write their test cases. So, we will go through one by one that what kind of test cases one can write specially for API testing.

On the fake website that was opened lastly would show different tabs and templates on the left side. These templates provide different test cases that could be written on the platform. Here all the test cases would be explained one by one. 

First Test: Status Code: Code is 200

So, the first test we can write is the Status Code: Code is 200 when we are making a get request. If the response of this get request is successful then we are going to get 200 OK requests. The structure of postman shows up like:

pm.test(“Status Code is 200”, function ( )

pm.response.to.have.status (200);

The pm.test is the name of the test and pm.response is the response of the test. The whole test case shows up the status of 200 so after running the test the results below indicate that the status has been passed and test has been carried out successfully. 

pass Status Code is 200

Now if the status code is changed from 200 to 201 which is status code of creation that we would do in post request. After running the test it would fail as it was the wrong command added and would show up like:

pm.test(“Status Code is 201”, function ( )

pm.response.to.have.status (201);

Test Result:

fail Status Code is 200, Assertion Error expected to have status code 201

Second Test: Response Body: Contains String

Now the second test that we can write is Response Body: Contains String to run another test. 

In the previous example if suppose we want to validate the different following keys that these need to be present in the test:

“page”: 1

“per page”: 3

“total”: 12

“total pages”: 4

So for this purpose we could write the simple test case where body matches the string and we also mention the keys as well for instance in first test we write the page as a key to run the test. It could be done as follows:

pm.test (“Body matches string”, function ( )

pm.expect (pm.response.text ( ).to.include (“page”)

pm.expect (pm.response.text ( ).to.include (“per page”)

pm.expect (pm.response.text ( ).to.include (“total”)

pm.expect (pm.response.text ( ).to.include (“total pages”)

So these are the 4 keys that we wanted to validate. API testing is nothing about the validation with expecting result with the actual result that we are getting. Now after running the second request, as all the things are matching so we are getting the pass in the test result as shown below:

pass Body matches String

If we mess up with any of the keys and add wrong data in the above section then the test result would be failed as body is not matching:

fail Body is not matching

Third Test: Response Body: JSON Value Check

Now the third test is known as Response Body: JSON Value Check that is the most important test especially the values. So for example if we want to validate the first name that is supposed as Janet then the following code would be added:

pm.test (“Expected Janet at Data(1).first_name” , function ( )

var jsondata = pm.response.json( );

pm.expect (jsonData.data(1).first_name).to.eq1 (Janet);

Now if we want to navigate to first name it will be shown in the second category of data. Now after running the test the results have shown that is has passed like:

pass Expected Janet at Data(1) first_name

In API testing the JSON test case is known to be the most important test case and most of the time or approximately 70% of test cases revolve around this test. That means we have to validate certain kinds of JSON keys and values. We have compare here basically the expected and the actual values. This kind of test cases are added in the postman most of the time. 

Fourth Test: Response Headers: Content Type Header Check

The next test is very simple one and just verify the content type that someone is responding should be containing the type of content and the response header that we are getting whatever it is. The next test is known as Response Headers: Content Type Header Check. It would show up like:

pm.test (“Content-Type is present”, fuction ( )

pm.response.to.have.header (“Content-Type”);

Fifth Test: Response Time is Less than 200ms

So if we proceed to the next test that is Response Time is Less than 200ms. It shown on the screen that we are getting 43ms response time and it should be always below 200. It is a of little bit performing test case that we are using in postman as shown below:

pm.test (“Response time is less than 200ms”, function ( )

pm.expect (pm.response.responseTime).to.be.below (200);

Sixth Test: Status Code: Successful POST Request

The text test case is Status Code: Successful POST Request in which if there is a particular post request we will get the results in 201 to 202 from this and the message they will give you would be “created”. So if we want to create a news resource so a post request would be required to be made and we are also validating here that it should be either 201 or 202 while we are making a post request and if we run the test if show failed because we have not made the post request yet as follows:

pm.test (“Successful POST request”, function ( )

pm.expect (pm.response.code).to.be.oneOf(201,202));

Test Results:

fail Successful POST request | AssertionError: expected 200 to be one of (201,202)

Now if the post request is changed from the above and added 200 series then the result would be shown as passed because the 200 would be matching there. The command would be as follows:

pm.test (“Successful POST request”, function ( )

pm.expect (pm.response.code).to.be.oneOf(200,201));

Test Results:

pass Successful POST request 

Seventh Test: Status Code: Code Name has String

Now the next test is known as Status Code: Code Name has String for whenever you are making a post request you might get a particular kind of status. If you are making a post request you are going to get created and if you are making a get request then you are going to get OK. If we run the test now it would show up fail because we have not added OK at the place of created as follows. 

pm.test (“Status code name has string”, function ( )

pm.response.to.have.status (“Created”)

Test Result:

fail Status code name has string | AssertionError: expected response to have status reaction “Created but got “OK”

If we change the command OK instead of Created then the test resulted would be pass as follows:

pm.test (“Status code name has string”, function ( )

pm.response.to.have.status (“OK”)

Test Result:

pass Status code name has string

Eighth Test: Response Body: Convert JSON body to a JSON Object

The next test is Response Body: Convert JSON body to a JSON Object that can convert whatever JSON we have into xml and we can verify it using xml also in similar way and the command of this test is as follows:

pm.test (“Status code name has string”. function ( )

pm.response.to.have.status (“OK”)

var jsonobject = xml2json (responsebody);

Ninth Test: Use Tiny Validator for JSON Data

Now another test case named as Use Tiny Validator for JSON Data can be requested as well. It is also known to be the most powerful test case and also known as JSON schema validation. This test case has the following command:

var schema = (

“Items”;

“type”; “Boolean”

var data1 = [true, false];

var data2 = [true, 123];

pm.test (“Schema is valid , function ( )

pm.expect )tv4.validate(data1, schema)).to.be.true;

pm.expect )tv4.validate(data2, schema)).to.be.true;

All the test cases discussed above are known to be Postman test examples that make sense to everyone.

Related Articles

Leave a Reply

Your email address will not be published.

Back to top button