Generate test files from acceptance criteria or existing code. Supports criteria-driven mode (from specs/tasks) and code-analysis mode (from source files). Auto-detects test framework and spawns test-writer agents for parallel generation.
Generates test files from acceptance criteria or existing code by auto-detecting the project's test framework.
/plugin marketplace add sequenzia/agent-alchemy/plugin install agent-alchemy-tdd-tools@agent-alchemyThis skill is limited to using the following tools:
references/framework-templates.mdreferences/test-patterns.mdGenerate high-quality, behavior-driven test files from acceptance criteria or existing source code. This skill auto-detects the project's test framework, loads framework-specific patterns, and spawns test-writer agents to produce runnable test files that follow project conventions.
CRITICAL: Complete ALL 6 phases. The workflow is not complete until Phase 6: Report is finished. After completing each phase, immediately proceed to the next phase without waiting for user prompts.
tdd-cycle, or any other skill to function.IMPORTANT: You MUST use the AskUserQuestion tool for ALL questions to the user. Never ask questions through regular text output.
Text output should only be used for presenting information, summaries, and progress updates.
Goal: Determine the operating mode and resolve input paths.
Analyze the $ARGUMENTS to determine which mode to use:
Criteria-Driven Mode -- triggered when input is:
specs/SPEC-feature.md)5, #5, task-5)specs/SPEC-feature.md Section 5.1)Code-Analysis Mode -- triggered when input is:
src/utils.py, src/services/auth.ts)src/services/)Ambiguous input: If the input could be either mode (e.g., a path that could be a spec or source file), check the file content. If it contains **Acceptance Criteria:** or is a markdown file in a specs/ directory, use criteria-driven mode. Otherwise, use code-analysis mode.
TaskGet to retrieve the task description, then extract acceptance criteriaInvalid spec path:
ERROR: Spec file not found: {path}
Did you mean one of these?
{List matching files from Glob search for similar names}
Usage: /generate-tests <spec-path|task-id|file-path>
Invalid task ID:
ERROR: Task #{id} not found.
Available tasks:
{List first 5 pending/in-progress tasks from TaskList}
Usage: /generate-tests <task-id>
Empty directory:
ERROR: No source files found in {path}.
The directory exists but contains no files matching supported extensions (.py, .ts, .tsx, .js, .jsx).
Goal: Auto-detect the project's test framework using the detection chain.
Read the framework detection reference for the full detection algorithm:
Read: ${CLAUDE_PLUGIN_ROOT}/skills/generate-tests/references/framework-templates.md
Follow the detection chain in priority order:
Check for framework configuration files:
Python:
pyproject.toml with [tool.pytest.ini_options] or [tool.pytest] -> pytestsetup.cfg with [tool:pytest] -> pytestpytest.ini exists -> pytestconftest.py at project root or in tests/ -> pytestTypeScript/JavaScript:
vitest.config.* exists -> Vitest (takes priority)jest.config.* exists -> Jestpackage.json with vitest in dependencies/devDependencies -> Vitestpackage.json with jest in dependencies/devDependencies -> Jestpackage.json with "jest": {} config section -> JestIf no config files found, scan for existing test files:
test_*.py or *_test.py -> pytest*.test.ts / *.spec.ts with vitest imports -> Vitest*.test.ts / *.spec.ts with jest imports or no explicit imports -> JestIf detection is still ambiguous, check .claude/agent-alchemy.local.md for:
tdd:
framework: pytest # or jest, vitest, auto
If tdd.framework is set to a recognized value (pytest, jest, vitest), use it. If set to auto or an unrecognized value, continue to Step 4.
If all detection methods fail, prompt the user:
AskUserQuestion:
questions:
- header: "Test Framework"
question: "No test framework was detected in this project. Which framework should be used for test generation?"
options:
- label: "pytest"
description: "Python testing framework (recommended for Python projects)"
- label: "Jest"
description: "JavaScript/TypeScript testing framework"
- label: "Vitest"
description: "Vite-native testing framework (modern alternative to Jest)"
- label: "Other"
description: "A different framework (tests may need manual adjustment)"
multiSelect: false
After detection, record:
tests/, __tests__/, src/)test_*.py, *.test.ts)@testing-library/jest-dom)In monorepos, detect framework per-directory by walking up from the target source file to find the nearest config file. Different directories may use different frameworks.
Goal: Load framework-specific patterns and project conventions.
Read the test patterns reference for framework-specific guidance:
Read: ${CLAUDE_PLUGIN_ROOT}/skills/generate-tests/references/test-patterns.md
This provides:
Read the framework templates reference for boilerplate structure:
Read: ${CLAUDE_PLUGIN_ROOT}/skills/generate-tests/references/framework-templates.md
This provides:
Load language-specific patterns and project conventions for the target project:
Read: ${CLAUDE_PLUGIN_ROOT}/../core-tools/skills/language-patterns/SKILL.md
Read: ${CLAUDE_PLUGIN_ROOT}/../core-tools/skills/project-conventions/SKILL.md
Apply these skills' guidance when generating tests to ensure:
If the project has existing test files:
Goal: Produce test files by spawning test-writer agents.
When operating from acceptance criteria (spec, task, or section reference):
Extract criteria: Parse the acceptance criteria into categories:
Plan test files: Determine how many test files to create:
Map criteria to test cases: For each criterion, create one or more test cases:
Determine test file paths: Follow the project's convention:
When operating from source files:
Analyze source files: For each source file, identify:
Generate characterization tests: Create tests that verify current behavior:
Identify untested edge cases: Look for:
Preserve existing tests: Before writing, check for existing test files:
_additional or _generated suffixSingle feature/file: Use a single test-writer agent:
Task tool with subagent_type: "test-writer"
Prompt:
"Generate tests for {feature/file}.
Framework: {detected framework}
Test patterns: {summary of project's test patterns}
File naming: {convention}
Target test file: {output path}
{Mode-specific context:}
- Criteria-driven: {formatted acceptance criteria}
- Code-analysis: {source file content and analysis}
Follow these conventions from the project's existing tests:
{Summary of patterns from existing test files}
Write the test file to: {target path}"
Multiple features/files: Spawn test-writer agents in parallel:
For each feature/file, launch a separate Task with subagent_type: "test-writer"
- Each agent gets its own feature's criteria or source file
- All agents share the same framework detection and pattern context
- Launch all agents simultaneously in a single turn for parallelism
Wait for all agents to complete before proceeding to Phase 5.
If a test-writer agent fails:
Goal: Verify generated tests are syntactically valid and follow conventions.
For each generated test file, run a syntax check:
pytest:
python -c "import ast; ast.parse(open('{test_file}').read())"
Jest/Vitest:
npx tsc --noEmit {test_file}
Or if TypeScript is not configured:
node --check {test_file}
If syntax validation fails:
Verify each generated test file follows project conventions:
If no implementation exists for the tested code:
If implementation already exists:
NOTE: Implementation for {module} already exists. Generated tests may pass
immediately (not in RED state). These tests document existing behavior rather
than driving new implementation.
Goal: Present a summary of generated test files.
Present the following summary to the user:
## Test Generation Summary
**Mode**: {Criteria-Driven | Code-Analysis}
**Framework**: {pytest | Jest | Vitest} ({confidence level})
**Source**: {spec path | task ID | file/directory path}
### Generated Files
| File | Tests | Coverage Area |
|------|-------|---------------|
| {test_file_1} | {count} | {what it tests} |
| {test_file_2} | {count} | {what it tests} |
**Total**: {total_tests} tests across {file_count} files
### Criteria Coverage (Criteria-Driven Mode Only)
| Category | Criteria | Tests |
|----------|----------|-------|
| Functional | {n} criteria | {n} tests |
| Edge Cases | {n} criteria | {n} tests |
| Error Handling | {n} criteria | {n} tests |
| Performance | {n} criteria | {n} tests |
### State
{If no implementation exists:}
Tests are in RED state -- they will fail when run. Implement the code to make them pass.
{If implementation exists:}
WARNING: Implementation already exists. Tests may pass immediately.
Run your test suite to verify: {test command}
### Next Steps
- Run tests: `{test_command}`
- Start TDD cycle: `/tdd-cycle {feature}`
- Analyze coverage: `/analyze-coverage {path}`
If any files were skipped because tests already exist:
### Skipped (Existing Tests)
| Source File | Existing Test File | Reason |
|------------|-------------------|--------|
| {source} | {test_file} | Test file already exists |
If any test-writer agents failed:
### Failures
| Feature/File | Error | Recommendation |
|-------------|-------|----------------|
| {feature} | {error summary} | {suggestion} |
If $ARGUMENTS is empty or missing:
AskUserQuestion:
questions:
- header: "Test Generation Input"
question: "What would you like to generate tests for?"
options:
- label: "Spec file"
description: "Generate tests from a specification's acceptance criteria"
- label: "Task ID"
description: "Generate tests from a Claude Code Task's acceptance criteria"
- label: "Source file or directory"
description: "Generate characterization tests for existing code"
multiSelect: false
Then prompt for the specific path or ID based on the selection.
If framework detection reaches the user prompt fallback (Step 4) and the user selects "Other":
WARNING: Unsupported framework selected. Generating tests using the closest
supported framework ({inferred}). Generated tests may need manual adjustment
for your specific framework.
If the input resolves to a spec or task with no acceptance criteria:
The following settings in .claude/agent-alchemy.local.md affect test generation:
tdd:
framework: auto # auto | pytest | jest | vitest
test-review-threshold: 70 # Minimum test quality score (0-100)
test-review-on-generate: false # Run test-reviewer after generate-tests
| Setting | Default | Used In |
|---|---|---|
tdd.framework | auto | Phase 2 (framework detection fallback) |
tdd.test-review-threshold | 70 | Phase 6 (test quality evaluation threshold) |
tdd.test-review-on-generate | false | Phase 6 (auto-run test-reviewer after generation) |
Error handling:
tdd.framework is set to an unrecognized value, fall back to auto-detection.See tdd-tools/README.md for the full settings reference.
references/test-patterns.md -- Framework-specific test patterns, behavior-driven guidance, anti-patternsreferences/framework-templates.md -- Auto-detection chain, boilerplate templates, configuration referenceActivates when the user asks about AI prompts, needs prompt templates, wants to search for prompts, or mentions prompts.chat. Use for discovering, retrieving, and improving prompts.
Search, retrieve, and install Agent Skills from the prompts.chat registry using MCP tools. Use when the user asks to find skills, browse skill catalogs, install a skill for Claude, or extend Claude's capabilities with reusable AI agent components.
This skill should be used when the user asks to "create a slash command", "add a command", "write a custom command", "define command arguments", "use command frontmatter", "organize commands", "create command with file references", "interactive command", "use AskUserQuestion in command", or needs guidance on slash command structure, YAML frontmatter fields, dynamic arguments, bash execution in commands, user interaction patterns, or command development best practices for Claude Code.