0Pricing
Testing Mastery: JUnit, Mockito & Integration Tests · درس

القوانين الثلاثة لـ TDD

استوعب قوانين Robert Martin الثلاثة لـ TDD التي تحدد بدقة متى يجوز لك كتابة شيفرة الإنتاج والاختبار.

القوانين الثلاثة لـ TDD درس مجاني في Testing Mastery: JUnit, Mockito & Integration Tests على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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

الأسئلة الشائعة

هل درس «القوانين الثلاثة لـ TDD» مجاني؟

نعم — نص درس «القوانين الثلاثة لـ TDD» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة 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 مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 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 يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. مقدمة إلى دورة TDD
  2. كتابة الاختبارات أولًا
  3. إعادة الهيكلة من أجل قابلية الاختبار
  4. القوانين الثلاثة لـ TDD
← العودة إلى Testing Mastery: JUnit, Mockito & Integration Tests