AI Test Generation Patterns
Systematic patterns for prompting AI coding agents to generate high-quality tests including prompt engineering for test creation, coverage-driven generation, mutation-aware testing, and review checklists for AI-generated test code.
git clone --depth 1 https://github.com/PramodDutta/qaskills /tmp/ai-test-generation-patterns && cp -r /tmp/ai-test-generation-patterns/seed-skills/ai-test-generation ~/.claude/skills/ai-test-generation-patternsSKILL.md
# AI Test Generation Patterns Skill
You are an expert in using AI coding agents to generate high-quality test code. When the user asks you to generate tests using AI, create prompting strategies for test generation, build coverage-driven test pipelines, or review AI-generated test quality, follow these detailed instructions.
## Core Principles
1. **Context-rich prompting** -- Provide the AI agent with the source code under test, existing test patterns, project conventions, and expected behavior. More context produces better tests.
2. **Coverage-gap targeting** -- Analyze existing coverage reports to identify untested paths, then prompt the AI specifically for those gaps rather than regenerating all tests.
3. **Pattern-based generation** -- Establish test patterns (AAA, Given-When-Then) in your codebase first, then instruct the AI to follow those patterns for consistency.
4. **Incremental validation** -- Generate tests in small batches, run them, verify they pass and fail correctly, then generate the next batch. Never generate an entire suite without validation.
5. **Mutation-aware testing** -- Use mutation testing results to identify weak test assertions and prompt the AI to strengthen them with more specific checks.
6. **Human review gates** -- Every AI-generated test must pass human review. AI excels at generating structure but can miss business logic nuances.
7. **Prompt versioning** -- Version your generation prompts alongside your code. When test patterns change, update prompts accordingly.
## Project Structure
```
test-generation/
prompts/
unit-test-prompt.md
integration-test-prompt.md
e2e-test-prompt.md
api-test-prompt.md
edge-case-prompt.md
templates/
vitest-unit.template.ts
playwright-e2e.template.ts
api-test.template.ts
analyzers/
coverage-analyzer.ts
mutation-analyzer.ts
complexity-analyzer.ts
generators/
prompt-builder.ts
batch-generator.ts
test-validator.ts
review/
quality-checker.ts
anti-pattern-detector.ts
assertion-strength-analyzer.ts
config/
generation-config.ts
model-config.ts
```
## Prompt Builder for Test Generation
```typescript
// test-generation/generators/prompt-builder.ts
export interface PromptContext {
sourceCode: string;
sourceFile: string;
existingTests?: string;
coverageReport?: string;
testPatterns?: string;
projectConventions?: string;
focusAreas?: string[];
}
export interface GenerationPrompt {
system: string;
user: string;
expectedFormat: string;
}
export class TestPromptBuilder {
buildUnitTestPrompt(context: PromptContext): GenerationPrompt {
const system = `You are a senior test engineer generating unit tests.
Follow these rules strictly:
1. Use the Arrange-Act-Assert (AAA) pattern for every test
2. Name tests using the pattern: "should [expected behavior] when [condition]"
3. Test one behavior per test function
4. Mock all external dependencies
5. Include edge cases: null, undefined, empty strings, boundary values
6. Include error cases: invalid inputs, thrown exceptions
7. Use TypeScript strict types for all test code
8. Do NOT use any as a type
9. Generate descriptive assertion messages
10. Group related tests in describe blocks`;
const user = this.buildUserPrompt(context, 'unit');
return {
system,
user,
expectedFormat: 'typescript',
};
}
buildIntegrationTestPrompt(context: PromptContext): GenerationPrompt {
const system = `You are a senior test engineer generating integration tests.
Follow these rules strictly:
1. Test the interaction between two or more components
2. Use real implementations for the components under test
3. Mock only external services (APIs, databases, file systems)
4. Set up realistic test data that represents production scenarios
5. Test both success and failure paths for each integration point
6. Verify side effects (database writes, cache updates, event emissions)
7. Clean up test data in afterEach/afterAll hooks
8. Test timeout and retry behavior for network calls
9. Use factories or builders for complex test data
10. Assert on the complete response shape, not just one field`;
const user = this.buildUserPrompt(context, 'integration');
return {
system,
user,
expectedFormat: 'typescript',
};
}
buildE2ETestPrompt(context: PromptContext): GenerationPrompt {
const system = `You are a senior test engineer generating Playwright E2E tests.
Follow these rules strictly:
1. Use Page Object Model pattern
2. Prefer getByRole, getByTestId, getByLabel over CSS selectors
3. Use web-first assertions: expect(locator).toBeVisible()
4. Never use page.waitForTimeout() - use proper wait conditions
5. Handle loading states explicitly
6. Test the complete user journey, not individual UI elements
7. Include accessibility assertions where relevant
8. Clean up test state (logout, clear data) in afterEach
9. Capture screenshots on failure for debugging
10. Test on at least desktop and mobile viewports`;
const user = this.buildUserPrompt(context, 'e2e');
return {
system,
user,
expectedFormat: 'typescript',
};
}
private buildUserPrompt(context: PromptContext, testType: string): string {
let prompt = `Generate ${testType} tests for the following code:\n\n`;
prompt += `### Source File: ${context.sourceFile}\n`;
prompt += `\`\`\`typescript\n${context.sourceCode}\n\`\`\`\n\n`;
if (context.existingTests) {
prompt += `### Existing Tests (follow this pattern):\n`;
prompt += `\`\`\`typescript\n${context.existingTests}\n\`\`\`\n\n`;
}
if (context.coverageReport) {
prompt += `### Coverage Gaps (focus on these):\n${context.coverageReport}\n\n`;
}
if (context.projectConventions) {
prompt += `### Project Conventions:\n${context.projectConventions}\n\n`;
}
if (context.focusAreas?.length) {
prompt += `### Focus Areas:\n`;
for (const area of context.focAutomated accessibility testing with axe-core integrated into CI pipelines, including custom rule configuration, issue prioritization, and remediation guidance.
Validating A/B test implementations including traffic splitting accuracy, statistical significance calculation, metric tracking, and experiment cleanup.
Comprehensive WCAG compliance and accessibility testing covering ARIA, keyboard navigation, screen readers, color contrast, and automated a11y validation.
Comprehensive WCAG 2.1 AA compliance testing combining automated axe-core scans with manual keyboard navigation, screen reader compatibility, and focus management verification
American Fuzzy Lop Plus Plus mutation-based fuzz testing for finding crashes, hangs, and security vulnerabilities in binary programs.
Fast Rust-based headless browser automation CLI with Node.js fallback for AI agents, featuring navigation, clicking, typing, snapshots, and structured commands optimized for agent workflows.
AI-first testing methodology where autonomous agents plan, generate, execute, and maintain test suites with minimal human intervention, covering agent orchestration, feedback loops, and intelligent test prioritization.
Comprehensive evaluation patterns for AI agents including multi-turn conversation testing, LLM-as-judge frameworks, benchmark suites, regression detection, and systematic eval pipelines for measuring agent quality and safety.