$npx -y skills add PramodDutta/qaskills --skill ai-test-generationSystematic 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.
| 1 | # AI Test Generation Patterns Skill |
| 2 | |
| 3 | 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. |
| 4 | |
| 5 | ## Core Principles |
| 6 | |
| 7 | 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. |
| 8 | 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. |
| 9 | 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. |
| 10 | 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. |
| 11 | 5. **Mutation-aware testing** -- Use mutation testing results to identify weak test assertions and prompt the AI to strengthen them with more specific checks. |
| 12 | 6. **Human review gates** -- Every AI-generated test must pass human review. AI excels at generating structure but can miss business logic nuances. |
| 13 | 7. **Prompt versioning** -- Version your generation prompts alongside your code. When test patterns change, update prompts accordingly. |
| 14 | |
| 15 | ## Project Structure |
| 16 | |
| 17 | ``` |
| 18 | test-generation/ |
| 19 | prompts/ |
| 20 | unit-test-prompt.md |
| 21 | integration-test-prompt.md |
| 22 | e2e-test-prompt.md |
| 23 | api-test-prompt.md |
| 24 | edge-case-prompt.md |
| 25 | templates/ |
| 26 | vitest-unit.template.ts |
| 27 | playwright-e2e.template.ts |
| 28 | api-test.template.ts |
| 29 | analyzers/ |
| 30 | coverage-analyzer.ts |
| 31 | mutation-analyzer.ts |
| 32 | complexity-analyzer.ts |
| 33 | generators/ |
| 34 | prompt-builder.ts |
| 35 | batch-generator.ts |
| 36 | test-validator.ts |
| 37 | review/ |
| 38 | quality-checker.ts |
| 39 | anti-pattern-detector.ts |
| 40 | assertion-strength-analyzer.ts |
| 41 | config/ |
| 42 | generation-config.ts |
| 43 | model-config.ts |
| 44 | ``` |
| 45 | |
| 46 | ## Prompt Builder for Test Generation |
| 47 | |
| 48 | ```typescript |
| 49 | // test-generation/generators/prompt-builder.ts |
| 50 | export interface PromptContext { |
| 51 | sourceCode: string; |
| 52 | sourceFile: string; |
| 53 | existingTests?: string; |
| 54 | coverageReport?: string; |
| 55 | testPatterns?: string; |
| 56 | projectConventions?: string; |
| 57 | focusAreas?: string[]; |
| 58 | } |
| 59 | |
| 60 | export interface GenerationPrompt { |
| 61 | system: string; |
| 62 | user: string; |
| 63 | expectedFormat: string; |
| 64 | } |
| 65 | |
| 66 | export class TestPromptBuilder { |
| 67 | buildUnitTestPrompt(context: PromptContext): GenerationPrompt { |
| 68 | const system = `You are a senior test engineer generating unit tests. |
| 69 | Follow these rules strictly: |
| 70 | 1. Use the Arrange-Act-Assert (AAA) pattern for every test |
| 71 | 2. Name tests using the pattern: "should [expected behavior] when [condition]" |
| 72 | 3. Test one behavior per test function |
| 73 | 4. Mock all external dependencies |
| 74 | 5. Include edge cases: null, undefined, empty strings, boundary values |
| 75 | 6. Include error cases: invalid inputs, thrown exceptions |
| 76 | 7. Use TypeScript strict types for all test code |
| 77 | 8. Do NOT use any as a type |
| 78 | 9. Generate descriptive assertion messages |
| 79 | 10. Group related tests in describe blocks`; |
| 80 | |
| 81 | const user = this.buildUserPrompt(context, 'unit'); |
| 82 | |
| 83 | return { |
| 84 | system, |
| 85 | user, |
| 86 | expectedFormat: 'typescript', |
| 87 | }; |
| 88 | } |
| 89 | |
| 90 | buildIntegrationTestPrompt(context: PromptContext): GenerationPrompt { |
| 91 | const system = `You are a senior test engineer generating integration tests. |
| 92 | Follow these rules strictly: |
| 93 | 1. Test the interaction between two or more components |
| 94 | 2. Use real implementations for the components under test |
| 95 | 3. Mock only external services (APIs, databases, file systems) |
| 96 | 4. Set up realistic test data that represents production scenarios |
| 97 | 5. Test both success and failure paths for each integration point |
| 98 | 6. Verify side effects (database writes, cache updates, event emissions) |
| 99 | 7. Clean up test data in afterEach/afterAll hooks |
| 100 | 8. Test timeout and retry behavior for network calls |
| 101 | 9. Use factories or builders for complex test data |
| 102 | 10. Assert on the complete response shape, not just one field`; |
| 103 | |
| 104 | const user = this.build |