กฎสามข้อของ TDD
ทำความเข้าใจกฎสามข้อของ TDD ตามแนวคิดของ Robert Martin ซึ่งกำหนดอย่างชัดเจนว่าคุณจะเขียนโค้ดสำหรับใช้งานจริงและโค้ดทดสอบได้เมื่อใด
กฎสามข้อของ TDD เป็นบทเรียน Testing Mastery: JUnit, Mockito & Integration Tests ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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
เรียนรู้ Testing Mastery: JUnit, Mockito & Integration Tests ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 48
คำถามที่พบบ่อย
บทเรียน “กฎสามข้อของ TDD” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “กฎสามข้อของ TDD” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Testing Mastery: JUnit, Mockito & Integration Tests ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Testing Mastery: JUnit, Mockito & Integration Tests มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “กฎสามข้อของ TDD”
ทำความเข้าใจกฎสามข้อของ TDD ตามแนวคิดของ Robert Martin ซึ่งกำหนดอย่างชัดเจนว่าคุณจะเขียนโค้ดสำหรับใช้งานจริงและโค้ดทดสอบได้เมื่อใด คุณปฏิบัติ Testing Mastery: JUnit, Mockito & Integration Tests ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Testing Mastery: JUnit, Mockito & Integration Tests หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Testing Mastery: JUnit, Mockito & Integration Tests บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “กฎสามข้อของ TDD” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Testing Mastery: JUnit, Mockito & Integration Tests นี้ได้ไหม
ได้ บทเรียน Testing Mastery: JUnit, Mockito & Integration Tests ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