0
0
Postmantesting~5 mins

Default and conditional responses in Postman

Choose your learning style9 modes available
Introduction

Default and conditional responses help you check if your API sends the right reply based on different situations. This makes sure your app works well for all users.

When you want to test how your API responds to a successful request.
When you need to verify the API returns an error message for bad input.
When you want to check different responses based on user roles or permissions.
When you want to handle unexpected responses gracefully in your tests.
Syntax
Postman
// Example of conditional test in Postman
pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

if (pm.response.code === 200) {
    // test for success response
    pm.test("Response has success message", function () {
        pm.expect(pm.response.json().message).to.eql("Success");
    });
} else {
    // test for error response
    pm.test("Response has error message", function () {
        pm.expect(pm.response.json().error).to.exist;
    });
}

Use pm.response.code to check the HTTP status code.

Use pm.test() to create test cases that pass or fail.

Examples
This test checks if the response status code is exactly 200 (OK).
Postman
pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});
This checks if the response is 404 and verifies the error message.
Postman
if (pm.response.code === 404) {
    pm.test("Not Found error returned", function () {
        pm.expect(pm.response.json().error).to.eql("Not Found");
    });
}
This test ensures the response code is one of the expected default codes.
Postman
pm.test("Default response check", function () {
    pm.expect(pm.response.code).to.be.oneOf([200, 400, 404]);
});
Sample Program

This script tests if the response status is either 200 or 400. If 200, it checks for a success message. If 400, it checks for an error message. Otherwise, it fails the test with a message.

Postman
// Sample Postman test script for default and conditional responses
pm.test("Response status is 200 or 400", function () {
    pm.expect(pm.response.code).to.be.oneOf([200, 400]);
});

if (pm.response.code === 200) {
    pm.test("Success message is correct", function () {
        pm.expect(pm.response.json().message).to.eql("Operation completed successfully");
    });
} else if (pm.response.code === 400) {
    pm.test("Error message is present", function () {
        pm.expect(pm.response.json().error).to.exist;
    });
} else {
    pm.test("Unexpected status code", function () {
        pm.expect.fail("Received unexpected status code: " + pm.response.code);
    });
}
OutputSuccess
Important Notes

Always check for both expected success and error responses to cover all cases.

Use clear and descriptive test names to understand test results easily.

Conditional tests help you handle different API behaviors in one script.

Summary

Default and conditional responses let you test different API replies in one place.

Use pm.test() and if conditions to check various outcomes.

This helps catch errors early and improves your API reliability.