Pagination is one of the most common yet overlooked areas in API testing. While developers typically test happy path scenarios—requesting page 1 with a valid page size—edge cases around pagination often receive minimal attention until production issues surface. This guide shows you how to use AI to generate pagination edge case tests that catch real-world bugs before they affect users.
Understanding Pagination Edge Cases
Before diving into AI-powered test generation, you need to understand what makes pagination testing challenging. APIs typically implement pagination using several approaches: offset-based, cursor-based, or page-based. Each approach has distinct edge cases that can break your API.
Common pagination edge cases include:
-
Zero results: What happens when the query returns no data?
-
Negative offsets: Passing
page=-1oroffset=-5 -
Excessive page sizes: Requesting
limit=1000000or larger than maximum allowed -
Boundary conditions: Requesting the last page, then one beyond
-
Empty pages: What happens when a page exists but contains no results after filtering?
-
Cursor expiration: In cursor-based pagination, what happens when the cursor becomes invalid?
Using AI to Generate Test Cases
Modern AI coding assistants can generate test suites when provided with the right context. Here’s how to structure your prompts for maximum effectiveness.
Step 1: Provide Your API Specification
Start by giving the AI your API documentation, OpenAPI spec, or endpoint signatures. Include the pagination parameters your API supports.
// Example API endpoint signature for a products list
interface ProductsListParams {
page?: number; // 1-indexed page number
pageSize?: number; // Items per page (max 100)
sortBy?: string; // Field to sort by
sortOrder?: 'asc' | 'desc';
cursor?: string; // For cursor-based pagination
}
Step 2: Request Specific Edge Case Categories
Prompt the AI to generate tests for each category systematically. A well-structured prompt produces better results than asking for “everything at once.”
Generate Jest tests for a products API endpoint that includes pagination.
Focus specifically on edge cases:
1. Boundary conditions: first page, last page, beyond last page
2. Invalid inputs: negative numbers, zero, non-numeric values, excessive sizes
3. Empty states: no results, filtered results with no matches
4. Cursor handling: invalid cursor, expired cursor, malformed cursor
5. Concurrency: multiple simultaneous requests to same endpoint
Step 3: Review and Refine Generated Tests
AI-generated tests require developer oversight. Review for:
-
Correctness: Does the test actually verify what it claims?
-
Completeness: Are critical edge cases missing?
-
Clarity: Can other developers understand what each test validates?
Practical Code Examples
Here’s an example of AI-generated pagination edge case tests using Jest:
describe('GET /api/products - Pagination Edge Cases', () => {
const baseUrl = 'http://localhost:3000/api/products';
describe('Boundary Conditions', () => {
test('should return empty array when requesting beyond total pages', async () => {
// First, determine total pages
const firstResponse = await fetch(`${baseUrl}?page=1&pageSize=10`);
const { total, pageSize } = await firstResponse.json();
const totalPages = Math.ceil(total / pageSize);
// Request beyond available data
const response = await fetch(`${baseUrl}?page=${totalPages + 1}&pageSize=10`);
const data = await response.json();
expect(response.status).toBe(200);
expect(data.items).toEqual([]);
});
test('should handle page size of 1 correctly', async () => {
const response = await fetch(`${baseUrl}?page=1&pageSize=1`);
const data = await response.json();
expect(data.items).toHaveLength(1);
expect(data.hasNext).toBe(true);
});
});
describe('Invalid Inputs', () => {
test('should reject negative page numbers', async () => {
const response = await fetch(`${baseUrl}?page=-1`);
expect(response.status).toBe(400);
const error = await response.json();
expect(error.message).toContain('page');
});
test('should reject page size of zero', async () => {
const response = await fetch(`${baseUrl}?pageSize=0`);
expect(response.status).toBe(400);
});
test('should cap excessive page sizes', async () => {
const response = await fetch(`${baseUrl}?pageSize=1000000`);
const data = await response.json();
// API should either reject or cap the value
expect(data.items.length).toBeLessThanOrEqual(100);
});
test('should reject non-numeric pagination parameters', async () => {
const response = await fetch(`${baseUrl}?page=abc`);
expect(response.status).toBe(400);
});
});
describe('Empty States', () => {
test('should return correct pagination metadata for zero results', async () => {
// Assuming category "nonexistent" has no products
const response = await fetch(`${baseUrl}?category=nonexistent`);
const data = await response.json();
expect(data.total).toBe(0);
expect(data.items).toEqual([]);
expect(data.hasNext).toBe(false);
expect(data.hasPrevious).toBe(false);
});
});
});
Advanced AI Testing Strategies
Property-Based Testing
For more coverage, ask AI tools to generate property-based tests using libraries like fast-check or jqwik. These tests verify that pagination properties hold across all valid inputs.
import { fc } from 'fast-check';
test('pagination invariants should hold for any valid page combination', () => {
fc.assert(
fc.property(
fc.integer({ min: 1, max: 1000 }),
fc.integer({ min: 1, max: 100 }),
(totalItems, pageSize) => {
const totalPages = Math.ceil(totalItems / pageSize);
// Test that we can always reach the last page
const lastPage = fetchPage(totalPages, pageSize);
expect(lastPage.items.length).toBeGreaterThan(0);
// Test that page beyond total returns empty
const beyondPage = fetchPage(totalPages + 10, pageSize);
expect(beyondPage.items).toEqual([]);
}
)
);
});
Generating Test Data
AI can also help generate the test data needed for pagination testing. Request scenarios with specific data distributions—datasets with exactly N items, datasets where filtering returns empty results, or datasets with many pages of data.
Best Practices for AI-Generated Pagination Tests
-
Always verify test assertions: AI can generate tests that pass for wrong reasons or fail for wrong reasons. Double-check what each test actually validates.
-
Test across different data states: Pagination behaves differently with 0 items, 1 item, exactly page size items, and thousands of items. Ensure your test suite covers these data volume variations.
-
Include timing tests: For cursor-based pagination, test how the API handles requests made after significant time has passed—the cursor may expire or underlying data may have changed.
-
Document expected behaviors: Add comments explaining what each edge case represents in business terms. Future developers (including future you) will appreciate the context.
Automating Test Generation Workflow
You can integrate AI test generation into your CI/CD pipeline:
-
Pre-commit hook: Generate tests before code review
-
Post-deployment: Run edge case tests against staging
-
Scheduled runs: Catch issues from data changes over time
AI accelerates the initial test generation, but maintaining and updating tests still requires developer oversight. The combination produces more test coverage than either approach alone.
Related Articles
- How to Use AI to Generate Unicode and Emoji Edge Case Tests
- How to Use AI to Generate Timezone Edge Case Test Data
- How to Use AI to Create Edge Case Test Scenarios from API Er
- How to Use AI to Generate Jest Tests for Next.js API Routes
- How to Use AI to Generate Currency Decimal Precision Edge Ca
Built by theluckystrike — More at zovo.one