← 返回 Skills 市场
56
总下载
0
收藏
0
当前安装
2
版本数
在 OpenClaw 中安装
/install laosi-tdd
功能描述
测试驱动开发 - 严格红/绿/重构循环,一次一个垂直切片,测试先行。含pytest/mock示例和覆盖率检查
使用说明 (SKILL.md)
TDD — Test-Driven Development
激活词: TDD / 测试驱动 / 测试先行
核心循环
🔴 RED → 写一个失败的测试
🟢 GREEN → 写最少代码让测试通过
🔄 REFACTOR → 重构,同时保持所有测试通过
每次迭代不超过5分钟。如果超过,说明测试范围太大了。
Python 实现
from dataclasses import dataclass, field
from typing import List, Callable
@dataclass
class TestCase:
name: str
fn: Callable
passed: bool = False
error: str = ""
class TDDCycle:
def __init__(self):
self.tests: List[TestCase] = []
self.implementation: str = ""
def write_test(self, name: str, test_fn: Callable) -> TestCase:
"""RED阶段: 先写测试(预期会失败)"""
tc = TestCase(name=name, fn=test_fn)
self.tests.append(tc)
try:
test_fn()
tc.passed = True
except AssertionError as e:
tc.passed = False
tc.error = f"预期失败 ✅: {e}"
except Exception as e:
tc.passed = False
tc.error = f"预期失败 ✅: {e}"
return tc
def run_tests(self) -> dict:
"""GREEN阶段: 跑所有测试"""
results = {"passed": 0, "failed": 0, "total": len(self.tests)}
for tc in self.tests:
try:
tc.fn()
tc.passed = True
results["passed"] += 1
except Exception as e:
tc.passed = False
tc.error = str(e)
results["failed"] += 1
return results
def refactor(self, new_code: str):
"""REFACTOR阶段: 改进实现"""
self.implementation = new_code
results = self.run_tests()
if results["failed"] > 0:
raise RuntimeError(
f"重构后 {results['failed']} 个测试失败,回滚!"
)
return results
# 实际TDD演示:开发一个斐波那契函数
# 🔴 STEP 1: 写测试
def fib_test_basic():
assert fib(0) == 0
assert fib(1) == 1
def fib_test_nth():
assert fib(2) == 1
assert fib(3) == 2
assert fib(4) == 3
assert fib(5) == 5
def fib_test_edge():
try:
fib(-1)
assert False, "应该抛出异常"
except ValueError:
pass
def fib_test_large():
assert fib(50) == 12586269025
# 🟢 STEP 2: 实现(先写最简单的)
def fib(n: int) -> int:
if n \x3C 0:
raise ValueError("n must be non-negative")
if n \x3C= 1:
return n
a, b = 0, 1
for _ in range(2, n + 1):
a, b = b, a + b
return b
# 运行TDD循环
cycle = TDDCycle()
print("=== 🔴 RED: 先写测试(测试应该失败)===")
for name, fn in [("basic", fib_test_basic), ("nth", fib_test_nth),
("edge", fib_test_edge), ("large", fib_test_large)]:
tc = cycle.write_test(name, fn)
status = "✅" if tc.passed else "🔴"
print(f" {status} {tc.name}: {tc.error if tc.error else 'passed'}")
print("\
=== 🟢 GREEN: 实现功能(测试应该通过)===")
results = cycle.run_tests()
print(f" 通过: {results['passed']}/{results['total']}")
print("\
=== 🔄 REFACTOR: 优化代码 ===")
# 改用线性递归(虽然不如迭代,但展示了重构)
def fib_refactored(n: int, memo=None) -> int:
if memo is None:
memo = {0: 0, 1: 1}
if n \x3C 0:
raise ValueError("n must be non-negative")
if n not in memo:
memo[n] = fib_refactored(n - 1, memo) + fib_refactored(n - 2, memo)
return memo[n]
try:
cycle.refactor("fib_refactored")
# 重新定义函数
import builtins
builtins.fib = fib_refactored
results2 = cycle.run_tests()
print(f" 重构后: {results2['passed']}/{results2['total']} 通过 ✅")
except RuntimeError as e:
print(f" 重构失败: {e}")
pytest 集成
# test_fib.py — 用pytest写TDD测试
import pytest
def fib(n: int) -> int:
if n \x3C 0:
raise ValueError("n must be non-negative")
if n \x3C= 1:
return n
a, b = 0, 1
for _ in range(2, n + 1):
a, b = b, a + b
return b
class TestFib:
def test_base_cases(self):
assert fib(0) == 0
assert fib(1) == 1
def test_normal_cases(self):
assert fib(10) == 55
assert fib(20) == 6765
def test_large_input(self):
assert fib(100) == 354224848179261915075
def test_invalid_input(self):
with pytest.raises(ValueError):
fib(-1)
@pytest.mark.parametrize("n,expected", [
(0, 0), (1, 1), (2, 1), (3, 2),
(4, 3), (5, 5), (6, 8), (7, 13),
])
def test_parametrized(self, n, expected):
assert fib(n) == expected
# 命令行运行:
# pytest test_fib.py -v
# pytest test_fib.py --cov # 配合 coverage 看行覆盖率
TDD守则
| 规则 | 说明 |
|---|---|
| 测试先于实现 | 一行实现代码都不能写在测试之前 |
| 一次一个测试 | 写一个测试→让它通过→下一个 |
| 最小实现 | 写刚好让测试通过的最少代码 |
| 全绿才重构 | 有任何测试失败时,不允许重构 |
| 重构不改变行为 | 重构只改结构,不改功能 |
使用场景
- 新功能开发: 先定义接口契约(测试),再实现
- Bug修复: 先写复现Bug的测试,再修代码
- 重构遗留代码: 先写特性测试作为安全网
- 设计探索: 通过测试用例探索API设计
依赖
- Python 3.8+
- pytest (可选,用于自动化测试运行)
安全使用建议
This skill is reasonable to install if you want TDD workflow guidance. Treat the Python snippets as teaching examples before copying them into production code, and install pytest only in projects where you actually want automated test support.
能力评估
Purpose & Capability
The skill's stated purpose is test-driven development guidance, and the artifact content matches that purpose with a red/green/refactor workflow, Fibonacci examples, pytest examples, and TDD rules.
Instruction Scope
Instructions are scoped to software testing and development practice; there are no prompt overrides, hidden role changes, data collection instructions, or unrelated agent-control behavior.
Install Mechanism
The package contains a single markdown SKILL.md file with no executable scripts or declared package dependencies.
Credentials
The examples mention Python 3.8+ and optional pytest use, which is proportionate for a testing skill; there is no network access, credential handling, broad local indexing, or sensitive data flow.
Persistence & Privilege
No persistence, privilege escalation, background execution, startup hooks, or long-running worker behavior is present. One toy code example mutates Python builtins within an illustrative snippet, but it is not an install-time or hidden behavior.
如何使用
- 确保已安装 OpenClaw(本地或 Docker 部署)
- 在对话框中输入安装命令:
/install laosi-tdd - 安装完成后,直接呼叫该 Skill 的名称或使用
/laosi-tdd触发 - 根据 Skill 的参数说明提供必要输入,即可获得结构化输出
版本历史
v1.1.0
- Major documentation update: SKILL.md is now a comprehensive Chinese TDD guide.
- Added detailed TDD core loop explanation and practical Python code samples.
- Included a full TDD cycle demonstration with a Fibonacci implementation.
- Integrated pytest examples and code coverage usage.
- Expanded rules, scenarios, and dependencies sections for clearer guidance.
v1.0.0
Initial release of tdd skill.
- Implements strict test-driven development (TDD) workflow.
- Enforces red-green-refactor cycle with step-by-step guidance.
- Promotes writing tests first, then minimal code, followed by refactoring.
- Emphasizes vertical slicing and quality through disciplined testing.
元数据
常见问题
测试驱动开发 是什么?
测试驱动开发 - 严格红/绿/重构循环,一次一个垂直切片,测试先行。含pytest/mock示例和覆盖率检查. 它是一个面向 Claude Code / OpenClaw 的 AI Agent Skill 插件,目前累计下载 56 次。
如何安装 测试驱动开发?
在 OpenClaw 或 Claude Code 对话框中运行命令「/install laosi-tdd」即可一键安装,无需额外配置。
测试驱动开发 是免费的吗?
是的,测试驱动开发 完全免费,采用 MIT-0 许可证,可自由下载、安装和使用。
测试驱动开发 支持哪些平台?
测试驱动开发 跨平台运行,可在任意部署了 OpenClaw / Claude Code 的环境中使用(cross-platform)。
谁开发了 测试驱动开发?
由 534422530(@534422530)开发并维护,当前版本 v1.1.0。
推荐 Skills