npx skills add https://github.com/89jobrien/steve --skill test-driven-development先写测试,再写代码。始终如此。 TDD 不是关于测试——它是通过小而严谨的步骤进行设计。
□ 编写一个定义期望行为的测试
□ 运行测试 - 验证它失败
□ 验证它因正确的原因失败(不是语法错误)
□ 暂时不要编写实现代码
所需证据:显示带有失败消息的测试输出
□ 编写最少的代码使测试通过
□ 抵制添加额外功能的冲动
□ 运行测试 - 验证它通过
□ 如果测试仍然失败,修复实现(而不是测试)
所需证据:显示测试通过的输出
□ 移除代码重复(DRY)
□ 改进命名以提高清晰度
□ 将复杂逻辑提取到函数中
□ 运行所有测试 - 必须始终保持通过
□ 检查变更行的测试覆盖率
所需证据:显示重构后所有测试仍然通过
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
重构阶段之后,为下一个行为开始新的红色阶段。
❌ 错误:一次性测试整个功能
✅ 正确:一次一个测试。通过它。重构。下一个测试。
❌ 错误:"测试通过了,发布它!"
✅ 正确:始终重构。干净的代码是目标。
❌ 错误:先写代码,然后添加测试
✅ 正确:如果在测试之前代码已存在,删除它并从测试开始
❌ 错误:添加错误处理、边界情况、优化
✅ 正确:使当前测试通过的最简单实现
任务:"添加用户登录功能"
红色阶段:
1. 编写测试:test_login_with_valid_credentials()
2. 运行:pytest -v → 失败(不存在登录函数)
3. 证据:"FAILED - AttributeError: 'User' has no 'login'"
绿色阶段:
4. 编写最小的 login() 方法
5. 运行:pytest -v → 通过
6. 证据:"1 passed in 0.03s"
重构阶段:
7. 将密码哈希提取到单独的函数
8. 改进变量名
9. 运行:pytest -v → 通过(仍然)
10. 证据:"1 passed in 0.02s"
下一个循环:test_login_with_invalid_credentials()
def test_user_can_login():
# 准备
user = User(email="test@example.com", password="secret")
# 执行
result = user.login("secret")
# 断言
assert result.success is True
assert result.token is not None
def test_login_with_wrong_password():
# 给定一个已注册用户
user = create_user(password="correct")
# 当他们尝试用错误密码登录时
result = user.login("wrong")
# 那么登录应该失败
assert result.success is False
assert "Invalid password" in result.error
❌ 编写立即通过的测试(测试没有证明任何东西)
❌ 测试实现细节而不是行为
❌ 为"简单"代码跳过测试
❌ 在实现后编写测试"因为我们赶时间"
❌ 一次性验证许多事情的大型测试
✅ 每个测试验证一个行为
✅ 测试名称描述被测试的行为
✅ 测试是独立的(无共享状态)
✅ 测试运行快速(模拟外部依赖)
从红色阶段移动到绿色阶段之前:
[ ] 测试确实失败了吗?
[ ] 因正确的原因失败了吗?
[ ] 测试名称描述了行为吗?
从绿色阶段移动到重构阶段之前:
[ ] 测试通过了吗?
[ ] 实现是最小的吗?
[ ] 没有过早优化吗?
开始下一个循环之前:
[ ] 代码是干净的吗?
[ ] 所有测试仍然通过吗?
[ ] 准备好进行下一个行为了吗?
TDD 是一种设计技术,而不是测试技术。
循环永不改变:红色 → 绿色 → 重构 → 重复
先写测试迫使你思考:
- 我需要什么行为?
- 我怎么知道它有效?
- 最简单的实现是什么?
这会产生设计更好、更易于维护的代码。
每周安装次数
1
代码仓库
GitHub 星标数
4
首次出现
1 天前
安全审计
安装于
zencoder1
amp1
cline1
openclaw1
opencode1
cursor1
Write tests before code. Always. TDD is not about testing—it's about design through small, disciplined steps.
□ Write ONE test that defines desired behavior
□ Run test - verify it FAILS
□ Verify it fails for the RIGHT reason (not syntax error)
□ DO NOT write implementation yet
Evidence required: Show test output with failure message
□ Write MINIMAL code to make test pass
□ Resist urge to add extra features
□ Run test - verify it PASSES
□ If test still fails, fix implementation (not test)
Evidence required: Show test output with pass
□ Remove code duplication (DRY)
□ Improve naming for clarity
□ Extract complex logic into functions
□ Run ALL tests - must stay green throughout
□ Check test coverage on changed lines
Evidence required: Show all tests still passing after refactor
After REFACTOR, start new RED phase for next behavior.
❌ WRONG: Test entire feature in one go
✅ CORRECT: One test at a time. Pass it. Refactor. Next test.
❌ WRONG: "Tests pass, ship it!"
✅ CORRECT: ALWAYS refactor. Clean code is the goal.
❌ WRONG: Write code, then add tests after
✅ CORRECT: If code exists before test, delete it and start with test
❌ WRONG: Add error handling, edge cases, optimizations
✅ CORRECT: Simplest thing that makes THIS test pass
Task: "Add user login feature"
RED Phase:
1. Write test: test_login_with_valid_credentials()
2. Run: pytest -v → FAILED (no login function exists)
3. Evidence: "FAILED - AttributeError: 'User' has no 'login'"
GREEN Phase:
4. Write minimal login() method
5. Run: pytest -v → PASSED
6. Evidence: "1 passed in 0.03s"
REFACTOR Phase:
7. Extract password hashing to separate function
8. Improve variable names
9. Run: pytest -v → PASSED (still)
10. Evidence: "1 passed in 0.02s"
Next cycle: test_login_with_invalid_credentials()
def test_user_can_login():
# Arrange
user = User(email="test@example.com", password="secret")
# Act
result = user.login("secret")
# Assert
assert result.success is True
assert result.token is not None
def test_login_with_wrong_password():
# Given a registered user
user = create_user(password="correct")
# When they attempt login with wrong password
result = user.login("wrong")
# Then login should fail
assert result.success is False
assert "Invalid password" in result.error
❌ Write test that passes immediately (test proves nothing)
❌ Test implementation details instead of behavior
❌ Skip tests for "simple" code
❌ Write tests after implementation "because we're in a hurry"
❌ Large tests that verify many things at once
✅ Each test verifies ONE behavior
✅ Test names describe the behavior being tested
✅ Tests are independent (no shared state)
✅ Tests run fast (mock external dependencies)
Before moving from RED to GREEN:
[ ] Test actually fails?
[ ] Fails for the right reason?
[ ] Test name describes behavior?
Before moving from GREEN to REFACTOR:
[ ] Test passes?
[ ] Implementation is minimal?
[ ] No premature optimization?
Before starting next cycle:
[ ] Code is clean?
[ ] All tests still pass?
[ ] Ready for next behavior?
TDD is a DESIGN technique, not a testing technique.
The cycle never changes: RED → GREEN → REFACTOR → Repeat
Writing tests first forces you to think about:
- What behavior do I need?
- How will I know it works?
- What's the simplest implementation?
This produces better-designed, more maintainable code.
Weekly Installs
1
Repository
GitHub Stars
4
First Seen
1 day ago
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
zencoder1
amp1
cline1
openclaw1
opencode1
cursor1
站立会议模板:敏捷开发每日站会指南与工具(含远程团队异步模板)
10,500 周安装
Power BI DAX 公式优化器 - 提升性能、可读性与可维护性的专家指南
7,600 周安装
Kotlin MCP 服务器生成器 - 快速构建生产级模型上下文协议服务
7,500 周安装
GitHub Copilot 代码迁移指令生成器 | 自动分析代码演进与生成转换规则
7,600 周安装
Fabric Lakehouse 使用指南:核心概念、性能优化与数据管理实践
7,600 周安装
EditorConfig 专家 - 自动生成最佳实践配置文件,统一团队代码风格
7,600 周安装
BigQuery流水线审计:成本控制、安全与生产就绪度检查指南
7,500 周安装