20 / 65 · 14 API Testing Fundamentals · Response Validation← prev⊞ allnext →☰ Read as one page
3.4Pagination Testing
Pagination is a common source of bugs: overlapping pages, missing items, incorrect totals.
def test_pagination_no_overlap(api):
"""Pages should not contain overlapping items."""
r1 = api.get("/users?page=1&per_page=10")
r2 = api.get("/users?page=2&per_page=10")
ids1 = {u["id"] for u in r1.json()["items"]}
ids2 = {u["id"] for u in r2.json()["items"]}
assert ids1.isdisjoint(ids2), f"Overlapping IDs: {ids1 & ids2}"
def test_pagination_total_consistency(api):
"""Total count should be consistent across pages."""
r1 = api.get("/users?page=1&per_page=10")
r2 = api.get("/users?page=2&per_page=10")
assert r1.json()["total"] == r2.json()["total"]
def test_pagination_all_items_covered(api):
"""Collecting all pages should yield exactly 'total' items."""
all_ids = set()
page = 1
total = None
while True:
r = api.get(f"/users?page={page}&per_page=50")
data = r.json()
total = data["total"]
items = data["items"]
if not items:
break
all_ids.update(u["id"] for u in items)
page += 1
assert len(all_ids) == total
def test_pagination_boundary(api):
"""Request a page beyond the last page."""
r = api.get("/users?page=99999&per_page=10")
assert r.status_code == 200
assert r.json()["items"] == []
def test_pagination_invalid_params(api):
"""Invalid pagination parameters should return errors."""
r = api.get("/users?page=0&per_page=10")
assert r.status_code == 400
r = api.get("/users?page=1&per_page=0")
assert r.status_code == 400
r = api.get("/users?page=-1&per_page=10")
assert r.status_code == 400