Useful commands and a strong test
A strong test fails for every plausible breakage: code, structure, value and side effect, so checking only status 200 is not enough. The minimum is the status, the schema and key values taken from what was sent, plus a GET after a write. Useful commands are pm.sendRequest for a helper request, pm.execution.setNextRequest for the Runner order and console.log for debugging in the Postman Console.
API · Scripts and checks in Postman
A test checks only the 200 code and is green. What else could the backend have broken?
PATCH /accounts/42 renaming an account returns 200. The test is green. A week later it turns out the backend wrote the name into the wrong field and wiped the currency: the endpoint behaved like PUT. Status 200 only says the server processed something without an error. The lesson covers why such a test is weak, what to add, and which script commands help check and debug.
How it works and why it is designed so
A test is strong when it fails for any plausible breakage. Breakages are of four kinds: code (a server error), structure (a field vanished or changed type), value (the wrong thing was written) and side effect (something extra was touched or nothing was saved). Status covers only the first. So the minimum for a response is status, schema and key values. A write adds a following GET, because the PATCH response shows what the server says, and the GET shows what is really stored.
A weak and a strong test
Only status 200
An empty array or a wrong value passes: the test is green, the defect stays.
Status, schema, values, GET
After a write a GET too: you see what was saved and what was not touched.
Example: a weak script, what it misses, a strong script
The weak version in post-response:
pm.test('ok', () => pm.response.to.have.status(200));
It misses: a name written into another field; a response without the required id; a cardNumber field that started coming back; a new name that exists only in the response and not in the database. The strong version:
const schema = { type: 'object', required: ['id', 'name'] };
1pm.test('Status 200', () => pm.response.to.have.status(200));
const body = pm.response.json();
pm.test('Name changed to the sent value', () => {
2 pm.expect(body.name).to.eql(pm.variables.get('newName'));
});
pm.test('Schema is valid', () => {
3 pm.response.to.have.jsonSchema(schema);
});
pm.test('No card data in the response', () => {
4 pm.expect(body).to.not.have.any.keys('cardNumber', 'cvv');
});
- The status is needed, but it is only the entrance ticket.
- A value check that the sent value was actually saved.
- The schema catches drift in structure and types.
- A negative check that nothing sensitive leaks.
The newName value was set in pre-request via pm.variables.set and is available in the post-response of the same request and, within a collection run, in the following requests. Then the next request of the run re-reads the resource:
// Post-response of GET /accounts/42, the next request
const acc = pm.response.json();
pm.test('Name was persisted', () => {
pm.expect(acc.name).to.eql(pm.variables.get('newName'));
});
pm.test('The other fields are untouched', () => {
pm.expect(acc.currency).to.eql(pm.environment.get('currencyBefore'));
});
This checks both what was written and that the other fields stayed as they were. The currencyBefore value was stored by an earlier GET.
Commands that help
pm.sendRequest(...): a helper request from a script, for example fetching a token in pre-request;pm.execution.setNextRequest('Name'): changes the order in the Runner,nullstops the run. Old collections usepostman.setNextRequest;pm.environment.unset('token'): remove a variable after yourself;console.log(...): output to the Postman Console, which shows the raw request and response. It is the main debugging tool: it shows the final URL and headers, so you see at once whether{{token}}went out literally.
Limits and alternatives
A strong test does not mean "more checks at any price". Checking every field duplicates the schema and breaks on every harmless change. Choose by risk: money, permissions, persistence. What is hard to check through the response (an event in a queue, an email, an audit entry) is checked another way: a separate request or a direct query to the store. For big suites, queues and complex data, code (Playwright, REST Assured) fits better, with real functions and types.
Common mistakes
- Only 200. The test is green on an empty or wrong response;
- Checking a write only by its response. A PATCH response does not prove the data was saved;
- An expected value taken from the response.
expect(body.name).to.eql(body.name)is always green: take the value from what was sent; - Ignoring the Console. People spend hours searching for a cause visible in the raw request.
Check yourself
A test after PATCH checks status 200 and that body.name equals the sent value. Which breakage does it still miss?
Answer
Those not visible in the response: the data was not saved in the store, other fields were wiped, the response was assembled bypassing the database. A following GET catches them.
A request failed with 401 and the URL in the Console looks fine. Where to look next?
Answer
At the headers in the same Console: is there an Authorization and does it hold a real token and not a literal {{token}}. Then the variable scope and the token's validity.