Testing Strategies with Claude Code: A Practical Guide
How to use Claude Code to write effective tests: TDD, E2E, mocking and best practices.
Tests are often the least loved part of development. Claude Code changes the game by making test writing as fast as writing the code itself. Here's how to get the most out of it.
TDD with Claude Code
Test-Driven Development is the ideal workflow with Claude Code:
The Red-Green-Refactor Cycle
- Red: Ask Claude to write the test first
- Green: Ask for the minimal implementation that passes the test
- Refactor: Ask to improve the code while keeping tests green
Concrete Example
You: "Create a test for a formatPrice function that takes a number
and returns a string formatted in euros (e.g., 42.5 → '42.50 €')"
Claude writes the test:
describe('formatPrice', () => {
it('formats integer price', () => {
expect(formatPrice(42)).toBe('42.00 €');
});
it('formats decimal price', () => {
expect(formatPrice(42.5)).toBe('42.50 €');
});
it('handles zero', () => {
expect(formatPrice(0)).toBe('0.00 €');
});
});
Then the implementation follows naturally.
E2E Testing with Playwright
The Playwright Skill (score 85/100) is your best ally for E2E tests:
Recommended Patterns
1. Page Object Model
Ask Claude to structure tests with POM:
class LoginPage {
constructor(private page: Page) {}
async login(email: string, password: string) {
await this.page.getByLabel('Email').fill(email);
await this.page.getByLabel('Password').fill(password);
await this.page.getByRole('button', { name: 'Sign in' }).click();
}
}
2. Reusable Fixtures
const test = base.extend<{ loginPage: LoginPage }>({
loginPage: async ({ page }, use) => {
await use(new LoginPage(page));
},
});
3. Network Mocking
For fast, reliable tests:
await page.route('**/api/users', route =>
route.fulfill({ json: [{ id: 1, name: 'Test' }] })
);
Unit Testing with Jest
The Jest Testing Expert skill guides Claude for:
- Tests with advanced mocking (
jest.spyOn,jest.mock) - React Testing Library (render, screen, userEvent)
- Async tests (waitFor, findBy)
- Targeted coverage (not 100%, but critical paths)
The Over-Testing Trap
Claude tends to write too many tests. Guide it:
"Only write tests for edge cases and error paths.
The happy path is covered by E2E tests."
Best Practices
1. One Test = One Behavior
Each test verifies a single behavior. No tests that check 5 things at once.
2. Descriptive Names
✅ "should return 404 when user does not exist"
❌ "test user endpoint"
3. No Logic in Tests
Tests should be linear: setup → action → assertion. No if/else, no loops.
4. Independent Tests
Each test should be able to run alone, in any order.
Recommended Skills
| Need | Skill |
|---|---|
| E2E tests | Playwright Skill |
| Unit tests | Jest Testing Expert |
| Test design | Pict Test Designer |
| Accessibility tests | Design Auditor |
| Security tests | VibeSec Skill |
Conclusion
With the right skills, Claude Code transforms testing from a chore into a fluid workflow. The key: use TDD, be specific in your requests, and let Claude handle the boilerplate.