0
0
Postmantesting~20 mins

Test utilities and helpers in Postman - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Postman Test Utilities Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Postman test script?
Consider this Postman test script that uses a helper function to check response status. What will be the test result after running this script if the response status is 404?
Postman
function checkStatus(expected) {
  pm.test(`Status is ${expected}`, function () {
    pm.response.to.have.status(expected);
  });
}

checkStatus(200);
ATest passes because the function ignores the actual status
BTest fails with message 'Status is 200' because actual status is 404
CTest fails with a syntax error in the function
DTest passes because 404 is treated as 200 internally
Attempts:
2 left
💡 Hint
Think about what pm.response.to.have.status(expected) does when the actual status differs from expected.
assertion
intermediate
2:00remaining
Which assertion correctly verifies a JSON response property using a helper?
You have a helper function that asserts a JSON property value in Postman tests. Which option correctly asserts that the response JSON has a property 'success' equal to true?
Postman
function assertPropertyEquals(property, expectedValue) {
  pm.test(`Property ${property} equals ${expectedValue}`, function () {
    pm.expect(pm.response.json()[property]).to.eql(expectedValue);
  });
}
AassertPropertyEquals('success', 1);
BassertPropertyEquals(success, 'true');
CassertPropertyEquals('success', 'true');
DassertPropertyEquals('success', true);
Attempts:
2 left
💡 Hint
Remember the difference between boolean true and string 'true' in JSON and JavaScript.
🔧 Debug
advanced
2:00remaining
Why does this Postman test helper cause a runtime error?
This helper function is intended to check if a response header exists. Why does it cause a runtime error when used in a test?
Postman
function checkHeaderExists(headerName) {
  pm.test(`Header ${headerName} exists`, function () {
    pm.expect(pm.response.headers.get(headerName).length).to.be.above(0);
  });
}

checkHeaderExists('Content-Type');
Apm.response.headers.get(headerName) returns undefined if header missing, causing .length to fail
Bpm.expect is not defined in Postman test scripts
Cpm.response.headers.get requires a second argument for case sensitivity
Dpm.test cannot be called inside a function
Attempts:
2 left
💡 Hint
What happens if the header is not present? What does .get() return?
🧠 Conceptual
advanced
2:00remaining
What is the main benefit of using test utility functions in Postman?
Why should you create and use test utility functions or helpers in your Postman test scripts?
ATo avoid writing any assertions manually
BTo make tests run faster by skipping assertions
CTo reduce code duplication and improve test maintainability
DTo automatically generate test reports without configuration
Attempts:
2 left
💡 Hint
Think about how helpers affect code reuse and clarity.
framework
expert
3:00remaining
How to implement a reusable Postman test helper for validating JSON schema?
You want to create a reusable helper function in Postman to validate the response JSON against a given JSON schema. Which code snippet correctly implements this using the Ajv library available in Postman sandbox?
Postman
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;
  });
}
AUse ajv.compile(schema) once inside the test and validate pm.response.json() with pm.expect(valid).to.be.true
BCall ajv.validate(schema, pm.response.json()) inside pm.test and assert the result with pm.expect
CUse pm.response.to.have.jsonSchema(schema) directly without Ajv
DUse JSON.parse(pm.response.text()) and compare keys manually without Ajv
Attempts:
2 left
💡 Hint
Ajv requires compiling the schema before validation. Check how to assert validation results.