Communitygithub.com

测试驱动开发

测试驱动开发。当用户希望以测试为先的方式构建功能或修复错误、提到“红-绿-重构”,或需要集成测试时使用。

测试驱动开发 是什么?

该技能提供了一套完整的测试驱动开发(TDD)方法论,强调通过公共接口验证行为而非实现细节,以确保测试在内部重构时依然健壮。它明确区分了“好测试”和“坏测试”:好测试是集成风格的,描述系统做什么而不是怎么做,能够存活于重构;坏测试与实现耦合,会在重构时无故失败。技能特别反对“水平分割”做法(即先写完所有测试再写所有实现),提倡“垂直切片”或“示踪子弹”式循环:一次一个测试、一次一个实现、反复迭代。工作流程包括与用户确认公共接口和待测行为、为每个行为编写单一测试、用最小代码使其通过、最后重构。该技能支持在Claude Code、Cursor、Codex等AI agent平台上使用,通过检查清单确保测试聚焦行为、使用公共接口、可抵御重构。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/Lev-it/lev-skills/tree/HEAD/.claude/skills/tdd

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

测试驱动开发 是做什么的?

哲学

核心原则:测试应通过公共接口验证行为,而非实现细节。代码可以完全改变;测试不应改变。

好测试是集成风格的:它们通过公共 API 执行真实代码路径。它们描述的是系统做什么,而不是怎么做。好的测试读起来像一份规范——“用户可以用有效购物车结账”告诉你确切存在什么能力。这些测试能经受重构,因为它们不关心内部结构。

坏测试与实现耦合。它们模拟内部协作者、测试私有方法,或通过外部手段(例如直接查询数据库而不是使用接口)进行验证。警示信号:重构时测试失败,但行为并未改变。如果你重命名一个内部函数导致测试失败,那么这些测试测试的是实现,而不是行为。

有关示例,请参见 tests.md;有关模拟指南,请参见 mocking.md。

反模式:水平分割

不要先编写所有测试,再编写所有实现。 这就是“水平分割”——将 RED 阶段视为“编写所有测试”,将 GREEN 阶段视为“编写所有代码”。

这会产生糟糕的测试:

  • 批量编写的测试测试的是想象的行为,而不是实际的行为
  • 你最终会测试事物的形状(数据结构、函数签名),而不是面向用户的行为
  • 测试对真实变化不敏感——行为破坏时通过,行为正常时失败
  • 你盲目前行,在理解实现之前就确定了测试结构

正确方法:通过示踪子弹进行垂直切片。一个测试 → 一个实现 → 重复。每个测试都响应你从上一个循环中学到的东西。因为你刚刚写了代码,所以你确切知道什么行为重要以及如何验证它。

错误(水平):
  RED:   test1, test2, test3, test4, test5
  GREEN: impl1, impl2, impl3, impl4, impl5

正确(垂直):
  RED→GREEN: test1→impl1
  RED→GREEN: test2→impl2
  RED→GREEN: test3→impl3
  ...

工作流程

1. 规划

探索代码库时,阅读 CONTEXT.md(如果存在),以使测试名称和接口词汇与项目的领域语言匹配,并尊重你所触及区域的 ADR。

在编写任何代码之前:

  • 与用户确认需要哪些接口更改
  • 与用户确认要测试哪些行为(优先排序)
  • 识别深层模块的机会(小接口,深实现)——运行 /codebase-design 技能以获取词汇和可测试性检查
  • 列出要测试的行为(而非实现步骤)
  • 获取用户对计划的批准

提问:“公共接口应该是什么样的?哪些行为最重要需要测试?”

你不可能测试所有东西。 与用户确认确切哪些行为最重要。将测试工作集中在关键路径和复杂逻辑上,而不是每个可能的边界情况。

2. 示踪子弹

编写一个测试,确认系统的一个事实:

RED:   编写第一个行为的测试 → 测试失败
GREEN: 编写最简代码使其通过 → 测试通过

这是你的示踪子弹——证明路径端到端可用。

3. 增量循环

对于剩余每个行为:

RED:   编写下一个测试 → 失败
GREEN: 最简代码使其通过 → 通过

规则:

  • 一次一个测试
  • 只编写足以通过当前测试的代码
  • 不要提前为未来测试做准备
  • 保持测试聚焦于可观察行为

4. 重构

所有测试通过后,查找重构候选:

  • 提取重复
  • 深化模块(将复杂性放到简单接口背后)
  • 在自然的地方应用 SOLID 原则
  • 考虑新代码揭示了现有代码的什么信息
  • 每次重构步骤后运行测试

永远不要在 RED 状态下重构。 先达到 GREEN。

每个循环的检查清单

[ ] 测试描述行为,而非实现
[ ] 测试只使用公共接口
[ ] 测试在内部重构后依然能通过
[ ] 代码对于此测试是最简的
[ ] 没有添加推测性功能

Individual skills in this repo

This repo contains 11 individual skills — each has its own dedicated page.

Lev-it/lev-skills

Ask which skill or flow fits your situation. A router over the user-invoked skills in this repo.

Lev-it/lev-skills

Premium brand-kit image generation skill for creating high-end brand-guidelines boards, logo systems, identity decks, and visual-world presentations. Trained for minimalist, cinematic, editorial, dark-tech, luxury, cultural, security, gaming, developer-tool, and consumer-app brand systems. Optimized for intentional logo concepting, refined composition, sparse typography, strong symbolic meaning, premium mockups, art-directed imagery, and flexible grid layouts.

Lev-it/lev-skills

Compact the current conversation into a handoff document for another agent to pick up.

Lev-it/lev-skills

Ink terminal renderer for json-render that turns JSON specs into interactive terminal UIs. Use when working with @json-render/ink, building terminal UIs from JSON, creating terminal component catalogs, or rendering AI-generated specs in the terminal.

Lev-it/lev-skills

Interactive QA session where user reports bugs or issues conversationally, and the agent files GitHub issues. Explores the codebase in the background for context and domain language. Use when user wants to report bugs, do QA, file issues conversationally, or mentions "QA session".

Lev-it/lev-skills

Facilitate a structured sprint retrospective — what went well, what didn't, and prioritized action items with owners and deadlines. Use when running a retrospective, reflecting on a sprint, creating action items from team feedback, or learning how to run effective retros.

Lev-it/lev-skills

Review the changes since a fixed point (commit, branch, tag, or merge-base) along two axes — Standards (does the code follow this repo's documented coding standards?) and Spec (does the code match what the originating issue/PRD asked for?). Runs both reviews in parallel sub-agents and reports them side by side. Use when the user wants to review a branch, a PR, work-in-progress changes, or asks to "review since X".

Lev-it/lev-skills

Teach the user a new skill or concept, within this workspace.

Lev-it/lev-skills

Turn the current conversation into a PRD and publish it to the project issue tracker — no interview, just synthesis of what you've already discussed.

Lev-it/lev-skills

Move issues through a state machine of triage roles — categorise, reproduce, grill if needed, and write agent-ready briefs.

Lev-it/lev-skills

Create product backlog items in Why-What-Acceptance format — independent, valuable, testable items with strategic context. Use when writing structured backlog items, breaking features into work items, or using the WWA format.

相关技能