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.

Safety Notice

This listing is from the official public ClawHub registry. Review SKILL.md and referenced scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "tdd" with this command: npx skills add michealxie001/openclaw-tdd

TDD (Test-Driven Development)

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

Version: 1.0
Features: 测试生成、测试运行、覆盖率追踪、TDD 工作流


Quick Start

1. 为函数生成测试

# 为单个函数生成测试
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. 运行测试

# 运行所有测试
python3 scripts/main.py run

# 运行特定测试文件
python3 scripts/main.py run tests/test_cart.py

# 持续监听模式
python3 scripts/main.py run --watch

3. TDD 循环

# 开始 TDD 会话
python3 scripts/main.py cycle --file src/new_feature.py

# 检查当前状态
python3 scripts/main.py status

4. 覆盖率报告

# 生成覆盖率报告
python3 scripts/main.py coverage

# 查看未覆盖的代码
python3 scripts/main.py coverage --uncovered

Commands

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

Test Generation

从函数签名生成

# src/calculator.py
def calculate_discount(price: float, rate: float) -> float:
    """Calculate discounted price"""
    return price * (1 - rate)

生成的测试:

# tests/test_calculator.py
def test_calculate_discount():
    # Normal case
    assert calculate_discount(100.0, 0.2) == 80.0
    
    # Edge cases
    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
    
    # Negative tests
    with pytest.raises(ValueError):
        calculate_discount(-100.0, 0.2)

支持的测试框架

语言框架自动检测
Pythonpytest
Pythonunittest
JavaScriptJest
Gotesting

TDD Cycle

红-绿-重构循环

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

🔄 TDD Cycle for src/calculator.py
================================

🔴 RED: Write a failing test
   Generated: tests/test_calculator.py
   Add your test case and run: tdd run

$ # 编辑测试文件...

$ python3 scripts/main.py run
🔴 Test FAILED (expected)

💚 GREEN: Make it pass
   Implement the function to pass the test

$ # 编辑实现...

$ python3 scripts/main.py run
💚 Test PASSED

♻️  REFACTOR: Improve the code
   Run: tdd run
   Check coverage: tdd coverage

$ python3 scripts/main.py run
♻️  All tests pass, ready to refactor

状态追踪

$ python3 scripts/main.py status

📊 TDD Status
=============
Current Phase: GREEN
Last Run: 2026-04-01 18:30:00
Tests: 5 passed, 0 failed
Coverage: 87%

Next: Refactor or add new test

Coverage

基本报告

python3 scripts/main.py coverage

输出:

📊 Coverage Report
==================
Total: 87%

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

未覆盖代码

python3 scripts/main.py coverage --uncovered

输出:

🔍 Uncovered Lines
==================
src/discount.py:45-52  calculate_bulk_discount
src/utils.py:12-30     validate_email

HTML 报告

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

Configuration

.tdd.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
  }
}

Examples

场景 1:为新功能写测试

# 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:提高覆盖率

# 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:回归测试

# 修复 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

Mutation Testing

测试测试的质量:

python3 scripts/main.py mutant src/

原理:

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

输出:

🧬 Mutation Testing
===================
Mutants: 45
Killed: 42 (93%) ✅
Survived: 3 (7%) ⚠️

Survived mutants:
- src/calculator.py:23  (changed + to -)
  Test didn't catch this!

CI/CD 集成

# .github/workflows/test.yml
name: Tests
on: [push, pull_request]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      
      - name: Run Tests
        run: python3 skills/tdd/scripts/main.py run
      
      - name: Coverage Check
        run: python3 skills/tdd/scripts/main.py coverage --fail-under 80

Files

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

Roadmap

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

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

Coding

Spicy Ai Video

Turn a 60-second talking head clip into 1080p high-energy edited videos just by typing what you need. Whether it's turning bland footage into visually intens...

Registry SourceRecently Updated
Coding

Video Maker Fast

Get polished MP4 videos ready to post, without touching a single slider. Upload your video clips (MP4, MOV, AVI, WebM, up to 500MB), say something like "trim...

Registry SourceRecently Updated
Coding

Generation Generator

generate text prompts or clips into AI generated videos with this skill. Works with MP4, MOV, PNG, JPG files up to 500MB. marketers, content creators, social...

Registry SourceRecently Updated
Coding

Editor On Android

Get edited MP4 clips ready to post, without touching a single slider. Upload your video clips (MP4, MOV, AVI, WebM, up to 500MB), say something like "trim th...

Registry SourceRecently Updated