Test-Driven Development
TDDالتطوير بالاختبار أولاً
Definition
A development practice where you write a failing test first, then write only enough code to make it pass, then refactor — ensuring every line of code is tested.
ممارسة تطوير تكتب فيها اختباراً فاشلاً أولاً ثم تكتب فقط ما يكفي من الكود لتمريره ثم تُعيد الهيكلة — مما يضمن اختبار كل سطر كود.
Why It Matters
TDD forces you to define the expected behavior before you implement it. This prevents 'write code, then write tests that pass because you know what the code does' — which is circular and doesn't prevent bugs.
يُجبرك TDD على تعريف السلوك المتوقع قبل تطبيقه. يمنع هذا 'كتابة الكود ثم كتابة اختبارات تنجح لأنك تعرف ما يفعله الكود' — وهو أمر دائري ولا يمنع الأخطاء.
Full Definition
Example Usage
“TDD for `buildSlug('Large Language Model')`: Red: `expect(buildSlug('Large Language Model')).toBe('large-language-model')` — test fails. Green: `function buildSlug(name) { return name.toLowerCase().trim().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, ''); }` — test passes. Refactor: add TypeScript types, export the function.”
“TDD لـ `buildSlug('Large Language Model')`: أحمر: `expect(buildSlug('Large Language Model')).toBe('large-language-model')` — الاختبار يفشل. أخضر: `function buildSlug(name) { return name.toLowerCase().trim().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, ''); }` — الاختبار ينجح. إعادة هيكلة: إضافة أنواع TypeScript وتصدير الدالة.”
AI Builder Tips
Avoid these mistakes when using Test-Driven Development:
Writing the implementation first and then tests to match — this produces tests that certify behavior instead of specifying intended behavior
Writing too many assertions per test — each test should verify one behavior so failures are easy to diagnose
Applying TDD to UI components — UI layout and visual hierarchy are poorly served by TDD; use snapshot testing or visual regression tools instead
Sign in to unlock guided AI explanations from AI Teacher.
Generate a Prompt
Copy this prompt and use it directly with any AI model — no setup needed.
Help me build a project using Test-Driven Development. Explain: 1. What is Test-Driven Development and why it matters 2. The core architecture and required tools 3. Step-by-step implementation plan 4. Common mistakes to avoid: Writing the implementation first and then tests to match — this produces tests that certify behavior instead of specifying intended behavior, Writing too many assertions per test — each test should verify one behavior so failures are easy to diagnose, Applying TDD to UI components — UI layout and visual hierarchy are poorly served by TDD; use snapshot testing or visual regression tools instead 5. Best practices and production tips