Pre-request, post-response and basic checks
A pre-request script prepares data before sending (unique values, token, headers); a post-response script checks and stores values after the reply. Each check is a separate pm.test with a clear name, because the first failed expect aborts the test and hides the rest. The status is checked first, since on a non-JSON response json() throws a parse error. Shared logic moves to the collection level.
API · Scripts and checks in Postman
A script must prepare data before the request and check the response after it. Where does each go?
A create-payment test fails with "email already exists" on the second run: the request body kept the same email. And another test, with a status, a field and the response time side by side, shows only the first failure. The right place for a script and the right shape of a test solve both. The lesson covers what runs before the request, what after, and how to write pm.test so that the report shows the cause.
How it works and why it is designed so
A pre-request script runs before sending, when there is no response yet, so that is where data is prepared: unique values, a token, a signature, headers. A post-response script runs after the response, when pm.response exists: that is where you check and store values. Collection, folder and request scripts run in that order, for both tabs. The language is JavaScript, assertions are Chai via pm.expect, and pm.test wraps a group of checks under one name.
When a script runs
Each pm.test gives a separate report row, passed or failed. Inside one test the first failed expect throws, and the remaining checks of that test do not run.
Example: from one big test to several small ones
First a pre-request script that solves the repeated email:
// Pre-request: a unique email for every run
const email = 'qa+' + Date.now() + '@example.com';
pm.variables.set('email', email);
// request body: { "email": "{{email}}" }
Now the post-response script. The "everything in one" version:
// one test: the first failure hides the rest
pm.test('Response is correct', () => {
pm.response.to.have.status(201);
pm.expect(pm.response.json().status).to.eql('pending');
pm.expect(pm.response.responseTime).to.be.below(800);
});
If the server returns 500, the report shows one red test, and whether the field and the time are broken too is unknown. The split version gives a row for every cause:
pm.test('Status 201', () => pm.response.to.have.status(201));
pm.test('Response time < 800 ms', () => {
pm.expect(pm.response.responseTime).to.be.below(800);
});
pm.test('Content-Type is JSON', () => {
pm.expect(pm.response.headers.get('Content-Type'))
.to.include('application/json');
});
const body = pm.response.json();
pm.test('Body fields', () => {
pm.expect(body.status).to.eql('pending');
pm.expect(body.amount).to.be.a('number').and.eql(150);
});
pm.test('Sensitive data is not returned', () => {
pm.expect(body).to.not.have.property('cardNumber');
});
Now the Runner report shows exactly what broke, and the test name is itself a description of the expectation.
Limits and alternatives
Small tests are not free: every pm.response.json() parses the body, so parse once into a variable. If the response is not JSON (an HTML error page from a proxy), json() throws: inside a pm.test the test fails with an unclear parse error, and at the top level of the script, like const body above, the script aborts and the following tests disappear from the report. That is why the first test in a script checks the status. A script that every request needs (a header, a token) goes to the collection or folder level instead of being copied.
Common mistakes
- A check in pre-request. There is no response yet and
pm.responseis unavailable; - One test for everything. The first failure hides the rest, and the name "Response is correct" explains nothing;
- A fixed email or name. The second run hits a uniqueness rule on the server. Generate data in pre-request;
- Checking only the status. The body, headers and absence of sensitive fields stay unchecked.
Check yourself
A test has three expect calls side by side, and the report says it failed on the first. Were the other two run?
Answer
No. A failed expect throws and aborts the test function. To see every problem in one run, split the checks into separate pm.test calls.
Where do you generate a unique email for the request body: pre-request or post-response, and why?
Answer
In pre-request: the value is needed before sending. In post-response the request has already gone out with whatever was there.
pm.test with a clear name. Middle/Senior: Scripts run collection, folder, request. Small tests are needed because the first failed expect aborts the test and hides the rest. I check the status first because on a non JSON response json() gives a parse error instead of a clear failure, and I move shared logic to the collection level.