Using Chai assertion library in Postman - Build an Automation Script
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