Tdd Guide
2026-03-29
新闻来源:网淘吧
围观:10
电脑广告
手机广告
TDD 指南
测试驱动开发技能,用于生成测试、分析覆盖率,并指导在 Jest、Pytest、JUnit 和 Vitest 框架中的红-绿-重构工作流程。
工作流程
从代码生成测试
- 提供源代码(TypeScript、JavaScript、Python、Java)
- 指定目标框架(Jest、Pytest、JUnit、Vitest)
- 运行
test_generator.py并包含需求 - 审查生成的测试桩
- 验证:测试能够编译,并覆盖正常路径、错误情况和边界情况
分析覆盖率缺口
- 从测试运行器生成覆盖率报告(
npm test -- --coverage) - 运行
coverage_analyzer.py处理 LCOV/JSON/XML 报告 - 审查按优先级排序的缺口(P0/P1/P2)
- 为未覆盖的路径生成缺失的测试
- 验证:覆盖率已达到目标阈值(通常为80%以上)
TDD 新功能开发
- 首先编写失败测试(RED阶段)
- 运行
tdd_workflow.py --phase red进行验证 - 编写最少代码使测试通过(GREEN阶段)
- 运行
tdd_workflow.py --phase green进行验证 - 重构并保持测试通过(REFACTOR阶段)
- 验证:每个循环后所有测试均通过
示例
测试生成 — 输入 → 输出(Pytest)
输入源函数文件(math_utils.py):
def divide(a: float, b: float) -> float:
if b == 0:
raise ValueError("Cannot divide by zero")
return a / b
命令:
python scripts/test_generator.py --input math_utils.py --framework pytest
生成的测试输出文件(test_math_utils.py):
import pytest
from math_utils import divide
class TestDivide:
def test_divide_positive_numbers(self):
assert divide(10, 2) == 5.0
def test_divide_negative_numerator(self):
assert divide(-10, 2) == -5.0
def test_divide_float_result(self):
assert divide(1, 3) == pytest.approx(0.333, rel=1e-3)
def test_divide_by_zero_raises_value_error(self):
with pytest.raises(ValueError, match="Cannot divide by zero"):
divide(10, 0)
def test_divide_zero_numerator(self):
assert divide(0, 5) == 0.0
覆盖率分析 — P0/P1/P2 示例输出
命令:
python scripts/coverage_analyzer.py --report lcov.info --threshold 80
示例输出:
Coverage Report — Overall: 63% (threshold: 80%)
P0 — Critical gaps (uncovered error paths):
auth/login.py:42-58 handle_expired_token() 0% covered
payments/process.py:91-110 handle_payment_failure() 0% covered
P1 — High-value gaps (core logic branches):
users/service.py:77 update_profile() — else branch 0% covered
orders/cart.py:134 apply_discount() — zero-qty guard 0% covered
P2 — Low-risk gaps (utility / helper functions):
utils/formatting.py:12 format_currency() 0% covered
Recommended: Generate tests for P0 items first to reach 80% threshold.
关键工具
| 工具 | 用途 | 用法 |
|---|---|---|
test_generator.py | 从代码/需求生成测试用例 | python scripts/test_generator.py --input source.py --framework pytest |
coverage_analyzer.py | 解析和分析覆盖率报告 | python scripts/coverage_analyzer.py --report lcov.info --threshold 80 |
tdd_workflow.py | 指导红-绿-重构循环 | python scripts/tdd_workflow.py --phase red --test test_auth.py |
fixture_generator.py | 生成测试数据和模拟对象 | python scripts/fixture_generator.py --entity User --count 5 |
附加脚本:framework_adapter.py(框架间转换),metrics_calculator.py(质量指标计算),format_detector.py(检测语言/框架),output_formatter.py(CLI/桌面/CI输出格式化)。
输入要求
对于测试生成:
- 源代码(文件路径或粘贴的内容)
- 目标框架(Jest, Pytest, JUnit, Vitest)
- 覆盖范围(单元测试、集成测试、边界情况)
对于覆盖率分析:
- 覆盖率报告文件(LCOV、JSON 或 XML 格式)
- 可选:用于上下文的源代码
- 可选:目标阈值百分比
对于TDD工作流:
- 功能需求或用户故事
- 当前阶段(RED, GREEN, REFACTOR)
- 测试代码与实现状态
局限性
| 范围 | 详情 |
|---|---|
| 单元测试重点 | 集成测试和端到端测试需要不同的模式 |
| 静态分析 | 无法执行测试或测量运行时行为 |
| 语言支持 | 最适合TypeScript、JavaScript、Python、Java |
| 报告格式 | 仅支持LCOV、JSON、XML格式;其他格式需转换 |
| 生成测试 | 提供框架;复杂逻辑需人工审查 |
何时使用其他工具:
- 端到端测试:Playwright、Cypress、Selenium
- 性能测试:k6、JMeter、Locust
- 安全测试:OWASP ZAP、Burp Suite
文章底部电脑广告
手机广告位-内容正文底部
上一篇:ETF投资助理
下一篇:Reddit (read only - no auth)


微信扫一扫,打赏作者吧~