0
0
Postmantesting~5 mins

Why automated assertions validate responses in Postman

Choose your learning style9 modes available
Introduction

Automated assertions check if the response from a server is correct without needing a person to look at it. This saves time and avoids mistakes.

When you want to quickly check if an API returns the right data after a request.
When you need to run many tests often and want to avoid checking results manually.
When you want to make sure a website or app works correctly after changes.
When you want to catch errors early before users see them.
When you want to keep a record of test results automatically.
Syntax
Postman
pm.test('Test description', function () {
    pm.response.to.have.status(200);
    pm.expect(pm.response.json().key).to.eql('expected value');
});

pm.test defines a test with a description.

pm.response accesses the response to check its status or body.

Examples
This test checks if the response status code is 200 (OK).
Postman
pm.test('Status is 200', function () {
    pm.response.to.have.status(200);
});
This test checks if the response JSON has a 'name' key with value 'Alice'.
Postman
pm.test('Response has user name', function () {
    const jsonData = pm.response.json();
    pm.expect(jsonData.name).to.eql('Alice');
});
This test checks if the response time is faster than 500 milliseconds.
Postman
pm.test('Response time is less than 500ms', function () {
    pm.expect(pm.response.responseTime).to.be.below(500);
});
Sample Program

This Postman test script checks two things: the server responded with status 200, and the response JSON has a message 'Success'.

Postman
pm.test('Status code is 200', function () {
    pm.response.to.have.status(200);
});

pm.test('Response contains success message', function () {
    const jsonData = pm.response.json();
    pm.expect(jsonData.message).to.eql('Success');
});
OutputSuccess
Important Notes

Always write clear test descriptions so you know what each test checks.

Use assertions that match what your API should return to catch real problems.

Automated assertions help you test faster and more reliably than manual checks.

Summary

Automated assertions check if responses are correct without manual work.

They save time and catch errors early.

Postman uses simple code to write these tests.