test-driven-development
ما الذي تفعله
يرشد وكيل الذكاء الاصطناعي إلى طريقة لبناء البرمجيات تبدأ بالاختبار، فيُكتب الاختبار قبل الشيفرة التي يغطيها. وهي موجهة للمطوّرين الذين يريدون شيفرة تعمل ودليلًا على أن كل تغيير يفعل ما ينبغي.
متى تستخدمها
- ✓عندما تطلب ميزة جديدة أو إصلاح خطأ أو تغييرًا في السلوك داخل الشيفرة.
- ✓عندما تريد إعادة هيكلة الشيفرة بأمان مع وجود اختبارات.
- ✓عندما يحتاج خطأ إلى إثبات أنه أُصلح باختبار كان فاشلًا قبل التغيير.
رابط المهارة
https://github.com/obra/superpowers/blob/main/skills/test-driven-development/SKILL.mdانسخه والصقه في الذكاء الاصطناعي الذي تستخدمه (Claude أو ChatGPT أو Cursor) واطلب منه اتباع التعليمات.
طريقة الاستخدام
أسهل طريقة: انسخ الرسالة والصقها في الذكاء الاصطناعي الذي تستخدمه. سيقرأ المهارة وينفّذ تعليماتها.
رسالة جاهزة للصق
لـ Claude Code أو Codex أو Cursor: شغّله داخل مجلد مشروعك وستُثبَّت المهارة.
نزّل ملف SKILL.md وضعه في مجلد مهارات وكيلك.
مثال على ما تطلبه من الذكاء الاصطناعي
“أضف ميزة تسمح للمستخدمين بتصدير قائمتهم كملف CSV، واكتب الاختبارات أولًا.”
5 مهارات جديدة كل أسبوع. قراءة في دقيقة واحدة.
كيف تعمل
تكتب المهارة اختبارًا للسلوك الجديد، وتشغّله لتتأكد من فشله، ثم تكتب أقل شيفرة لازمة لنجاحه. بعدها تنظّف الشيفرة مع إبقاء الاختبارات ناجحة.
المحتوى التقني (ملف SKILL.md الأصلي)
النص الأصلي من المؤلف، مباشرة من GitHub.
Test-Driven Development (TDD)
Overview
Write the test first. Watch it fail. Write minimal code to pass.
Core principle: If you didn't watch the test fail, you don't know if it tests the right thing.
Violating the letter of the rules is violating the spirit of the rules.
When to Use
Always:
- New features
- Bug fixes
- Refactoring
- Behavior changes
Exceptions (ask your human partner):
- Throwaway prototypes
- Generated code
- Configuration files
Thinking "skip TDD just this once"? Stop. That's rationalization.
The Iron Law
NO PRODUCTION CODE WITHOUT A FAILING TEST FIRST
Write code before the test? Delete it. Start over.
No exceptions:
- Don't keep it as "reference"
- Don't "adapt" it while writing tests
- Don't look at it
- Delete means delete
Implement fresh from tests. Period.
Red-Green-Refactor
digraph tdd_cycle {
rankdir=LR;
red [label="RED\nWrite failing test", shape=box, style=filled, fillcolor="#ffcccc"];
verify_red [label="Verify fails\ncorrectly", shape=diamond];
green [label="GREEN\nMinimal code", shape=box, style=filled, fillcolor="#ccffcc"];
verify_green [label="Verify passes\nAll green", shape=diamond];
refactor [label="REFACTOR\nClean up", shape=box, style=filled, fillcolor="#ccccff"];
next [label="Next", shape=ellipse];
red -> verify_red;
verify_red -> green [label="yes"];
verify_red -> red [label="wrong\nfailure"];
green -> verify_green;
verify_green -> refactor [label="yes"];
verify_green -> green [label="no"];
refactor -> verify_green [label="stay\ngreen"];
verify_green -> next;
next -> red;
}
RED - Write Failing Test
Write one minimal test showing what should happen.
<Good> ```typescript test('retries failed operations 3 times', async () => { let attempts = 0; const operation = () => { attempts++; if (attempts < 3) throw new Error('fail'); return 'success'; };const result = await retryOperation(operation);
expect(result).toBe('success'); expect(attempts).toBe(3); });
Clear name, tests real behavior, one thing
</Good>
<Bad>
```typescript
test('retry works', async () => {
const mock = jest.fn()
.mockRejectedValueOnce(new Error())
.mockRejectedValueOnce(new Error())
.mockResolvedValueOnce('success');
await retryOperation(mock);
expect(mock).toHaveBeenCalledTimes(3);
});
Vague name, tests mock not code </Bad>
Requirements:
- One behavior
- Clear name
- Real code (no mocks unless unavoidable)
Verify RED - Watch It Fail
MANDATORY. Never skip.
npm test path/to/test.test.ts
Confirm:
- Test fails (not errors)
- Failure message is expected
- Fails because feature missing (not typos)
Test passes? You're testing existing behavior. Fix test.
Test errors? Fix error, re-run until it fails correctly.
GREEN - Minimal Code
Write simplest code to pass the test.
<Good> ```typescript async function retryOperation<T>(fn: () => Promise<T>): Promise<T> { for (let i = 0; i < 3; i++) { try { return await fn(); } catch (e) { if (i === 2) throw e; } } throw new Error('unreachable'); } ``` Just enough to pass </Good> <Bad> ```typescript async function retryOperation<T>( fn: () => Promise<T>, options?: { maxRetries?: number; backoff?: 'linear' | 'exponential'; onRetry?: (attempt: number) => void; } ): Promise<T> { // YAGNI } ``` Over-engineered </Bad>Don't add features, refactor other code, or "improve" beyond the test.
Verify GREEN - Watch It Pass
MANDATORY.
npm test path/to/test.test.ts
Confirm:
- Test passes
- Other tests still pass
- Output pristine (no errors, warnings)
Test fails? Fix code, not test.
Other tests fail? Fix now.
"Other tests" means the project's suite, not just your file. A
green run of the test you wrote is not a green suite. Before you call
the change done, run the project's test command (bare pytest,
npm test, cargo test — whatever the repo uses) even when your task
named only one test file. A scope statement in your task bounds the
deliverable, not your verification. Any failure that run shows —
including one you didn't cause — goes in your report by name; a red
test you watched scroll past and didn't mention is a report falsified
by omission.
REFACTOR - Clean Up
After green only:
- Remove duplication
- Improve names
- Extract helpers
Keep tests green. Don't add be
الأسئلة الشائعة
هل يمكنها تخطي الاختبارات لنموذج أولي سريع؟
النماذج الأولية التي تُرمى والشيفرة المولّدة استثناءات، لكن المهارة تسألك قبل تخطي الاختبارات.
ماذا لو كُتبت الشيفرة قبل اختبارها؟
تقول المهارة إن هذه الشيفرة يجب حذفها وإعادة كتابتها من الاختبارات، حتى يقود الاختبار التصميم فعلًا.
لماذا نرى الاختبار يفشل؟
قد لا يختبر الاختبار الذي لم يُرَ فاشلًا الشيء الصحيح.
التفاصيل
الوصف الأصلي من المؤلف
Use when implementing any feature or bugfix, before writing implementation code
repo: obra/superpowers
الشعبية
- نجوم GitHub
- 311k
- الترتيب
- #95
- الترخيص
- MIT
- آخر تحديث
- 2/11/2026
التعليقات
- …
المزيد من المهارات من هذا المستودع
brainstorming
311kيساعد وكيلك على معرفة ما تريده فعلًا قبل بناء أي شيء جديد، عبر أسئلة مركّزة وعرض خيارات واضحة.
executing-plans
311kيساعد وكيلك على تنفيذ خطة تنفيذ مكتوبة خطوة بخطوة داخل الجلسة الحالية، مع اختبار كل خطوة قبل الانتقال إلى التالية.
finishing-a-development-branch
311kيساعد وكيلك على إنهاء شيفرة مكتملة بتشغيل الاختبارات، ثم عرض خيارات واضحة للدمج أو فتح طلب سحب أو الإبقاء على الفرع.
receiving-code-review
311kيساعد وكيلك على تقييم ملاحظات مراجعة الشيفرة بعناية، والتحقق من كل نقطة مقابل الشيفرة قبل تغيير أي شيء.
مهارات مشابهة
migrate-to-shoehorn
310kيستبدل تأكيدات النوع الخطرة في ملفات الاختبار بمكتبة shoehorn، حتى تتمكن الاختبارات من استخدام بيانات جزئية وتجتاز فحوص الأنواع.
cpp-testing
280kيساعد وكيلك على كتابة اختبارات لشيفرة C++ وتحسينها، وعلى إعداد أدوات الاختبار وإصلاح الأخطاء فيها.
csharp-testing
280kيساعد وكيلك على إنشاء اختبارات شيفرة C# و.NET، ويستعمل أفضل الممارسات ليكون الاختبار فعالًا وموثوقًا.