Consider this Postman test script that uses a reusable function to check response status:
function checkStatus(expected) {
pm.test(`Status is ${expected}`, function () {
pm.response.to.have.status(expected);
});
}
checkStatus(200);What will be the test result if the response status is 404?
function checkStatus(expected) {
pm.test(`Status is ${expected}`, function () {
pm.response.to.have.status(expected);
});
}
checkStatus(200);Think about what happens when the expected status does not match the actual response status.
The test checks if the response status equals 200. Since the actual status is 404, the assertion fails, causing the test to fail with the message 'Status is 200'.
You want to reuse the variable userId in multiple assertions in Postman tests. Which option correctly asserts that the response JSON has userId equal to 5?
const userId = 5; pm.test('User ID is correct', () => { // assertion here });
Remember to use the variable name without quotes to refer to its value.
Option D correctly uses the variable userId to compare the JSON property. Option D compares to the string 'userId', which is incorrect. Option D hardcodes 5, which is not reusing the variable. Option D compares to string '5', which may fail if the JSON property is a number.
Look at this reusable test script snippet intended to check response time:
function checkResponseTime(maxTime) {
pm.test('Response time is acceptable', () => {
pm.expect(pm.response.responseTime).to.be.below(maxTime);
});
}
checkResponseTime(200);When running, the test always passes even if response time is above 200ms. Why?
function checkResponseTime(maxTime) {
pm.test('Response time is acceptable', () => {
pm.expect(pm.response.responseTime).to.be.below(maxTime);
});
}
checkResponseTime(200);Check the units of pm.response.responseTime and the argument maxTime.
Postman reports pm.response.responseTime in milliseconds. If maxTime is given in seconds (e.g., 200 means 200 seconds), the test will always pass because responseTime is much smaller. The mismatch in units causes the logic error.
You want to reuse test scripts across multiple requests in a Postman collection. Which approach is best practice?
Think about maintainability and avoiding duplication.
Option A is best practice: defining reusable functions in the collection Tests tab allows all requests in the collection to call them, promoting reuse and easier maintenance. Copy-pasting duplicates code. Using environment variables with eval is error-prone and insecure. Pre-request scripts run before requests, not suitable for test assertions.
Choose the best explanation for why reusable test scripts improve API testing in Postman.
Think about maintainability and efficiency in test writing.
Reusable test scripts help reduce repeated code, so when you update a test logic, you do it once and all tests benefit. This improves maintainability and consistency. They do not affect test speed, offline capability, or guarantee coverage.