API Testing – Test API Response

API testing

Test response is an especially important feature for API testers. How can I run each request all day and then check each result manually, there must be a faster way

This section provides 2 great features to help testers speed up API testing.

  • Check automatically the return results of each field with 1, 2 lines of code, very easy, even without knowing the code.
  • Save the value of Response as a variable in Environment to continue passing in the next API params.

Postman provides a framework for us to work, only supports pure Javascript language, does not support jquery or other things. And it has no debug mode or console. → If you want to make sure your code runs correctly, write it outside first.

The section on the right is a collection of Postman syntaxes that are available to users, no need to think. Ok, let’s go to the specific problem.

Problem 1: I have a login API, I want to know that after logging in, my user_id returned is correct or not.

Step 1: Run the API test once to get the API Response structure.

We can see the user_id is located at: root > data > user > profile > id and in this case the id of this user is 401.

Step 2: Write Test

Code:


var jsonData = pm.response.json();

pm.test("Check user_id", function () {

pm.expect(jsonData.data.user.profile.id).to.eql(401);


});

Parse the returned Response and save it to the variable “jsonData” → this is the root written above

Test if user_id is equal to 401. The way to get the value is exactly the same as in Javascript.

After writing the Test, run the Request again and see if your Test is correct.

According to the results, my Test is Fail, I wrote it right.

This is a point that newbies will often encounter, it is the problem right in the tool that helps me test. In this case, it’s return value is 1 String, I can’t compare String and Int. Take a look at the image above, you will see that the id is “401”, we just need to add double quotes to our test part.

var jsonData = pm.response.json();

pm.test("Check user_id", function () {

pm.expect(jsonData.data.user.profile.id).to.eql("401");

});

Problem 2: I have a login API, after login I want to save the value of the token to use as data for the next API.

Still using the example above, we will see that the location of the token is: root > data > token.

We will write 1 more code below the 2 lines of code I wrote above:

var jsonData = pm.response.json();

pm.test("Check user_id", function () {

pm.expect(jsonData.data.user.profile.id).to.eql("401");

});

pm.environment.set("token", jsonData.data.token);

After that, we just run again and check in Environment.

Once the variable has been saved into the Environment, the part of assigning the variable to the request will be the same as the part I instructed in the previous post.

That’s it, using Postman’s Test is as simple as that. I know that I can’t say it all in 1 article, but I can also handle 2 important parts. If you have problems following it, please leave a comment.

To read more articles about API testing , please go this link

Leave a Reply

Your email address will not be published. Required fields are marked *

API testing

Test runner – API testing

Test runner – According to the previous post, you already know how to create simple tests for each API, but a project has too many APIs and too many different tasks, each task is a collection of several APIs how must be solved. Along with that is the management method that you think is applicable […]

API testing

How to test API

After reading “test API with Postman” series, you should be able to grasp the basic knowledge of the API and the functions Postman provides. But how to arrange tests and write Testcases for API still seems not very clear, so today I will write a post about how to test API properly. Reminder of knowledge […]

API testing

API testing – API documentation

Postman, in addition to providing an API testing tool, also helps us to make API documentation extremely professional and easy. This API document can be shared by both the team and the client. Usually, the API is usually written by Dev on google sheets, but at a certain stage of development, the dev will be […]