QA Interview Hub · Knowledge base · Quizzes · Українська

API › Scripts and checks in Postman

Common traps in Postman scripts and tests

Most Postman traps follow from three sandbox properties. A test in pm.test fails only on an exception, calls such as pm.sendRequest are asynchronous, and variables live in different scopes. So an expression without pm.expect is always green, and a literal {{userId}} in a URL means the variable is missing from the active scope. A new test is not trusted until it has turned red on deliberately wrong data.

API · Scripts and checks in Postman

The URL contained a literal {{userId}} and the test is green on a wrong response. Why?

The collection passed green, yet in production payments hang in the failed status. It turned out the "Status is paid" test could never fail. Most "weird" Postman failures are a few identical traps: a variable in the wrong place, a test without an assertion, an async callback, a string instead of a number. The lesson covers the symptom, the cause and the action for each, and the order of debugging.

How it works and why it is designed so

Three properties of the sandbox produce most problems. First: pm.test fails only when an exception is thrown inside, and a plain expression throws nothing. Second: pm.sendRequest and similar calls are asynchronous, so without await the code after the call does not wait. Third: Postman variables are stored as strings and live in different scopes with different lifetimes. Knowing this, a symptom is easy to trace to a cause.

A test that cannot fail

Wrong

An expression without expect

The comparison returns false and nobody throws: the test is green even on failed.

Right

pm.expect(...).to.eql

A mismatch throws and the test fails with an explanation.

Figure 1. Without pm.expect a comparison checks nothing.

Example: a green test on a wrong response

Before:

pm.test('Status is paid', () => {
  pm.response.json().status === 'paid'; // checks nothing
});

This test passes even with status: "failed": the expression returns false and that is it. After:

pm.test('Status is paid', () => {
  pm.expect(pm.response.json().status).to.eql('paid');
});

The pm.expect(...) call throws on a mismatch, and the report shows the expected and received values. Check through pm.expect or pm.response.to.... A useful habit: to trust a new test, deliberately break the expectation once and make sure it goes red.

Traps: symptom, cause, action

  • A literal {{userId}} in the URL. The variable is not in the active environment, no environment is selected, or the previous request did not set it. Open the Console: it shows the final URL. Check the scope and the order of requests;
  • The asynchrony of pm.sendRequest. The code after the call runs before the callback. Store the value inside the callback:
let ref;
pm.sendRequest(url, (err, res) => {
  ref = res.json().id; // runs later
});
pm.variables.set('ref', ref); // ref is still undefined here
  • Types. Postman variables are stored as strings, and quoted CSV values stay strings (Newman converts only unquoted numbers). Comparing eql(200) with the string '200' fails. Convert with Number();
  • Scope overriding. The narrower scope wins, so a token in pm.environment can hide behind a local variable of the same name. Check where the value comes from;
  • Secrets in logs. A console.log of a token or personal data lands in Newman logs and CI artefacts. Log identifiers, not secrets;
  • Order and foreign data dependence. A hard coded userId: 42 breaks when the data is gone. A test should create what it needs and clean up;
  • A heavy collection script. It runs for every request: add a condition for when it is really needed.

The order of debugging

From symptom to cause

1. Postman Consolefinal URL, headers, response2. Variable scopewhere the value comes from3. The request alonewithout the Runner and other data
Figure 2. First see what went out, then where the value came from, then rule out the surroundings.

Limits and alternatives

Discipline removes part of the traps: unique variable names, creating data in the test itself, linting scripts in the repository. If a collection grows and traps multiply, that is a signal that tests are better written in code with types and a safe CI. For the scripts themselves it helps to run the collection through Newman and check that it goes red on known bad data.

Check yourself

The test pm.test('ok', () => { pm.response.code === 200; }) is green on a 500 response. Why, and how to fix it?

Answer

The expression returns false but throws nothing, and pm.test fails only on an exception. The fix: pm.expect(pm.response.code).to.eql(200) or pm.response.to.have.status(200).

A value from a pm.sendRequest response was written to a variable after the call, and it is empty in the request. What is wrong?

Answer

The callback runs later than the line after the call, so the write happens before the value exists. Write inside the callback.

In an interview. Junior: Typical traps: a variable in the wrong scope, a test without pm.expect that is always green, the asynchrony of pm.sendRequest, strings instead of numbers. I debug with the Postman Console. Middle/Senior: Most traps follow from three properties: pm.test fails only on an exception, calls are asynchronous, variables are strings in different scopes. I do not trust a new test until I see it go red on deliberately wrong data. I do not log secrets because they end up in CI artefacts.

More articles in this chapter