Using Chai assertion library in Postman - Build an Automation Script
Start learning this pattern below
Jump into concepts and practice - no test required
pm.test('Status code is 200', () => { pm.response.to.have.status(200); }); const responseJson = pm.response.json(); pm.test('userId is 1', () => { pm.expect(responseJson.userId, 'userId should be 1').to.eql(1); }); pm.test('title is a non-empty string', () => { pm.expect(responseJson.title, 'title should be a non-empty string').to.be.a('string').and.not.empty; });
The first test checks the HTTP status code is exactly 200 using pm.response.to.have.status(200). This confirms the request succeeded.
Next, we parse the response JSON once with pm.response.json() and store it in responseJson to avoid repeated parsing.
The second test asserts that the userId property equals 1 using pm.expect().to.eql() with a clear message.
The third test verifies the title property is a string and not empty using chained Chai assertions .to.be.a('string').and.not.empty. This ensures the title is valid text.
Each assertion has a descriptive message to help understand failures. This script follows Postman and Chai best practices for clarity and efficiency.
Now add data-driven testing with 3 different post IDs (1, 2, 3) verifying the same assertions for each
Practice
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]
- Confusing Chai with request sending
- Thinking Chai creates UI elements
- Mixing Chai with environment variable storage
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]
- Using wrong object like response.code
- Using to.be instead of to.equal
- Missing pm.response prefix
pm.test('Check response body', () => {
pm.expect(pm.response.text()).to.include('success');
});What will happen if the response body is
'Operation was successful'?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]
- Expecting exact match instead of substring
- Confusing syntax error with assertion failure
- Assuming status code affects this test
pm.test('Status is 404', () => {
pm.expect(pm.response.status).to.equal(404);
});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]
- Using pm.response.status instead of pm.response.code
- Adding unnecessary 'to.be' in assertion
- Confusing pm.test with pm.expect
userId with a value greater than 100. Which code snippet correctly uses Chai assertions to do this?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]
- Using to.equal instead of to.be.above
- Checking response text instead of JSON property
- Checking status code instead of userId
