TDD 的三条法则
深入理解 Robert Martin 提出的 TDD 三条法则,明确何时才能编写生产代码和测试代码。
TDD 的三条法则 是 CoddyKit 上的免费 Testing Mastery: JUnit, Mockito & Integration Tests 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Testing Mastery: JUnit, Mockito & Integration Tests 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Testing Mastery: JUnit, Mockito & Integration Tests 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Discipline Behind the Cycle
The red-green-refactor cycle becomes precise through three short rules known as the Three Laws of TDD, formulated by Robert C. Martin. They constrain exactly when you write code.
The First Law
Law 1: You may not write production code until you have written a failing unit test.
No code is added speculatively. Every line exists to satisfy a test.
The Second Law
Law 2: You may not write more of a unit test than is sufficient to fail. A compilation failure counts as failing.
The Third Law
Law 3: You may not write more production code than is sufficient to pass the currently failing test.
A Tiny Failing Test
Following Law 2, write only enough test to fail. Here the class does not even exist yet, so it fails to compile.
@Test
void addsTwoNumbers() {
assertEquals(5, new Calc().add(2, 3));
}Just Enough Production Code
By Law 3 you write the minimum to pass, even if it looks naive at first.
class Calc {
int add(int a, int b) { return a + b; }
}Tight Loop
The three laws lock you into a loop measured in seconds, not minutes: write a bit of failing test, write a bit of code, repeat.
Why So Strict?
The strictness guarantees every line of production code is covered by a test and keeps you from over-engineering features no test demands.
Avoiding Speculative Design
Because you only write code that a test requires, you naturally avoid YAGNI violations (You Aren't Gonna Need It) and dead code.
Practicing the Laws
The laws feel restrictive at first. With practice the loop becomes muscle memory, producing comprehensive test coverage almost as a side effect.
Relationship to Red-Green-Refactor
The laws drive the red and green phases; refactoring still happens once tests pass, with the safety net the laws built.
Quick Check
According to the first law, when may you write production code?
Recap
You learned the disciplined core of TDD:
- Law 1: no production code without a failing test
- Law 2: write only enough test to fail
- Law 3: write only enough code to pass
- The laws create a tight loop and full coverage
用 AI 导师学习 Testing Mastery: JUnit, Mockito & Integration Tests — 免费
在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。
- 课程
- 12
- 课程
- 48
常见问题解答
「TDD 的三条法则」课时是免费的吗?
是的 — 「TDD 的三条法则」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Testing Mastery: JUnit, Mockito & Integration Tests 课程的其余内容,请升级到 CoddyKit PRO。 Testing Mastery: JUnit, Mockito & Integration Tests 课程共包含 4 节课。
「TDD 的三条法则」这节课中我会学到什么?
深入理解 Robert Martin 提出的 TDD 三条法则,明确何时才能编写生产代码和测试代码。 你通过在浏览器中直接运行的动手代码来练习 Testing Mastery: JUnit, Mockito & Integration Tests,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Testing Mastery: JUnit, Mockito & Integration Tests 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Testing Mastery: JUnit, Mockito & Integration Tests 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「TDD 的三条法则」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Testing Mastery: JUnit, Mockito & Integration Tests 课中编写并运行代码吗?
能。每节 Testing Mastery: JUnit, Mockito & Integration Tests 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。