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.
When you want to confirm an API returns the right status code like 200 or 404.
When you need to check if the response body contains expected data.
When you want to verify headers in the API response.
When you want to make sure a value is true, false, or equals something specific.
When you want clear messages about what failed in your tests.
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
Checks if the API response status code is exactly 200.
Postman
pm.test('Status code is 200', function () {
pm.response.to.have.status(200);
});
Verifies the JSON response contains a userId field equal to 5.
Postman
pm.test('Response has userId 5', function () {
const jsonData = pm.response.json();
pm.expect(jsonData.userId).to.equal(5);
});
Checks if the response text includes the word 'success'.
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;
});
OutputSuccess
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/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
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 B
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
Step 1: Identify the correct Postman response object
The response object in Postman is accessed via pm.response.
Step 2: Use Chai syntax to check status code
The correct Chai assertion is pm.expect(pm.response.code).to.equal(200);.
Final Answer:
pm.expect(pm.response.code).to.equal(200); -> Option C
Quick Check:
Status code check uses pm.response.code [OK]
Hint: Use pm.response.code with to.equal for status [OK]
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
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 D
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
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.
Step 2: Confirm Chai syntax correctness
The to.equal syntax is correct and pm.test is used properly.
Final Answer:
pm.response.status is not the correct property for status code -> Option A
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?