In API testing, Assertions are a way of validating the responses. Postman allows us to write JavaScript code which can assert on the responses and automatically check the response.
Assertions to the responses of the requests can be added in the “Tests” tab. Whenever a request is sent, the code will run and the results will be shown in the Test Results tab. It has both assertions passed: in green color and failed: in red color. Postman makes the process easier by listing commonly used snippets next to the editor. These assertions are added just by clicking on the code snippet and the appropriate code will be added to the test editor.
Below are the various types of assertions:
1. For validating status code returned tests["Status code is 200"] = responseCode.code === 200;
2. For validating the string contained in the response body tests["Body matches string"] = responseBody.has("string_you_want_to_search");
3. For validating key: value in the response body var jsonData = JSON.parse(responseBody); tests["Your test name"] = jsonData.value === 200;
4. For validating the correctness of the response body tests["Body is correct"] = responseBody === "response_body_string";
5. For validating Status code name has string tests["Status code name has string"] = responseCode.name.has("Date");
6. For validating headers tests["Content-Type is present"] = postman.getResponseHeader("Content-Type");
7. For validating response time tests["Response time is less than 100ms"] = responseTime < 100;
How to Get Started with API Testing (Video Demo)
You know API testing and you're ready to get started with it -- you just need to know how. Well, we're here to help! Check out our latest guided demo…
Replies
In API testing, Assertions are a way of validating the responses.
Postman allows us to write JavaScript code which can assert on the responses and automatically check the response.
Assertions to the responses of the requests can be added in the “Tests” tab.
Whenever a request is sent, the code will run and the results will be shown in the Test Results tab. It has both assertions passed: in green color and failed: in red color.
Postman makes the process easier by listing commonly used snippets next to the editor. These assertions are added just by clicking on the code snippet and the appropriate code will be added to the test editor.
Below are the various types of assertions:
1. For validating status code returned
tests["Status code is 200"] = responseCode.code === 200;
2. For validating the string contained in the response body
tests["Body matches string"] = responseBody.has("string_you_want_to_search");
3. For validating key: value in the response body
var jsonData = JSON.parse(responseBody);
tests["Your test name"] = jsonData.value === 200;
4. For validating the correctness of the response body
tests["Body is correct"] = responseBody === "response_body_string";
5. For validating Status code name has string
tests["Status code name has string"] = responseCode.name.has("Date");
6. For validating headers
tests["Content-Type is present"] = postman.getResponseHeader("Content-Type");
7. For validating response time
tests["Response time is less than 100ms"] = responseTime < 100;