pm.test('Status code is 200', () => {
pm.response.to.have.status(200);
});
pm.test('Response body is an array of 5 items', () => {
const jsonData = pm.response.json();
pm.expect(jsonData).to.be.an('array', 'Response is not an array');
pm.expect(jsonData.length).to.eql(5, 'Response array length is not 5');
});
pm.test('All items have category "books"', () => {
const jsonData = pm.response.json();
jsonData.forEach(item => {
pm.expect(item.category).to.eql('books', `Item category is not 'books': ${JSON.stringify(item)}`);
});
});The first test checks that the response status code is 200, confirming the request was successful.
The second test parses the response body as JSON and verifies it is an array with exactly 5 items, matching the 'limit' query parameter.
The third test loops through each item in the response array and asserts that the 'category' field equals 'books', matching the 'category' query parameter.
Using descriptive messages helps identify which assertion failed if the test does not pass.