tdd
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTest-Driven Development
测试驱动开发
Implement $ARGUMENTS using Test-Driven Development (TDD).
使用测试驱动开发(TDD)实现$ARGUMENTS。
TDD Cycle
TDD 循环
Repeat: Red → Green → Refactor
1. Red: Write a failing test
2. Green: Write minimal code to pass the test
3. Refactor: Clean up code (tests still pass)Repeat: Red → Green → Refactor
1. Red: Write a failing test
2. Green: Write minimal code to pass the test
3. Refactor: Clean up code (tests still pass)Implementation Steps
实现步骤
Phase 1: Test Design
阶段1:测试设计
-
Confirm Requirements
- What is the input
- What is the output
- What are the edge cases
-
List Test Cases
- [ ] Happy path: Basic functionality - [ ] Happy path: Boundary values - [ ] Error case: Invalid input - [ ] Error case: Error handling
-
确认需求
- 输入是什么
- 输出是什么
- 有哪些边界场景
-
列出测试用例
- [ ] 正常路径:基础功能 - [ ] 正常路径:边界值 - [ ] 错误场景:无效输入 - [ ] 错误场景:错误处理
Phase 2: Red-Green-Refactor
阶段2:红-绿-重构
Step 1: Write First Test (Red)
步骤1:编写第一个测试(红)
python
undefinedpython
undefinedtests/test_{module}.py
tests/test_{module}.py
def test_{function}_basic():
"""Test the most basic case"""
result = function(input)
assert result == expected
Run test and **confirm failure**:
```bash
uv run pytest tests/test_{module}.py -vdef test_{function}_basic():
"""Test the most basic case"""
result = function(input)
assert result == expected
运行测试并**确认运行失败**:
```bash
uv run pytest tests/test_{module}.py -vStep 2: Implementation (Green)
步骤2:功能实现(绿)
Write minimal code to pass the test:
- Don't aim for perfection
- Hardcoding is OK
- Just make the test pass
Run test and confirm success:
bash
uv run pytest tests/test_{module}.py -v编写最少的代码让测试通过:
- 不要追求完美
- 硬编码是可接受的
- 只要让测试通过即可
运行测试并确认运行成功:
bash
uv run pytest tests/test_{module}.py -vStep 3: Refactoring (Refactor)
步骤3:代码重构(重构)
Improve while tests still pass:
- Remove duplication
- Improve naming
- Clean up structure
bash
uv run pytest tests/test_{module}.py -v # Confirm still passes在测试保持通过的前提下优化代码:
- 移除重复代码
- 优化命名
- 清理代码结构
bash
uv run pytest tests/test_{module}.py -v # Confirm still passesStep 4: Next Test
步骤4:下一个测试
Return to Step 1 with next test case from the list.
回到步骤1,处理测试用例列表中的下一个用例。
Phase 3: Completion Check
阶段3:完成校验
bash
undefinedbash
undefinedRun all tests
Run all tests
uv run pytest -v
uv run pytest -v
Check coverage (target 80%+)
Check coverage (target 80%+)
uv run pytest --cov={module} --cov-report=term-missing
undefineduv run pytest --cov={module} --cov-report=term-missing
undefinedReport Format
报告格式
markdown
undefinedmarkdown
undefinedTDD Complete: {Feature Name}
TDD Complete: {Feature Name}
Test Cases
Test Cases
- {test1}: {description}
- {test2}: {description} ...
- {test1}: {description}
- {test2}: {description} ...
Coverage
Coverage
{Coverage report}
{Coverage report}
Implementation Files
Implementation Files
- : {description}
src/{module}.py - : {N} tests
tests/test_{module}.py
undefined- : {description}
src/{module}.py - : {N} tests
tests/test_{module}.py
undefinedNotes
注意事项
- Write tests first (not after)
- Keep each cycle small
- Refactor after tests pass
- Prioritize working code over perfection
- 先写测试(不要事后补)
- 每个循环的粒度要小
- 测试通过后再重构
- 优先保证代码可运行,而非追求完美