TDD 红绿重构测试驱动开发
tdd
当在任何项目中使用与框架无关的 TDD 红-绿-重构方法时;不适用于仅对运行时证据进行分类或替换项目测试框架的场景;先证明测试在修改前失败、修改后通过,再评估重构。
- License 许可
- MIT
- 所属仓库
- mintuz/skills
- 最近更新
- 2026-11-13
- Stars
- ★ 35
- Forks
- 7
01 前言 Front Matter
- name
- tdd
- description
- WHEN using framework-neutral TDD Red-Green-Refactor in any project; NOT for merely classifying runtime evidence or replacing the project's test framework; proves failing-before and passing-after tests, then assesses refactoring.
02 资源文件 4
- 参考文档references/patterns.md3.9 KB
- 参考文档references/test-factories.md5.7 KB
- 参考文档references/violations.md4.8 KB
- 参考文档references/workflow-examples.md3.9 KB
03 SKILL.md 正文
TDD Best Practices
Core Principle
Every line of new or changed behaviour must be written in response to a failing test.
This is non-negotiable: production code that adds or changes behaviour without a failing test demanding it is not TDD. Refactoring is the one exception: it changes structure while every test stays green, so it demands no new failing test.
Only an actual failing-before and passing-after behaviour test proves a RED/GREEN cycle. A build, typecheck or static page render does not replace it. Direct interaction with an identified running artifact and its observed effects is valid bounded runtime evidence, but does not retroactively prove TDD. Keep that evidence; if a fix preceded its test, reverse only your own fix, preserve unrelated work, and run the discriminating regression against pre-fix code before reapplying the minimal implementation. Report the command, result and what it proves at each transition (principle-prove-it-works).
Use the project's existing runner and test framework across web, iOS, CLI,
library and service projects. Retain existing XCTest suites, including measure
performance tests; TDD does not require migration to Swift Testing. Framework
authoring belongs to the matching skill or the suite's established conventions.
Treat coverage as a by-product of the required behaviour cases, not a separate coverage-only task. Use a coverage report to discover missing business cases; never add implementation-detail assertions merely to satisfy a percentage.
The Sacred Cycle: Red → Green → Refactor
1. RED - Write a Failing Test
Write one test that describes the desired behaviour. Run it against the pre-change production code. The test must fail because the behaviour does not exist yet.
A failure that names the absent unit under test is a valid RED: an unresolved import of the module you are about to create, an undefined export you are about to add, or a wrong, missing, or thrown result from the code under test.
Any other failure is a harness fault, not RED: a wrong import path, a typo in the test file, a missing dependency, a broken runner configuration, or a compile error in unrelated code. Repair the harness fault first, preserving unrelated work and every existing test. Rerun the test before you write production code.
Never duplicate an existing test.
Rules:
- An unexpected pass is a stop condition. Do not add or adapt the implementation. Inspect the production code and name the reason for the pass: the behaviour already exists, the test is ineffective, or your own production edit is already in the working tree. Strengthen an ineffective test and rerun it. Stop the slice when the behaviour already exists.
- A pass caused by your own production edit is a false RED, not evidence. Reverse only your own edit, keep the test, and rerun it. Never revert, stash, or reset work you did not write (
principle-preserve-unrelated-work). - Run the narrowest scope that exercises the new test, then confirm the failure is the one you intended. A broad run buries that failure in unrelated output.
- Start with the simplest behaviour.
- Test ONE thing at a time. Write the next test only after the current one is green. Record the behaviours still to come as a list or as empty placeholders, never as further written tests.
- Test business behaviour, not implementation (
principle-test-behavior-not-implementation). - Name tests to document intent.
- Use fresh, explicit test data: factories in JavaScript/TypeScript; simple local immutable Swift values need no factory. Keep owned-resource lifecycle hooks.
2. GREEN - Minimal Implementation
Write the minimum code to make the current test pass. Nothing more.
Rules:
- No "just in case" logic or speculative features (
principle-foundational-thinking). - If you are writing more than the test needs, STOP and question why.
- After focused GREEN, run the relevant existing regression suite as a distinct check before declaring the slice safe (
principle-scale-verification).
3. REFACTOR - Assess and Improve
After every GREEN, assess whether refactoring would add value: once per behaviour, not once after a batch. If the code is already clean, move on. The refactoring skill owns the full procedure; these rules apply in every cycle:
- Secure a reversible GREEN baseline first. Commit the green code when you are allowed to commit; when the user forbids a commit, keep the green state recoverable another way. Never refactor code you cannot restore (
principle-capture-the-baseline). - Assess the changed, covered code before expanding to untested areas. Name the cleanup justified by the available code. If no worthwhile cleanup is justified, say so and leave that code unchanged. Never invent a cleanup to complete the cycle.
- Refactor only code that green tests already cover. For untested code, first write a characterisation test that records the output the code actually produces today and passes against unchanged production code. Without it, leave the code alone, whether or not it looks trivial.
- Keep external APIs unchanged and all tests passing.
- Keep the refactor separate from the behaviour change: a separate commit, or a separate reviewable step when you cannot commit.
Guides
| Guide | Use for |
|---|---|
| workflow-examples.md | Full RED-GREEN-REFACTOR walkthroughs (free shipping, payment validation); when to refactor vs move on |
| test-factories.md | Factory functions with overrides, why they beat let/beforeEach, composing factories, organising tests by behaviour, no 1:1 test-to-implementation mapping, 100% coverage through behaviour |
| violations.md | Critical violations, high-priority issues, style issues, behaviour vs implementation examples |
| patterns.md | Behaviour-focused tests, public-API-only testing, factories with schema validation, composing factories, test naming and organisation |
Decision Trees
Should I write this code?
Does this code add or change behavior?
├── No (pure refactor under green tests) → Keep every test green
└── Yes → Is there a failing test demanding it?
├── Yes → Write minimal code to pass
└── No → Write the failing test first
Is my test good?
Does the test verify a business outcome?
├── Yes → Does it use the public API only?
│ ├── Yes → Is its test data fresh and explicit?
│ │ ├── Yes (JS/TS factories; simple local immutable Swift values also qualify) → Good test ✓
│ │ └── No → Use factories in JS/TS; remove shared mutable fixtures
│ └── No → Rewrite to avoid internals
└── No → Rewrite to focus on behavior
How much code should I write?
Does this code make the current failing test pass?
├── Yes → Is there any code that could be removed
│ and tests still pass?
│ ├── Yes → Remove it
│ └── No → Done
└── No → Keep writing minimal code
Checklist Before Committing
- [ ] All new or changed behaviour has a test that demanded it
- [ ] Tests verify behaviour through the public API only, not implementation details
- [ ] Implementation is minimal
- [ ] Refactoring assessment completed
- [ ] All tests pass
- [ ] Test data is fresh and explicit. JavaScript/TypeScript fixture data uses factories, not
letdeclarations orbeforeEachbuilders. Local immutable Swiftletbindings are valid. Keep legitimate resource lifecycle hooks. - [ ] Test names describe business behaviour
- [ ] Edge cases covered
- [ ] Test organisation reflects business features
- [ ] 100% coverage achieved through behaviour testing
04 同仓库更多 Skills
lens-review
USE WHEN reviewing uncommitted work, a branch diff or a pull request through several installed skills, each run as its own review lens; NOT WHEN you need one cold second opinion (double-check), a verdict against a specification (acceptance-review) or the built-in fixed-lens /code-review. Runs each lens in an isolated read-only agent, has a separate agent try to refute every finding, and returns one severity-ranked report.
XState 状态机建模
xstate
当 React 状态因外部响应而变化——异步提交、进行中或加载标志、重试、取消、轮询、乐观更新、过期响应——或因与可中断的过程(如拖拽或手势)交互而变化时使用;或在构建、审查、测试或可视化 XState 5 actors 和 @xstate/react 集成时使用;不适用于单个事件即可完全设定的值,如文本草稿、焦点标志或简单的开关切换;建模类型化生命周期并渲染合法的 Mermaid stateDiagram-v2 图表。
网页界面设计优化
web-design
在优化 UI 布局、排版、色彩或细节打磨时使用;不涉及代码实现;提供简洁的原则,以实现有意图、易读的设计。
Tailwind CSS 样式开发
tailwind
在编写、修复或审查使用 Tailwind CSS 的项目中的样式时使用,即便只是改一个类名,或请求中未提及 Tailwind:颜色、焦点环、间距、由 prop、map 或 CVA 变体选择的类名、Tailwind 样式表或 tailwind.config 中的主题令牌与深色模式、配置或升级(如不确定项目是否使用 Tailwind,先检查 package.json 或主样式表);不适用于在没有 Tailwind 的项目中编写纯 CSS 或 Sass;产出的类名能被 Tailwind 识别,语法适配已安装的主版本,具有可见的焦点指示器,且不引入未要求的依赖或杜撰的品牌值。
代码重构规划
refactoring
当你需要制定计划来重构、重组或清理正常工作的代码,且测试结果已知(全绿或已记录的失败集合)时使用;不适用于新功能交付或任何行为变更;以可逆的基线安全性和按优先级排序的改进为核心。
react
USE WHEN changing the logic, data, props, state, Effects or rendered text of a React component, hook or page, even a one-line change; NOT WHEN the request is styling only (class names, Tailwind, CSS, dark mode, spacing, shadows) or tests only, or when it models submit, in-flight, loading, retry or failure states, even for a named React form (xstate covers that, including its React wiring); applies React 19 composition, state, Effect, structure, API and performance rules with only the project's installed libraries.