Chai helps you check if your API responses are correct by making easy-to-read tests. It tells you if something is wrong so you can fix it fast.
Using Chai assertion library in Postman
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Postman
pm.test('Test description', function () {
pm.expect(actualValue).to.equal(expectedValue);
});pm.test defines a test with a description.
pm.expect uses Chai's assertion style to check values.
Examples
Postman
pm.test('Status code is 200', function () { pm.response.to.have.status(200); });
Postman
pm.test('Response has userId 5', function () { const jsonData = pm.response.json(); pm.expect(jsonData.userId).to.equal(5); });
Postman
pm.test('Response body contains success message', function () { pm.expect(pm.response.text()).to.include('success'); });
Sample Program
This test checks the API returns status 200, the response JSON has a 'name' property which is a string, and an 'active' property that is true.
Postman
pm.test('Verify API response status and content', function () { pm.response.to.have.status(200); const jsonData = pm.response.json(); pm.expect(jsonData).to.have.property('name'); pm.expect(jsonData.name).to.be.a('string'); pm.expect(jsonData.active).to.be.true; });
Important Notes
Use clear and simple test descriptions so you know what each test checks.
Chai assertions give detailed error messages to help find problems quickly.
Always parse JSON responses before checking their properties.
Summary
Chai makes writing tests easy and readable in Postman.
Use pm.test and pm.expect to create clear checks for your API responses.
Good assertions help catch bugs early and improve API quality.
Practice
1. What is the main purpose of using the Chai assertion library in Postman tests?
easy
Solution
Step 1: Understand Chai's role in testing
Chai is used to write assertions that check if API responses meet expectations.Step 2: Identify the correct purpose in Postman context
In Postman, Chai helps create readable tests that verify API behavior.Final Answer:
To write clear and readable checks for API responses -> Option BQuick 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
Solution
Step 1: Identify the correct Postman response object
The response object in Postman is accessed viapm.response.Step 2: Use Chai syntax to check status code
The correct Chai assertion ispm.expect(pm.response.code).to.equal(200);.Final Answer:
pm.expect(pm.response.code).to.equal(200); -> Option CQuick 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:
What will happen if the response body is
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
Solution
Step 1: Understand the assertion used
The assertion checks if the response text includes the substring 'success'.Step 2: Check if 'success' is in 'Operation was successful'
The word 'successful' contains 'success' as a substring, so the assertion passes.Final Answer:
Test will pass because 'success' is included in the response -> Option DQuick 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
Solution
Step 1: Check the property used for status code
The correct property for status code in Postman ispm.response.code, notpm.response.status.Step 2: Confirm Chai syntax correctness
Theto.equalsyntax is correct andpm.testis used properly.Final Answer:
pm.response.status is not the correct property for status code -> Option AQuick 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
Solution
Step 1: Parse JSON response correctly
Usepm.response.json()to get the response as an object.Step 2: Use Chai's 'above' assertion on userId property
Check thatjsonData.userIdis greater than 100 withto.be.above(100).Final Answer:
pm.test('userId > 100', () => { const jsonData = pm.response.json(); pm.expect(jsonData.userId).to.be.above(100); }); -> Option AQuick 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
