Workflow

Test Generation

Add coverage for edge cases and critical paths quickly.

Overview

Writing comprehensive tests is time-consuming, but skipping them leads to production bugs and regression issues. Claude Code accelerates test creation by analyzing your code, identifying edge cases, and generating test suites with proper assertions and mocking. Instead of spending hours writing boilerplate test code, you can focus on reviewing test logic and ensuring critical paths are covered.

When to use

Use this workflow when adding tests to existing code, increasing coverage for critical paths, validating edge cases and failure modes, implementing TDD for new features, or preparing code for refactoring.

Step-by-step process

1

Describe the function or module under test

Provide context about what the code does, its inputs and outputs, and any dependencies or side effects. Include information about the testing framework you're using.

"Generate tests for the calculateDiscount function in src/pricing/discount.ts. It takes a cart total and coupon code, applies business rules, and returns the final price. We use Jest with TypeScript."
2

Ask for edge cases and failure modes

Request that Claude identify boundary conditions, error scenarios, and unexpected inputs that your tests should handle.

"What edge cases should we test? Consider: empty carts, expired coupons, negative amounts, extremely large numbers, null/undefined inputs, and concurrent discount applications."
3

Generate tests with clear assertions

Have Claude create test cases with descriptive names, proper setup/teardown, meaningful assertions, and appropriate mocking.

"Create a test suite with: happy path tests, error handling tests, boundary condition tests, and integration tests for the discount API endpoint. Use clear test names like 'should apply percentage discount when coupon is valid'."
4

Run the suite and review failures

Execute the generated tests, analyze any failures, and iterate on test implementation or the underlying code as needed.

"Run npm test discount.test.ts. For any failures, explain what's wrong and whether it's a test issue or a bug in the implementation."

Example prompts

Adding tests to untested legacy code

"This auth.service.ts file has no tests. Analyze it and create a comprehensive test suite covering user registration, login, token refresh, and error handling. Mock the database calls."

Testing React components

"Generate tests for the UserProfile component using React Testing Library. Test rendering with different prop combinations, user interactions (button clicks, form submissions), loading states, and error states."

API endpoint testing

"Create integration tests for the POST /api/orders endpoint. Test successful order creation, validation errors, authentication failures, and rate limiting. Use supertest and mock the payment gateway."

Increasing coverage for critical paths

"Our payment processing module has 40% test coverage. Identify the most critical untested code paths and generate tests for them, prioritizing transaction handling and refund logic."

Expected outputs

Test plan

Structured outline of test categories, edge cases to cover, and testing strategy tailored to your codebase

New tests

Complete test files with proper imports, setup/teardown, mocking, descriptive test names, and meaningful assertions

Coverage notes

Analysis of what's tested, what's missing, and recommendations for achieving comprehensive coverage

Mock implementations

Reusable mock objects, stubs, and fixtures for external dependencies like databases, APIs, and third-party services

Best practices

Start with the test plan before writing tests—understand what needs coverage before generating code
Focus on behavior and outcomes, not implementation details—tests should survive refactoring
Use descriptive test names that explain what's being tested and what the expected outcome is
Mock external dependencies to make tests fast, reliable, and deterministic
Review generated tests critically—Claude writes good tests, but you know your domain best

Common pitfalls

Generating tests without understanding test quality

Solution: Ask Claude to explain why each test is important and what specific behavior it validates. Don't just blindly accept generated tests.

Over-mocking and testing implementation instead of behavior

Solution: Request integration tests alongside unit tests. Ask Claude: 'Are we testing behavior or implementation here?'

Not running tests after generation

Solution: Always run the test suite immediately after generation. Fix any failures before moving on to ensure tests are actually valid.

Pro tips

Generate tests alongside new features

""I'm building a new comment system. Write the tests first based on these requirements, then we'll implement the code to pass them.""

Use coverage reports to guide test generation

""Here's our coverage report showing 60% coverage. Analyze the uncovered lines and generate tests to get us to 85%.""

Create reusable test utilities

""Extract the common setup code from these tests into a test factory. I want to reuse user creation and authentication mocks across test files.""

Related workflows