Bird
Raised Fist0
Postmantesting~10 mins

Response body assertions in Postman - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if the response status is 200.

Postman
pm.test("Status code is 200", function () {
    pm.response.to.have.status([1]);
});
Drag options to blanks, or click blank then click option'
A500
B404
C201
D200
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong status code like 404 or 500.
Using a string instead of a number.
2fill in blank
medium

Complete the code to assert that the response body contains the string 'success'.

Postman
pm.test("Body contains success", function () {
    pm.expect(pm.response.text()).to.include([1]);
});
Drag options to blanks, or click blank then click option'
A"error"
B"success"
C"fail"
D"pending"
Attempts:
3 left
💡 Hint
Common Mistakes
Using a word that is not in the response body.
Forgetting the quotes around the string.
3fill in blank
hard

Fix the error in the code to correctly assert that the JSON response has a property 'id'.

Postman
pm.test("Response has id", function () {
    const jsonData = pm.response.json();
    pm.expect(jsonData).to.have.property([1]);
});
Drag options to blanks, or click blank then click option'
A"id"
B'jsonData.id'
CjsonData.id
Did
Attempts:
3 left
💡 Hint
Common Mistakes
Using the property name without quotes.
Using the value of the property instead of the name.
4fill in blank
hard

Fill both blanks to assert that the response JSON has a property 'name' and its value equals 'John'.

Postman
pm.test("Name is John", function () {
    const jsonData = pm.response.json();
    pm.expect(jsonData).to.have.property([1], [2]);
});
Drag options to blanks, or click blank then click option'
A"name"
B"John"
C'John'
D'name'
Attempts:
3 left
💡 Hint
Common Mistakes
Using property name without quotes.
Using value without quotes or wrong quotes.
5fill in blank
hard

Fill all three blanks to assert that the response JSON array 'users' has length greater than 0 and the first user's 'active' property is true.

Postman
pm.test("Users array is not empty and first user is active", function () {
    const jsonData = pm.response.json();
    pm.expect(jsonData.[1]).to.be.an('array').that.has.length.[2](0);
    pm.expect(jsonData.users[0].[3]).to.be.true;
});
Drag options to blanks, or click blank then click option'
Ausers
Babove
Cactive
Dlength
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'length' instead of 'users' for the array.
Using 'length' instead of 'above' for the length check.
Checking wrong property name instead of 'active'.

Practice

(1/5)
1. What does pm.response.json() do in Postman tests?
easy
A. It parses the response body as a JSON object.
B. It sends a new request to the server.
C. It clears the response body.
D. It validates the response status code.

Solution

  1. Step 1: Understand the purpose of pm.response.json()

    This function reads the response body and converts it into a JSON object for easy access.
  2. Step 2: Compare with other options

    Sending requests, clearing body, or validating status are different functions, not pm.response.json().
  3. Final Answer:

    It parses the response body as a JSON object. -> Option A
  4. Quick Check:

    Parsing response body = A [OK]
Hint: Remember: json() reads response body as JSON [OK]
Common Mistakes:
  • Confusing json() with sending requests
  • Thinking json() clears data
  • Mixing response body parsing with status code checks
2. Which of the following is the correct syntax to assert that the response JSON has a key status with value success in Postman?
easy
A. pm.expect(response.status).to.equal('success');
B. pm.response.json().status == 'success';
C. pm.expect(pm.response.json().status).to.eql('success');
D. pm.assert(pm.response.status == 'success');

Solution

  1. Step 1: Identify correct assertion syntax in Postman

    Postman uses pm.expect() with Chai assertion style, so pm.expect(pm.response.json().status).to.eql('success'); is correct.
  2. Step 2: Check other options for errors

    pm.response.json().status == 'success'; lacks assertion, C uses wrong object, D uses incorrect method.
  3. Final Answer:

    pm.expect(pm.response.json().status).to.eql('success'); -> Option C
  4. Quick Check:

    Use pm.expect() with to.eql() for value check [OK]
Hint: Use pm.expect() with to.eql() for JSON value checks [OK]
Common Mistakes:
  • Using == instead of pm.expect() for assertions
  • Referencing response.status instead of response.json().status
  • Using pm.assert() which is not a Postman function
3. Given this response body:
{"user":{"id":5,"name":"Alice"}}
What will this test output?
const jsonData = pm.response.json();
pm.test("User ID is 5", () => {
  pm.expect(jsonData.user.id).to.equal(5);
});
medium
A. Test passes because user.id equals 5.
B. Test fails because user.id is not 5.
C. Test throws an error due to syntax.
D. Test is skipped because no assertion is made.

Solution

  1. Step 1: Parse the response JSON

    The response has user.id = 5, so jsonData.user.id is 5.
  2. Step 2: Evaluate the assertion

    The test asserts jsonData.user.id equals 5, which is true, so the test passes.
  3. Final Answer:

    Test passes because user.id equals 5. -> Option A
  4. Quick Check:

    Value matches assertion = Pass [OK]
Hint: Match JSON path value with expected to pass test [OK]
Common Mistakes:
  • Misreading JSON structure
  • Assuming test fails without checking value
  • Confusing syntax errors with assertion failures
4. Identify the error in this Postman test code:
const data = pm.response.json();
pm.test("Check user name", () => {
  pm.expect(data.user.name).to.equal('Bob')
});
medium
A. Missing semicolon after assertion line.
B. No error; the test code is correct.
C. Incorrect function name; should be pm.test, not pm.tests.
D. Missing parentheses after pm.expect.

Solution

  1. Step 1: Review syntax of Postman test code

    The code uses pm.test correctly, with proper arrow function and assertion syntax.
  2. Step 2: Check for syntax errors

    Semicolons are optional in JavaScript; parentheses and function names are correct.
  3. Final Answer:

    No error; the test code is correct. -> Option B
  4. Quick Check:

    Correct syntax means no error [OK]
Hint: Check function names and parentheses carefully [OK]
Common Mistakes:
  • Confusing pm.test with pm.tests
  • Thinking semicolons are mandatory
  • Missing parentheses in pm.expect
5. You want to assert that the response JSON array items contains an object with id equal to 10. Which test code correctly checks this in Postman?
hard
A. pm.expect(pm.response.json().items.id).to.equal(10);
B. const items = pm.response.json().items; pm.expect(items.find(id => id === 10)).to.exist;
C. pm.expect(pm.response.json().items.includes({id:10})).to.be.true;
D. const items = pm.response.json().items; pm.expect(items.some(item => item.id === 10)).to.be.true;

Solution

  1. Step 1: Understand the response structure

    items is an array of objects; we want to check if any object has id 10.
  2. Step 2: Evaluate each option

    const items = pm.response.json().items; pm.expect(items.some(item => item.id === 10)).to.be.true; uses some() to check if any item has id === 10, which is correct. pm.expect(pm.response.json().items.id).to.equal(10); wrongly accesses items.id (invalid). pm.expect(pm.response.json().items.includes({id:10})).to.be.true; tries to use includes() with an object, which won't work. const items = pm.response.json().items; pm.expect(items.find(id => id === 10)).to.exist; uses find() but the callback is incorrect (should check item.id).
  3. Final Answer:

    const items = pm.response.json().items; pm.expect(items.some(item => item.id === 10)).to.be.true; -> Option D
  4. Quick Check:

    Use some() with correct callback for array check [OK]
Hint: Use some() to check if array contains object with property [OK]
Common Mistakes:
  • Using includes() with objects (doesn't work)
  • Accessing array properties directly
  • Incorrect callback function in find()