Check every item in an array response
Most list tests check items[0] and move on. The bug is usually in item 37: the one with a null price, the archived record that shouldn't be there, the order from another customer. Checking every item takes a few more lines and catches all of them.
The naive version, and why it isn't enough
const items = pm.response.json().data;
pm.test('items have a price', () => {
items.forEach((item) => pm.expect(item.price).to.be.a('number'));
});This works, but it stops at the first bad item and says Expected null to be a number. Which item? You'll open the response and search.
Collect failures, then assert once
const items = pm.response.json().data;
pm.test('list is not empty', () => {
pm.expect(items).to.be.an('array').that.is.not.empty;
});
pm.test('every item has a numeric price', () => {
const bad = items
.filter((item) => typeof item.price !== 'number')
.map((item) => `${item.id} (price: ${JSON.stringify(item.price)})`);
pm.expect(bad.slice(0, 5)).to.be.empty;
});When it fails, the message lists the bad items: Expected ["prd_19 (price: null)","prd_44 (price: \"12.00\")"] to be empty. That reads like a bug report. Cutting the list to five keeps it readable when a whole page is broken.
A helper for several rules
When you have more than one rule, put them in a small table and loop over it:
const items = pm.response.json().data;
const rules = [
['has an id', (i) => typeof i.id === 'string' && i.id.startsWith('prd_')],
['price is a positive number', (i) => typeof i.price === 'number' && i.price > 0],
['currency is ISO 4217', (i) => /^[A-Z]{3}$/.test(i.currency)],
['not archived', (i) => i.status !== 'archived'],
];
for (const [name, ok] of rules) {
pm.test(`every item: ${name}`, () => {
const bad = items.filter((i) => !ok(i)).map((i) => i.id ?? '(no id)');
pm.expect(bad.slice(0, 5)).to.be.empty;
});
}Each rule is its own test, so the run report shows exactly which rule broke, and the others still run.
Filters: every item should match the query
If you asked for ?status=active&customerId=cus_42, every item should match both:
const status = pm.request.url.getQueryString().match(/status=([^&]+)/)?.[1];
const items = pm.response.json().data;
pm.test(`every item has status ${status}`, () => {
const bad = items.filter((i) => i.status !== status).map((i) => i.id);
pm.expect(bad).to.be.empty;
});
pm.test('every item belongs to the customer', () => {
const customerId = pm.environment.get('customerId');
const bad = items.filter((i) => i.customerId !== customerId).map((i) => i.id);
pm.expect(bad).to.be.empty;
});The second test is also a cheap authorization check. More on that in can user B read user A's data?.
Nested arrays
Orders with line items, posts with comments: flatten first, then check.
const orders = pm.response.json().data;
const lines = orders.flatMap((o) => o.items.map((line) => ({ order: o.id, ...line })));
pm.test('every line item has a quantity of at least 1', () => {
const bad = lines.filter((l) => !(l.quantity >= 1)).map((l) => `${l.order}/${l.sku}`);
pm.expect(bad).to.be.empty;
});Without a script
If one rule is enough, the request's Tests tab can check a single field without code, for example $.data.length greater than 0, or $.data[0].price is type number. For a rule on every item, the script above is the way.
Speed
A loop over a few thousand items takes milliseconds. What slows tests down is a huge response. If a list endpoint returns 10,000 items by default, ask for a page (?limit=100) in the test and check pagination separately. See how to test API pagination.