انتقل إلى المحتوى
أمين أبوزيد
JNR-047

ممارسات الاختبار

  • 5 درس
  • 2 ساعة تدريب (تقدير)
في هذه الصفحة 8 موضوعات

نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.

نظرة عامة

الموضوعات

  • Arrange-Act-Assert
  • التجهيزات (Fixtures)
  • بيانات الاختبار
  • تجنب الاختبارات الهشّة

تعلّم أولًا

2 ساعة تدريب (تقدير)

مخرجات التعلم

  1. Structure tests with Arrange-Act-Assert and name them by behavior.
  2. Build test data with builders or object mothers instead of copy-paste.
  3. Identify brittle tests (time, order, randomness, over-specification) and fix them.
  4. Balance the test suite across unit, slice, and integration levels for speed and confidence.

الدروس

  1. 01Arrange-Act-Assert والتسمية
  2. 02بُناة بيانات الاختبار (Test data builders)
  3. 03الاختبارات المتقلبة والهشّة
  4. 04بناء مجموعة اختبارات سليمة
  5. 05عرض عملي: مطاردة اختبار متقلّب

المختبر والتمارين

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)

IDOutcomeTask
JNR-047-EX011Rename test1, placeOrderTest, and testCart into behavior names and restructure one test as Arrange-Act-Assert.
JNR-047-EX022Write a ProductBuilder with defaults and use it in three tests.
JNR-047-EX032Create an object mother with outOfStock(), expensive(), and discounted().
JNR-047-EX043Make a coupon-expiry test deterministic with an injected Clock.
JNR-047-EX053Replace a Thread.sleep(1000) with Awaitility or a synchronous call.
JNR-047-EX063List four causes of flaky tests and one fix for each.
JNR-047-EX074Given 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.

اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.

  1. 1In Arrange-Act-Assert, "Act" means:O1
  2. 2Best test name:O1
  3. 3A test data builder gives:O2
  4. 4An object mother is:O2
  5. 5Which causes a flaky test?O3
  6. 6Best way to control time in tests:O3
  7. 7A brittle test:O3
  8. 8Better than Thread.sleep for async checks:O3
  9. 9A healthy suite has the most:O4
  10. 10A suite with 150 slow integration tests and 5 unit tests suffers from:O4

الجزء العملي

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.

اكتب للبحث في المستويات والمراحل والوحدات والدروس.