Bird
Raised Fist0
Postmantesting~5 mins

Using Chai assertion library in Postman - Cheat Sheet & Quick Revision

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
Recall & Review
beginner
What is the Chai assertion library used for in Postman tests?
Chai is used to write clear and readable assertions to check if API responses meet expected conditions during Postman tests.
Click to reveal answer
beginner
How do you write a basic assertion to check if a response status code is 200 using Chai in Postman?
pm.test('Status code is 200', function () { pm.response.to.have.status(200); });
Click to reveal answer
intermediate
What does the Chai assertion 'expect(response).to.have.property("name")' check for?
It checks that the response object has a property called "name".
Click to reveal answer
intermediate
Explain the difference between 'expect' and 'assert' styles in Chai.
Both are assertion styles in Chai. 'expect' reads like natural language (e.g., expect(value).to.equal(5)), while 'assert' uses functions (e.g., assert.equal(value, 5)). Both check conditions but differ in syntax.
Click to reveal answer
beginner
How can you check if a JSON response body contains a specific string using Chai in Postman?
Use: pm.test('Body contains string', function () { pm.expect(pm.response.text()).to.include('expected string'); });
Click to reveal answer
Which Chai assertion checks if a response status is 404?
Apm.response.status(404);
Bpm.response.to.be.status(404);
Cpm.response.to.have.status(404);
Dpm.response.has.status(404);
What does this assertion check? pm.expect(responseJson.success).to.be.true;
AThat responseJson.success is a string
BThat responseJson.success is true
CThat responseJson.success is false
DThat responseJson.success exists
Which Chai style uses natural language syntax?
Aexpect
Bassert
Cshould
Dverify
How do you check if response body contains the word 'success'?
Apm.expect(pm.response.text()).to.include('success');
Bpm.response.to.have.body('success');
Cpm.expect(pm.response.body).to.have.string('success');
Dpm.response.body.includes('success');
What is the purpose of pm.test() in Postman tests?
ATo set environment variables
BTo send an API request
CTo log response data
DTo define a test case with a name and function
Describe how to write a Chai assertion in Postman to verify the response status code and a JSON property value.
Think about checking status first, then accessing JSON body and asserting a property.
You got /4 concepts.
    Explain the difference between 'expect' and 'assert' styles in Chai and when you might prefer one over the other.
    Consider how each style reads and how easy it is to understand.
    You got /4 concepts.

      Practice

      (1/5)
      1. What is the main purpose of using the Chai assertion library in Postman tests?
      easy
      A. To create user interface elements
      B. To write clear and readable checks for API responses
      C. To send HTTP requests faster
      D. To store environment variables

      Solution

      1. Step 1: Understand Chai's role in testing

        Chai is used to write assertions that check if API responses meet expectations.
      2. Step 2: Identify the correct purpose in Postman context

        In Postman, Chai helps create readable tests that verify API behavior.
      3. Final Answer:

        To write clear and readable checks for API responses -> Option B
      4. Quick Check:

        Chai assertions = readable API checks [OK]
      Hint: Chai is for checking API results clearly [OK]
      Common Mistakes:
      • Confusing Chai with request sending
      • Thinking Chai creates UI elements
      • Mixing Chai with environment variable storage
      2. Which of the following is the correct syntax to assert that the response status code is 200 using Chai in Postman?
      easy
      A. pm.expect(response.code).to.equal(200);
      B. pm.expect(pm.response.status).to.equal(200);
      C. pm.expect(pm.response.code).to.equal(200);
      D. pm.expect(response.status).to.be(200);

      Solution

      1. Step 1: Identify the correct Postman response object

        The response object in Postman is accessed via pm.response.
      2. Step 2: Use Chai syntax to check status code

        The correct Chai assertion is pm.expect(pm.response.code).to.equal(200);.
      3. Final Answer:

        pm.expect(pm.response.code).to.equal(200); -> Option C
      4. Quick Check:

        Status code check uses pm.response.code [OK]
      Hint: Use pm.response.code with to.equal for status [OK]
      Common Mistakes:
      • Using wrong object like response.code
      • Using to.be instead of to.equal
      • Missing pm.response prefix
      3. Given this Postman test code:
      pm.test('Check response body', () => {
        pm.expect(pm.response.text()).to.include('success');
      });

      What will happen if the response body is 'Operation was successful'?
      medium
      A. Test will pass only if response code is 200
      B. Test will fail because 'success' is not exactly matched
      C. Test will throw a syntax error
      D. Test will pass because 'success' is included in the response

      Solution

      1. Step 1: Understand the assertion used

        The assertion checks if the response text includes the substring 'success'.
      2. Step 2: Check if 'success' is in 'Operation was successful'

        The word 'successful' contains 'success' as a substring, so the assertion passes.
      3. Final Answer:

        Test will pass because 'success' is included in the response -> Option D
      4. Quick Check:

        Substring check includes 'success' [OK]
      Hint: Include checks pass if substring exists anywhere [OK]
      Common Mistakes:
      • Expecting exact match instead of substring
      • Confusing syntax error with assertion failure
      • Assuming status code affects this test
      4. Identify the error in this Postman test code snippet:
      pm.test('Status is 404', () => {
        pm.expect(pm.response.status).to.equal(404);
      });
      medium
      A. pm.response.status is not the correct property for status code
      B. to.equal should be to.be.equal
      C. pm.test should be pm.expect
      D. Missing semicolon after pm.test

      Solution

      1. Step 1: Check the property used for status code

        The correct property for status code in Postman is pm.response.code, not pm.response.status.
      2. Step 2: Confirm Chai syntax correctness

        The to.equal syntax is correct and pm.test is used properly.
      3. Final Answer:

        pm.response.status is not the correct property for status code -> Option A
      4. Quick Check:

        Status code property is pm.response.code [OK]
      Hint: Use pm.response.code for status, not pm.response.status [OK]
      Common Mistakes:
      • Using pm.response.status instead of pm.response.code
      • Adding unnecessary 'to.be' in assertion
      • Confusing pm.test with pm.expect
      5. You want to write a Postman test that asserts the JSON response has a property userId with a value greater than 100. Which code snippet correctly uses Chai assertions to do this?
      hard
      A. pm.test('userId > 100', () => { const jsonData = pm.response.json(); pm.expect(jsonData.userId).to.be.above(100); });
      B. pm.test('userId > 100', () => { const jsonData = pm.response.json(); pm.expect(jsonData.userId).to.equal(100); });
      C. pm.test('userId > 100', () => { pm.expect(pm.response.text()).to.include('userId > 100'); });
      D. pm.test('userId > 100', () => { pm.expect(pm.response.code).to.be.above(100); });

      Solution

      1. Step 1: Parse JSON response correctly

        Use pm.response.json() to get the response as an object.
      2. Step 2: Use Chai's 'above' assertion on userId property

        Check that jsonData.userId is greater than 100 with to.be.above(100).
      3. Final Answer:

        pm.test('userId > 100', () => { const jsonData = pm.response.json(); pm.expect(jsonData.userId).to.be.above(100); }); -> Option A
      4. Quick Check:

        Parse JSON then assert property above value [OK]
      Hint: Parse JSON then use to.be.above for numeric checks [OK]
      Common Mistakes:
      • Using to.equal instead of to.be.above
      • Checking response text instead of JSON property
      • Checking status code instead of userId