How to test API pagination
Pagination looks finished as soon as page one comes back. The bugs show up later: the last page repeats an item, the page after the end returns 500, a page size of 10,000 takes the database down, or a new record pushes an item from page 2 back onto page 1.
Here's how to test the three common styles with a handful of requests.
The three styles
| Style | Request | Next page |
|---|---|---|
| Offset | ?offset=40&limit=20 | offset + limit |
| Page number | ?page=3&per_page=20 | page + 1 |
| Cursor | ?cursor=eyJpZCI6NDJ9&limit=20 | The next_cursor in the response |
Offset and page APIs are easy to test with variables. Cursor APIs need a script, because the next request depends on the last response.
The edge cases
Make a request per case, with the page and size as variables:
GET {{baseUrl}}/orders?page={{page}}&per_page={{perPage}}Then check each one. A data file is the tidiest way to run them all:
page,perPage,expectedStatus,expectedCount
1,20,200,20
2,20,200,20
999,20,200,0
0,20,400,
1,0,400,
1,1,200,1
1,100,200,
1,101,400,pm.test('status matches the case', () => {
pm.expect(pm.response.code).to.equal(Number(pm.environment.get('expectedStatus')));
});
pm.test('page has the expected number of items', () => {
const expected = pm.environment.get('expectedCount');
if (!expected || pm.response.code !== 200) return;
pm.expect(pm.response.json().data).to.have.lengthOf(Number(expected));
});Check the metadata
Most APIs return totals or links alongside the data. Check they agree with each other:
const body = pm.response.json();
pm.test('page metadata is consistent', () => {
pm.expect(body.data.length).to.be.at.most(body.per_page);
pm.expect(body.total_pages).to.equal(Math.ceil(body.total / body.per_page));
});
pm.test('no next link on the last page', () => {
if (body.page === body.total_pages) pm.expect(body.next).to.be.oneOf([null, undefined]);
});Walk all the pages
The bug that hurts most is an item that's skipped or repeated between pages, usually because of an unstable sort order. To catch it, walk the pages in one run and collect the IDs as you go.
Make a folder called Pagination walk, give it a folder variable page with the value 1, and put the list request in it, using {{page}}. Duplicate the request until there are enough copies to reach the end of your test data (five copies of a 20-item page cover 100 records). Give every copy the same post-response script:
// Post-response script of each "List orders (walk)" request
const ids = JSON.parse(pm.environment.get('seenIds') || '[]');
const page = pm.response.json().data.map((o) => o.id);
pm.test('no item repeats across pages', () => {
const repeated = page.filter((id) => ids.includes(id));
pm.expect(repeated).to.be.empty; // the failure message lists the repeated IDs
});
pm.environment.set('seenIds', JSON.stringify(ids.concat(page)));
pm.environment.set('page', String(Number(pm.environment.get('page') || 1) + 1));requestarium run-collection "Orders API" --folder "Pagination walk" --env StagingValues set by a script are passed to the requests after it in the same run, so each copy asks for the next page and checks it against everything seen so far. If your API returns a total, the last copy can also check that the number of IDs collected equals it.
Cursor pagination
With cursors, chain the requests: store next_cursor from one response and use it in the next.
const body = pm.response.json();
pm.test('has a cursor while there is more', () => {
if (body.has_more) pm.expect(body.next_cursor).to.be.a('string').and.not.empty;
});
pm.environment.set('cursor', body.next_cursor || '');GET {{baseUrl}}/events?limit=20&cursor={{cursor}}Also test a cursor that's garbage (?cursor=abc) and one that's expired, if your cursors expire. Both should be a 400 with a clear message, not a 500.
Data that changes while you page
Offset pagination has a known weakness: if a record is inserted before your offset between two requests, an item shifts and appears twice. If that matters for your API, test it: fetch page 1, create a new record, fetch page 2, and check whether the first item of page 2 already appeared. Then decide whether to accept it, document it, or move to cursors.