Claude Opus 5 (best for reasoning through non-obvious edge cases and generating well-structured test code); also works well with GPT-5.4 or Gemini 3 Pro for most languages, though Claude tends to catch more subtle edge cases in code-review-style tasks.You're a backend developer at a mid-size e-commerce company. You just finished writing a discount-calculation function for the checkout flow, code review is in 20 minutes, and you haven't written a single test yet.Developer Tools

مدقق تغطية الاختبارات: حوّل أي دالة إلى مجموعة pytest كاملة قبل مراجعة الكود

مشاركة:
مدقق تغطية الاختبارات: حوّل أي دالة إلى مجموعة pytest كاملة قبل مراجعة الكود

لماذا تهم هذه المطالبة

Untested edge cases in payment-adjacent code — negative discounts, null inputs, rounding at boundary values — are a recurring source of production billing incidents precisely because they're the paths manual testing skips under deadline pressure. A discount function that silently allows a 150% discount, or rounds $19.995 down instead of up, doesn't fail loudly in a code review; it fails quietly in a refund queue three weeks later. Teams that ship this kind of function without edge-case tests routinely discover the bug only after a customer notices they were overcharged or undercharged, at which point it's a support ticket and a manual reconciliation instead of a five-minute fix caught in CI.

فيم نستخدمها

You're a backend developer at a mid-size e-commerce company. You just finished writing a discount-calculation function for the checkout flow, code review is in 20 minutes, and you haven't written a single test yet.

المطالبة

Role: You are a senior QA engineer and test architect with expertise in [PROGRAMMING LANGUAGE] and [TESTING FRAMEWORK].

Context: I'm about to ship the following function/feature to production. Here is the code and its intended behavior: [PASTE CODE OR FUNCTION DESCRIPTION HERE]. This function will be called by [DESCRIBE CALLING CONTEXT, e.g., "a public API endpoint that receives untrusted user input" or "an internal batch job processing financial records"].

Task: Generate a comprehensive test suite for this function. Include: (1) happy-path unit tests covering the documented behavior, (2) edge cases (boundary values, empty/null inputs, extremely large inputs), (3) error scenarios and how the function should fail, (4) at least [NUMBER] integration test outlines describing how this function interacts with [DEPENDENT SYSTEM OR MODULE].

Constraints: Do not test implementation details that would break on refactor — test behavior, not internals. Flag any inputs where the expected behavior is ambiguous rather than guessing. Use [TESTING FRAMEWORK] syntax and conventions. Keep each test focused on one assertion or one logical scenario.

Output Format: Organize as four sections with headers (Happy Path, Edge Cases, Error Scenarios, Integration Tests). For each test, give: a one-line description, the test code block, and a one-sentence note on what would break if this test failed. End with a short list of any ambiguous behaviors you flagged.

النتيجة

Happy Path

Test: standard 20% discount applies correctly.
def test_standard_discount():
    assert apply_discount(100.0, 20.0) == 80.0

Breaks if: basic discount math regresses — the most visible failure a customer would notice immediately.

Edge Cases

Test: zero discount returns original price.
def test_zero_discount():
    assert apply_discount(50.0, 0.0) == 50.0

Breaks if: a no-op discount accidentally changes price via floating-point drift.

Test: 100% discount returns exactly zero.
def test_full_discount():
    assert apply_discount(75.0, 100.0) == 0.0

Breaks if: rounding leaves a residual charge like $0.01 on a 'free' item.

Test: price with sub-cent rounding boundary.
def test_rounding_boundary():
    assert apply_discount(19.99, 33.33) == 13.33

Breaks if: rounding mode rounds against the customer instead of to standard cents.

Error Scenarios

Test: negative discount percentage raises ValueError.
def test_negative_discount_raises():
    with pytest.raises(ValueError):
        apply_discount(100.0, -10.0)

Breaks if: a negative discount silently increases the price instead of being rejected.

Test: discount over 100% raises ValueError.
def test_over_100_percent_raises():
    with pytest.raises(ValueError):
        apply_discount(100.0, 150.0)

Breaks if: a checkout coupon bug applies a 150% discount and pays the customer to buy the item.

Integration Tests

1. Checkout API: verify the applied discount is persisted to the order record with the same rounding as the response body, so invoice totals match what the customer saw at checkout.
2. Coupon service: verify a discount capped by business rules (e.g. max $50 off) is enforced even when a stacked promo code would mathematically exceed it.

Ambiguous behaviors flagged: Should exactly 100.0% be allowed, or only up to 99.99%? Should the function round half-up or round-to-even (banker's rounding) at the cent boundary? Both need a product decision before the tests can be finalized.

كتابة الاختبارات تحت ضغط الموعد النهائي تعني دائمًا تقريبًا كتابة الاختبارات الأسهل تفكيرًا، وليس تلك التي قد تكشف فعليًا عن خلل في الإنتاج. تحصل الدالة على اختبار المسار السعيد لأن هذه هي الحالة التي انتهى المطوّر للتو من كتابة الكود لها — بينما تُهمَل الحالات الحدية التي قد تكشف خصمًا سالبًا أو إدخالًا فارغًا أو خطأ تقريب عند حد السنت.

لماذا يطلب البرومبت أربع فئات، وليس مجرد «اكتب اختبارات»

طلب كتابة اختبارات من الذكاء الاصطناعي دون تحديد ينتج نفس النقطة العمياء لدى مطوّر مستعجل: تغطية المسار السعيد بشكل أساسي مع حالة حدية أو اثنتين كرمزيات. إجبار المخرجات على أربع فئات مسمّاة — المسار السعيد، الحالات الحدية، سيناريوهات الأخطاء، واختبارات التكامل — يجعل كل فئة بندًا في قائمة تحقق.

لماذا يحتاج كل اختبار إلى ملاحظة «ماذا سيتعطل»

قائمة بأسماء الاختبارات لا تساعد المراجع على تحديد ما إذا كانت المجموعة كافية فعلًا — بل تثبت فقط وجود اختبارات. اشتراط جملة واحدة توضح أي فشل واقعي سيكشفه كل اختبار يحوّل المخرجات إلى شيء يمكن للمراجع تقييمه فعليًا.

لماذا يرفض تخمين السلوك الغامض

برومبتات توليد الاختبارات التي تنتج دائمًا مجموعة تبدو كاملة وواثقة خطيرة تحديدًا لأنها تبدو جديرة بالثقة. إجبار النموذج على وضع علامة على الغموض بدلًا من حله يحمي من اختبارات تُرسّخ بهدوء تخمينًا غير مُراجَع كمصدر حقيقة لقاعدة الكود.

لماذا يطلب مخططات اختبار التكامل، وليس مجرد مزيد من اختبارات الوحدة

اختبارات الوحدة تتحقق من دالة بمعزل عن غيرها، لكن معظم حوادث الإنتاج تحدث عند نقطة التقاء وحدتين تعملان بشكل صحيح كل على حدة. طلب مخططات اختبار تكامل مقابل سياق الاستدعاء يكشف فئة الأخطاء التي تجتاز كل اختبار وحدة لكنها لا تزال تُفسد مجموعًا يراه العميل.

testingdeveloper toolscode reviewqapytest
مشاركة: