How to perform assertion testing in Postman?

In Postman, assertion testing is accomplished through the authoring of JavaScript scripts, leveraging the Chai assertion library built into Postman for validation purposes.

Home > Blog > How to perform assertion testing in Postman?

In the modern software development process, APIs (Application Programming Interfaces) play an extremely important role, allowing interaction and data exchange between different software applications. As the applications that first appeared with APIs become increasingly complex, ensuring the reliability and stability of APIs becomes particularly important. That's why API testing, especially automated testing using assertions, has a place in the software development lifecycle.Postman is a popular API testing tool that can send HTTP requests, receive responses, and provides a rich set of features that allow us to create test scripts to verify that all aspects of API responses meet expectations. This article will detail how to use assertions for API testing in Postman.

Preliminary exploration of assertion testing

Assertion testing is the core of automated testing. Its working principle is simple: after receiving an API response, it verifies whether the actual result meets expectations. If it meets expectations, the test passes; if it does not, the test fails, indicating that there may be a problem with some aspect of the API.In Postman, assertion testing is implemented by writing JavaScript scripts that use Postman's built-in assertion library Chai for verification.

Write assertion test scripts

The steps to create an assertion test in Postman usually involve verification in the following aspects:

1. Verification status code

The status code is the most direct indicator of whether an API response is successful.

pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});
How to perform assertion testing in Postman

2. Verify response time

Response time is one of the important metrics for measuring API performance.

pm.test("Response time is less than 500ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(500);
});
How to perform assertion testing in Postman

3. Verify response body

The response body contains the data returned by the API, and verifying its structure and content is the most important part of API testing.

pm.test("Body contains the expected structure and data", function () {
    var responseBody = pm.response.json();
    pm.expect(responseBody).to.deep.include({
        "expected_key": "expected_value"
    });
});
Verify response body

4. Verify response headers

The response header may contain information such as content type and character set, and sometimes verification is required.

pm.test("Content-Type is application/json", function () {
    pm.response.to.have.header("Content-Type");
    var headerValue = pm.response.headers.get("Content-Type");
    pm.expect(headerValue).to.include("application/json");
});
How to perform assertion testing in Postman

Execute tests and view results

After writing the test script and sending the request, Postman will automatically run these tests and display the test results in the "Test Results" tab. If all tests pass, your API will perform as expected in these test designs, which will greatly enhance your confidence in the API.

How to perform assertion testing in Postman

Summary

Using Postman for assertion testing not only improves API quality, but also saves a lot of manual testing time, making the testing process more automated and systematic. With well-designed test scripts, you can ensure that your API runs stably and reliably in the production environment.

How to perform assertion testing in Postman?
In Postman, assertion testing is accomplished through the authoring of JavaScript scripts, leveraging the Chai assertion library built into Postman for validation purposes.

Learn more: