0
0
Postmantesting~20 mins

Response body assertions in Postman - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Response Body Assertion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
assertion
intermediate
2:00remaining
Check if response JSON contains a specific key with expected value
Given a JSON response body, which Postman test script correctly asserts that the key status has the value success?
Postman
pm.test('Status is success', function () {
    // Write assertion here
});
Apm.expect(pm.response.json().status).to.eql('success');
Bpm.expect(pm.response.json().status).to.be.true();
Cpm.expect(pm.response.json().status).to.equal(true);
Dpm.expect(pm.response.json().status).to.be('success');
Attempts:
2 left
💡 Hint
Use the correct assertion method to compare values exactly.
assertion
intermediate
2:00remaining
Verify response body contains a non-empty array
Which Postman test script correctly asserts that the response JSON has a key items which is an array with at least one element?
Postman
pm.test('Items array is not empty', function () {
    // Write assertion here
});
Apm.expect(pm.response.json().items).to.be.an('array').that.is.not.empty;
Bpm.expect(pm.response.json().items).to.have.lengthOf.above(0);
Cpm.expect(pm.response.json().items.length).to.be.above(0);
Dpm.expect(pm.response.json().items).to.have.length.above(0);
Attempts:
2 left
💡 Hint
Use chaining to check type and emptiness.
Predict Output
advanced
2:00remaining
Output of response body assertion with nested JSON
What will be the result of this Postman test script if the response body is {"user":{"id":123,"active":true}}?
Postman
pm.test('User is active', function () {
    pm.expect(pm.response.json().user.active).to.be.false;
});
ATest passes because active is true
BTest fails with assertion error because active is true, not false
CSyntaxError due to incorrect assertion method
DTest fails with TypeError because user key is missing
Attempts:
2 left
💡 Hint
Check the actual value of the active key and the assertion used.
🔧 Debug
advanced
2:00remaining
Identify the error in response body assertion script
What is wrong with this Postman test script if the response body is {"data":{"count":5}}?
pm.test('Count is 5', function () {
    pm.expect(pm.response.json().count).to.eql(5);
});
ATest will pass because count equals 5
BSyntaxError due to missing semicolon
CAssertion method to.eql() is invalid
Dpm.response.json().count is undefined because count is nested inside data
Attempts:
2 left
💡 Hint
Check the JSON path used to access the count key.
🧠 Conceptual
expert
3:00remaining
Best practice for asserting optional keys in response body
In Postman tests, what is the best way to assert that an optional key message in the response body is either absent or a non-empty string if present?
AFail the test if <code>message</code> key is missing
BAlways assert <code>message</code> is a non-empty string without checking existence
CUse a conditional check: if <code>message</code> exists, assert it is a non-empty string; else pass the test
DIgnore the <code>message</code> key entirely in tests
Attempts:
2 left
💡 Hint
Think about how to handle keys that may or may not be present.