Chapter 17: AI-Assisted Testing
Test Generation
Agents excel at generating comprehensive test suites from implementations. On real teams this is how coverage moves from under 10% to 40% with no dedicated QA team - the agent writes the unit and integration tests, humans review them, and the suite runs as a CI gate.
// Prompt: "Generate Vitest tests for this function"
// userService.js
async function createUser(userData) {
if (!userData.email) throw new Error('Email required');
if (!validateEmail(userData.email)) throw new Error('Invalid email');
const existing = await User.findOne({ email: userData.email });
if (existing) throw new Error('Email already exists');
return await User.create(userData);
}
// AI generates: userService.test.js
import { describe, it, expect, beforeEach } from 'vitest';
describe('createUser', () => {
beforeEach(async () => {
await User.deleteMany({});
});
it('creates a user with valid data', async () => {
const user = await createUser({ email: '[email protected]', name: 'Test' });
expect(user.email).toBe('[email protected]');
});
it('throws if email missing', async () => {
await expect(createUser({})).rejects.toThrow('Email required');
});
it('throws if email invalid', async () => {
await expect(createUser({ email: 'invalid' })).rejects.toThrow('Invalid email');
});
it('throws if email already exists', async () => {
await User.create({ email: '[email protected]' });
await expect(createUser({ email: '[email protected]' }))
.rejects.toThrow('Email already exists');
});
});
UI Regression with Playwright
Agent-generated unit tests plus Playwright-driven UI regression suites are the combination that lets teams ship fast and ship quality - not one or the other. Agents can author Playwright specs from a user story and keep them current as the UI evolves, and product teams can be cross-trained to own smoke and release-regression suites.
import { test, expect } from '@playwright/test';
test('user registration flow', async ({ page }) => {
await page.goto('/register');
await page.getByLabel('Email').fill('[email protected]');
await page.getByLabel('Password').fill('SecurePass123!');
await page.getByRole('button', { name: 'Sign Up' }).click();
await expect(page.getByText('Welcome, John')).toBeVisible();
});
Tests as the Verification Layer
In an agentic SDLC, the test suite is not just quality assurance - it is the verification layer that makes autonomous work trustworthy. When an async agent returns a PR, the tests and evals are what confirm the work is actually done. A release that lands on time but does not work is not a win - it is a cleanup project handed to next sprint. Tests are how you avoid handing that project to yourself.