返回顶部
t

tdd测试驱动开发

Test-Driven Development assistant. Generates test cases from code or specifications, runs tests, tracks coverage, and guides the red-green-refactor cycle. Supports pytest, unittest, jest, and go test.

作者: admin | 来源: ClawHub
源自
ClawHub
版本
V 1.0.0
安全检测
已通过
106
下载量
免费
免费
0
收藏
概述
安装方式
版本历史

tdd

TDD(测试驱动开发)

测试驱动开发助手,从代码或规格生成测试用例,运行测试,追踪覆盖率,引导红-绿-重构循环。

版本:1.0
功能:测试生成、测试运行、覆盖率追踪、TDD 工作流



快速开始

1. 为函数生成测试

bash

为单个函数生成测试


python3 scripts/main.py generate --function calculate_total --file src/cart.py

为整个模块生成测试

python3 scripts/main.py generate --file src/cart.py --output tests/test_cart.py

2. 运行测试

bash

运行所有测试


python3 scripts/main.py run

运行特定测试文件

python3 scripts/main.py run tests/test_cart.py

持续监听模式

python3 scripts/main.py run --watch

3. TDD 循环

bash

开始 TDD 会话


python3 scripts/main.py cycle --file src/new_feature.py

检查当前状态

python3 scripts/main.py status

4. 覆盖率报告

bash

生成覆盖率报告


python3 scripts/main.py coverage

查看未覆盖的代码

python3 scripts/main.py coverage --uncovered

命令

命令说明示例
generate生成测试generate --file src/x.py
run
运行测试 | run tests/ | | cycle | TDD 循环 | cycle --file src/x.py | | status | 查看状态 | status | | coverage | 覆盖率报告 | coverage --html | | mutant | 变异测试 | mutant src/ |

测试生成

从函数签名生成

python

src/calculator.py


def calculate_discount(price: float, rate: float) -> float:
计算折扣后的价格
return price * (1 - rate)

生成的测试:
python

tests/test_calculator.py


def testcalculatediscount():
# 正常情况
assert calculate_discount(100.0, 0.2) == 80.0

# 边界情况
assert calculate_discount(0.0, 0.2) == 0.0
assert calculate_discount(100.0, 0.0) == 100.0
assert calculate_discount(100.0, 1.0) == 0.0

# 异常测试
with pytest.raises(ValueError):
calculate_discount(-100.0, 0.2)

支持的测试框架

语言框架自动检测
Pythonpytest
Python
unittest | ✅ | | JavaScript | Jest | ✅ | | Go | testing | ✅ |

TDD 循环

红-绿-重构循环

bash
$ python3 scripts/main.py cycle --file src/calculator.py

🔄 TDD 循环 - src/calculator.py
================================

🔴 红色:编写一个会失败的测试
已生成:tests/test_calculator.py
添加你的测试用例并运行:tdd run

$ # 编辑测试文件...

$ python3 scripts/main.py run
🔴 测试失败(预期)

💚 绿色:让它通过
实现函数以通过测试

$ # 编辑实现...

$ python3 scripts/main.py run
💚 测试通过

♻️ 重构:改进代码
运行:tdd run
检查覆盖率:tdd coverage

$ python3 scripts/main.py run
♻️ 所有测试通过,准备重构

状态追踪

bash
$ python3 scripts/main.py status

📊 TDD 状态
=============
当前阶段:绿色
上次运行:2026-04-01 18:30:00
测试:5 通过,0 失败
覆盖率:87%

下一步:重构或添加新测试



覆盖率

基本报告

bash
python3 scripts/main.py coverage

输出:

📊 覆盖率报告
==================
总计:87%

src/calculator.py 95% ✅
src/discount.py 72% ⚠️
src/utils.py 45% 🔴

未覆盖代码

bash
python3 scripts/main.py coverage --uncovered

输出:

