ممارسات الاختبار
في هذه الصفحة 8 موضوعات
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
نظرة عامة
الموضوعات
- Arrange-Act-Assert
- التجهيزات (Fixtures)
- بيانات الاختبار
- تجنب الاختبارات الهشّة
تعلّم أولًا
2 ساعة تدريب (تقدير)
مخرجات التعلم
- Structure tests with Arrange-Act-Assert and name them by behavior.
- Build test data with builders or object mothers instead of copy-paste.
- Identify brittle tests (time, order, randomness, over-specification) and fix them.
- Balance the test suite across unit, slice, and integration levels for speed and confidence.
الدروس
المختبر والتمارين
Lab
JNR-047-LAB1 · Suite cleanup
Outcomes: 1–4 · Time: ~2 h Refactor the Shop test suite: builders, deterministic clocks, no order dependence; record runtime before and after. Acceptance: 10 consecutive runs green.
Exercises (assessment: test-quality review)
| ID | Outcome | Task |
|---|---|---|
| JNR-047-EX01 | 1 | Rename test1, placeOrderTest, and testCart into behavior names and restructure one test as Arrange-Act-Assert. |
| JNR-047-EX02 | 2 | Write a ProductBuilder with defaults and use it in three tests. |
| JNR-047-EX03 | 2 | Create an object mother with outOfStock(), expensive(), and discounted(). |
| JNR-047-EX04 | 3 | Make a coupon-expiry test deterministic with an injected Clock. |
| JNR-047-EX05 | 3 | Replace a Thread.sleep(1000) with Awaitility or a synchronous call. |
| JNR-047-EX06 | 3 | List four causes of flaky tests and one fix for each. |
| JNR-047-EX07 | 4 | Given 5 unit, 40 slice, and 120 full integration tests, explain what is wrong and how to rebalance. |
أظهر الحلول
- EX01
placingOrderReducesStock,rejectsOrderWhenCartIsEmpty,totalIncludesTax. - EX02 Defaults valid;
aProduct().withStock(0).build(). - EX03
static Product outOfStock() { return aProduct().withStock(0).build(); }and so on. - EX04
Clock.fixed(Instant.parse(...), UTC)passed into the service; assert before and after expiry by moving the clock. - EX05
await().atMost(2, SECONDS).until(() -> repo.count() == 1);, or make the code run synchronously in tests. - EX06 Time (fixed clock), order (isolated data), randomness (seed), async/network (await or stub).
- EX07 Too many slow tests; move logic checks down to unit tests and keep a few integration journeys.
- Common mistakes (tests coupled to implementation) are checked in the assessment review.
الاختبار
Outcomes: O1 AAA and naming · O2 test data · O3 brittle and flaky tests · O4 suite balance.
اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.
الجزء العملي
Seven exercises JNR-047-EX01 to EX07 plus the lab; for the assessment, review the quality of a given test suite. Pass: quiz 8/10, at least five exercises correct, and 10 consecutive green runs in the lab.
أخطاء شائعة
Thread.sleep in tests; tests coupled to implementation details.
التقييم
Test-quality review.
الصلة بالمسار الوظيفي
Test strategy at Senior and Staff levels.