tdd-guide
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTDD Guide
TDD 指南
Test-driven development skill for generating tests, analyzing coverage, and guiding red-green-refactor workflows across Jest, Pytest, JUnit, and Vitest.
这是一项测试驱动开发技能,可用于生成测试、分析覆盖率,并在Jest、Pytest、JUnit和Vitest等框架中指导红-绿-重构工作流。
Table of Contents
目录
Capabilities
功能特性
| Capability | Description |
|---|---|
| Test Generation | Convert requirements or code into test cases with proper structure |
| Coverage Analysis | Parse LCOV/JSON/XML reports, identify gaps, prioritize fixes |
| TDD Workflow | Guide red-green-refactor cycles with validation |
| Framework Adapters | Generate tests for Jest, Pytest, JUnit, Vitest, Mocha |
| Quality Scoring | Assess test isolation, assertions, naming, detect test smells |
| Fixture Generation | Create realistic test data, mocks, and factories |
| 功能特性 | 描述 |
|---|---|
| 测试生成 | 将需求或代码转换为结构规范的测试用例 |
| 覆盖率分析 | 解析LCOV/JSON/XML报告,识别缺口,确定修复优先级 |
| TDD工作流 | 通过验证指导红-绿-重构循环 |
| 框架适配 | 为Jest、Pytest、JUnit、Vitest、Mocha生成测试 |
| 质量评分 | 评估测试隔离性、断言、命名,检测测试异味 |
| 测试夹具生成 | 创建真实的测试数据、模拟对象和工厂类 |
Workflows
工作流
Generate Tests from Code
从代码生成测试
- Provide source code (TypeScript, JavaScript, Python, Java)
- Specify target framework (Jest, Pytest, JUnit, Vitest)
- Run with requirements
test_generator.py - Review generated test stubs
- Validation: Tests compile and cover happy path, error cases, edge cases
- 提供源代码(TypeScript、JavaScript、Python、Java)
- 指定目标框架(Jest、Pytest、JUnit、Vitest)
- 结合需求运行
test_generator.py - 审查生成的测试桩代码
- 验证: 测试可编译,且覆盖正常路径、错误场景和边缘情况
Analyze Coverage Gaps
分析覆盖率缺口
- Generate coverage report from test runner ()
npm test -- --coverage - Run on LCOV/JSON/XML report
coverage_analyzer.py - Review prioritized gaps (P0/P1/P2)
- Generate missing tests for uncovered paths
- Validation: Coverage meets target threshold (typically 80%+)
- 从测试运行器生成覆盖率报告()
npm test -- --coverage - 对LCOV/JSON/XML报告运行
coverage_analyzer.py - 查看按优先级排序的缺口(P0/P1/P2)
- 为未覆盖路径生成缺失的测试
- 验证: 覆盖率达到目标阈值(通常为80%以上)
TDD New Feature
TDD 新功能开发
- Write failing test first (RED)
- Run to validate
tdd_workflow.py --phase red - Implement minimal code to pass (GREEN)
- Run to validate
tdd_workflow.py --phase green - Refactor while keeping tests green (REFACTOR)
- Validation: All tests pass after each cycle
- 先编写失败的测试(RED阶段)
- 运行进行验证
tdd_workflow.py --phase red - 实现最简代码使测试通过(GREEN阶段)
- 运行进行验证
tdd_workflow.py --phase green - 在保持测试通过的同时重构代码(REFACTOR阶段)
- 验证: 每个循环后所有测试均通过
Tools
工具
| Tool | Purpose | Usage |
|---|---|---|
| Generate test cases from code/requirements | |
| Parse and analyze coverage reports | |
| Guide red-green-refactor cycles | |
| Convert tests between frameworks | |
| Generate test data and mocks | |
| Calculate test quality metrics | |
| Detect language and framework | |
| Format output for CLI/desktop/CI | |
| 工具 | 用途 | 使用方式 |
|---|---|---|
| 从代码/需求生成测试用例 | |
| 解析并分析覆盖率报告 | |
| 指导红-绿-重构循环 | |
| 在不同框架间转换测试 | |
| 生成测试数据和模拟对象 | |
| 计算测试质量指标 | |
| 检测语言和框架 | |
| 为CLI/桌面/CI格式化输出 | |
Input Requirements
输入要求
For Test Generation:
- Source code (file path or pasted content)
- Target framework (Jest, Pytest, JUnit, Vitest)
- Coverage scope (unit, integration, edge cases)
For Coverage Analysis:
- Coverage report file (LCOV, JSON, or XML format)
- Optional: Source code for context
- Optional: Target threshold percentage
For TDD Workflow:
- Feature requirements or user story
- Current phase (RED, GREEN, REFACTOR)
- Test code and implementation status
测试生成所需:
- 源代码(文件路径或粘贴内容)
- 目标框架(Jest、Pytest、JUnit、Vitest)
- 覆盖范围(单元测试、集成测试、边缘情况)
覆盖率分析所需:
- 覆盖率报告文件(LCOV、JSON或XML格式)
- 可选:用于上下文参考的源代码
- 可选:目标阈值百分比
TDD工作流所需:
- 功能需求或用户故事
- 当前阶段(RED、GREEN、REFACTOR)
- 测试代码和实现状态
Limitations
局限性
| Scope | Details |
|---|---|
| Unit test focus | Integration and E2E tests require different patterns |
| Static analysis | Cannot execute tests or measure runtime behavior |
| Language support | Best for TypeScript, JavaScript, Python, Java |
| Report formats | LCOV, JSON, XML only; other formats need conversion |
| Generated tests | Provide scaffolding; require human review for complex logic |
When to use other tools:
- E2E testing: Playwright, Cypress, Selenium
- Performance testing: k6, JMeter, Locust
- Security testing: OWASP ZAP, Burp Suite
| 范围 | 详情 |
|---|---|
| 聚焦单元测试 | 集成测试和E2E测试需要不同的模式 |
| 静态分析 | 无法执行测试或衡量运行时行为 |
| 语言支持 | 对TypeScript、JavaScript、Python、Java支持最佳 |
| 报告格式 | 仅支持LCOV、JSON、XML;其他格式需要转换 |
| 生成的测试 | 提供基础框架;复杂逻辑需要人工审查 |
何时使用其他工具:
- E2E测试:Playwright、Cypress、Selenium
- 性能测试:k6、JMeter、Locust
- 安全测试:OWASP ZAP、Burp Suite