function checkStatus(expected) {
pm.test(`Status is ${expected}`, function () {
pm.response.to.have.status(expected);
});
}
checkStatus(200);The helper function checkStatus creates a test that expects the response status to be exactly the expected value. Since the response status is 404 but the function checks for 200, the test will fail with the message 'Status is 200'.
function assertPropertyEquals(property, expectedValue) {
pm.test(`Property ${property} equals ${expectedValue}`, function () {
pm.expect(pm.response.json()[property]).to.eql(expectedValue);
});
}Option D correctly passes the property name as a string and the expected boolean value true. Options B and C use incorrect types or missing quotes, and D uses a number instead of boolean.
function checkHeaderExists(headerName) {
pm.test(`Header ${headerName} exists`, function () {
pm.expect(pm.response.headers.get(headerName).length).to.be.above(0);
});
}
checkHeaderExists('Content-Type');If the header is missing, pm.response.headers.get(headerName) returns undefined. Calling .length on undefined causes a runtime error.
Test utility functions help reduce repeated code and make tests easier to update and maintain. They do not skip assertions or generate reports automatically.
const Ajv = require('ajv'); const ajv = new Ajv(); function validateSchema(schema) { pm.test('Response matches JSON schema', function () { const validate = ajv.compile(schema); const valid = validate(pm.response.json()); pm.expect(valid, JSON.stringify(validate.errors)).to.be.true; }); }
Option A correctly matches the provided code, which compiles the schema with ajv.compile(schema), validates the response JSON, and asserts the result. Option A describes using ajv.validate(schema, pm.response.json()), which is also valid but different from the code. Option A uses Postman's built-in pm.response.to.have.jsonSchema(schema) without explicit Ajv. Option A is manual and error-prone.