🔍 未覆盖的行
==================
src/discount.py:45-52 calculatebulkdiscount
src/utils.py:12-30 validate_email

HTML 报告

bash
python3 scripts/main.py coverage --html --output coverage_report/



配置

.tdd.json:

json
{
framework: pytest,
test_dir: tests,
source_dir: src,
coverage: {
threshold: 80,
exclude: [tests/, vendor/]
},
generate: {
edge_cases: true,
error_cases: true,
property_tests: false
}
}



示例

场景 1:为新功能写测试

bash

1. 创建新文件


touch src/payment.py

2. 生成测试框架

python3 scripts/main.py generate --file src/payment.py

3. 编辑测试(让它失败)

tests/test_payment.py

4. 运行测试(应该失败)

python3 scripts/main.py run

5. 实现功能(让它通过)

src/payment.py

6. 运行测试(应该通过)

python3 scripts/main.py run

7. 重构并确保测试仍通过

python3 scripts/main.py run

场景 2:提高覆盖率

bash

1. 查看当前覆盖率


python3 scripts/main.py coverage

2. 找到未覆盖的代码

python3 scripts/main.py coverage --uncovered

3. 为未覆盖的函数生成测试

python3 scripts/main.py generate --function uncovered_func --file src/utils.py

4. 再次检查覆盖率

python3 scripts/main.py coverage

场景 3:回归测试

bash

修复 bug 前,先写测试


python3 scripts/main.py generate --function buggy_function --file src/auth.py

确认测试失败(复现 bug)

python3 scripts/main.py run

修复 bug

...

确认测试通过

python3 scripts/main.py run

变异测试

测试测试的质量:

bash
python3 scripts/main.py mutant src/

原理:

  1. 1. 自动修改代码(变异)
  2. 运行测试
  3. 如果测试通过,说明测试不够严格

输出:

🧬 变异测试
===================
变异体:45
已杀死:42(93%)✅
存活:3(7%)⚠️

存活的变异体:

  • - src/calculator.py:23 (将 + 改为 -)

测试未检测到!


CI/CD 集成

yaml

.github/workflows/test.yml


name: 测试
on: [push, pull_request]

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: 运行测试
run: python3 skills/tdd/scripts/main.py run

- name: 覆盖率检查
run: python3 skills/tdd/scripts/main.py coverage --fail-under 80



文件

skills/tdd/
├── SKILL.md # 本文件
└── scripts/
├── main.py # ⭐ 统一入口
├── generator.py # 测试生成器
├── runner.py # 测试运行器
└── coverage.py # 覆盖率追踪



路线图

  • - [x] 测试生成(Python)
  • [x] 测试运行(pytest/unittest)
  • [x] 覆盖率报告
  • [ ] TDD 循环引导
  • [ ] 变异测试
  • [ ] JavaScript/Go 支持
  • [ ] AI 辅助测试生成

标签

skill ai

通过对话安装

该技能支持在以下平台通过对话安装:

OpenClaw WorkBuddy QClaw Kimi Claude

方式一:安装 SkillHub 和技能

帮我安装 SkillHub 和 openclaw-tdd-1775890441 技能

方式二:设置 SkillHub 为优先技能安装源

设置 SkillHub 为我的优先技能安装源,然后帮我安装 openclaw-tdd-1775890441 技能

通过命令行安装

skillhub install openclaw-tdd-1775890441

下载

⬇ 下载 tdd v1.0.0(免费)

文件大小: 12 KB | 发布时间: 2026-4-12 10:51

v1.0.0 最新 2026-4-12 10:51
Test-Driven Development assistant with test generation, test running, and coverage tracking

Archiver·手机版·闲社网·闲社论坛·羊毛社区· 多链控股集团有限公司 · 苏ICP备2025199260号-1

Powered by Discuz! X5.0   © 2024-2025 闲社网·线报更新论坛·羊毛分享社区·http://xianshe.com

p2p_official_large
返回顶部