Claude Sonnet 4.6 (recommended); also works with GPT-4o and Gemini 1.5 ProYou are reviewing a pull request that modifies billing calculation logic 20 minutes before standup. You need to confirm the tests cover all the critical cases — null inputs, boundary values, invalid user tiers — but writing them from scratch would take 45 minutes you do not have.Developer Tools

توليد حالات اختبار شاملة لأي دالة باستخدام هذا الـ Prompt

مشاركة:
توليد حالات اختبار شاملة لأي دالة باستخدام هذا الـ Prompt

Why this prompt matters

Functions that handle money, authentication, or user permissions fail in production in exactly the ways a rushed happy-path test misses: a null price, an unknown user tier, a floating-point rounding edge case on a $0.01 item. A typical code review surfaces 3-5 test cases; this prompt generates 15-25 in 30 seconds, including the boundary conditions responsible for most 2am incidents.

What we use it for

You are reviewing a pull request that modifies billing calculation logic 20 minutes before standup. You need to confirm the tests cover all the critical cases — null inputs, boundary values, invalid user tiers — but writing them from scratch would take 45 minutes you do not have.

Prompt

Act as a senior QA engineer and software testing expert with 10+ years of experience in unit testing, integration testing, and test-driven development across multiple programming languages.

I need comprehensive test coverage for the following function or feature:

[PASTE YOUR FUNCTION DESCRIPTION OR CODE HERE]

Language and framework: [e.g., Python with pytest, JavaScript with Jest, Java with JUnit 5, Go with the testing package, TypeScript with Vitest]

Additional context (optional):
- External dependencies: [e.g., PostgreSQL database, Stripe API, Redis cache]
- Related functions: [describe any functions this calls or that call it]
- Business rules: [any domain-specific constraints, e.g., "price can never be negative", "user tier must be one of: free, pro, enterprise"]

Generate a complete test suite covering:
1. Happy path — all expected inputs producing correct outputs, including the most common real-world inputs
2. Edge cases — boundary values, empty strings, zero, negative numbers, null/undefined/None, maximum/minimum values, single-element lists, very long strings
3. Error scenarios — invalid types, out-of-range values, missing required parameters, exception-triggering inputs
4. Integration considerations — how this function interacts with any listed external dependencies; flag what would need mocking

Constraints:
- Use descriptive test names following this pattern: test_[scenario]_[expected_result] (or the equivalent convention for [LANGUAGE])
- Keep each test focused on ONE behavior — no multi-assertion mega-tests
- Flag any test that requires mocking or a test fixture with a [REQUIRES MOCK] tag
- Do not generate tests for implementation details — test behavior, not internals

Output Format:
Organize the output into four sections:
### Happy Path Tests
### Edge Case Tests
### Error Scenario Tests
### Integration Tests

For each test, provide:
- Test name
- Input values
- Expected output or behavior
- One-sentence rationale (why this case matters)

End with a Coverage Summary listing: total test count, which scenarios are covered, and any gaps that require additional context or manual testing.

Result

Happy Path Tests

test_calculate_discount_pro_user_returns_20_percent
Input: price=100.00, user_tier="pro", coupon_code=None
Expected: 80.00
Why: Validates the standard pro-tier discount (20%) on a round number.

test_calculate_discount_enterprise_user_with_coupon_stacks_discounts
Input: price=200.00, user_tier="enterprise", coupon_code="SAVE10"
Expected: 144.00 (40% enterprise + 10% coupon applied sequentially)
Why: Confirms discount stacking for enterprise users with valid coupons.

test_calculate_discount_free_user_no_discount
Input: price=49.99, user_tier="free", coupon_code=None
Expected: 49.99
Why: Free-tier users receive no discount; price must pass through unchanged.

Edge Case Tests

test_calculate_discount_zero_price_returns_zero
Input: price=0.00, user_tier="pro", coupon_code=None
Expected: 0.00
Why: Discounting a free item must never produce a negative price.

test_calculate_discount_very_small_price_rounds_correctly
Input: price=0.01, user_tier="enterprise", coupon_code=None
Expected: 0.01 (rounds up, never $0.00)
Why: Floating-point math on tiny prices often produces $0.006.

test_calculate_discount_price_at_max_float_boundary
Input: price=999999.99, user_tier="enterprise", coupon_code="SAVE10"
Expected: 539999.99 (within float tolerance plus or minus 0.01)
Why: Large prices expose floating-point precision errors at billing scale.

test_calculate_discount_expired_coupon_silently_ignored
Input: price=100.00, user_tier="pro", coupon_code="EXPIRED2023"
Expected: 80.00 (only tier discount; expired coupon ignored gracefully)
Why: Expired coupons must fail gracefully without surfacing exceptions to users.

Error Scenario Tests

test_calculate_discount_negative_price_raises_value_error
Input: price=-50.00, user_tier="pro", coupon_code=None
Expected: raises ValueError("Price cannot be negative")
Why: Negative prices signal upstream data corruption and must be caught at the boundary.

test_calculate_discount_unknown_tier_raises_value_error
Input: price=100.00, user_tier="vip", coupon_code=None
Expected: raises ValueError("Unknown user tier: vip")
Why: Unknown tiers should fail loudly, not silently default to free and deny earned discounts.

test_calculate_discount_none_price_raises_type_error
Input: price=None, user_tier="pro", coupon_code=None
Expected: raises TypeError
Why: None prices originate from missing database fields and cause arithmetic errors downstream.

Integration Tests

test_calculate_discount_coupon_lookup_calls_db_once [REQUIRES MOCK: CouponRepository]
Input: price=100.00, user_tier="pro", coupon_code="SAVE10"
Expected: CouponRepository.find called exactly once with "SAVE10"; result=81.00
Why: Ensures no redundant database calls when validating a single coupon.

test_calculate_discount_audit_log_created_on_success [REQUIRES MOCK: AuditLogger]
Input: price=100.00, user_tier="enterprise", coupon_code=None
Expected: AuditLogger.log called once with {original: 100.00, final: 60.00, tier: "enterprise"}
Why: Discount events must be auditable for finance reconciliation.

Coverage Summary

Total tests: 12 | Happy path: 3 | Edge cases: 4 | Error scenarios: 3 | Integration: 2

Gaps requiring additional context: concurrent coupon redemption (race condition, needs load-test harness); locale-specific rounding (JPY has no cents); single-use coupon redemption limits (needs business rule clarification).

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

ما يولّده هذا الـ Prompt

أعطه وصف الدالة، ولغتك وإطار العمل، وأي سياق ذي صلة حول التبعيات أو قواعد العمل. يعيد أربع فئات اختبار منظمة: اختبارات المسار الأساسي بأكثر المدخلات شيوعاً في العالم الحقيقي، اختبارات الحالات الحدودية التي تغطي القيم الحدية والمدخلات الفارغة/null، اختبارات سيناريوهات الأخطاء للأنواع غير الصالحة والمدخلات المسببة للاستثناءات، واختبارات التكامل التي تحدد بالضبط ما يحتاج إلى محاكاة (Mock).

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

لماذا هيكلت هذا الـ Prompt بهذه الطريقة

قسم الدور (Role) يضبط الذكاء الاصطناعي كخبير ضمان جودة بدلاً من مساعد عام. هذا يحوّل المخرجات نحو اتفاقيات الاختبار المهنية — اختبارات ذات مسؤولية واحدة، تأكيدات سلوكية، وتسمية وصفية — بدلاً من الأمثلة البسيطة.

حقول السياق [بين قوسين مربعين] هي الجزء الأهم. اللغة والإطار يحددان اتفاقيات التسمية وصيغة التأكيد (Assertion). التبعيات الخارجية تحدد ما يحتاج إلى محاكاة. قواعد العمل تحدد أي حالات الخطأ حرجة وأيها تجميلية. تخطي هذه الحقول يولّد اختبارات عامة لا تطابق قاعدة الكود الفعلية لديك.

وسم [REQUIRES MOCK] يمنع أكثر وضع فشل شيوعاً في اختبارات الذكاء الاصطناعي: حذف اختبارات التكامل بصمت لأن كتابتها أصعب. الوسم يظهرها بشكل صريح لتعرف بالضبط ما هو مفقود من تشغيل الاختبار.

ملخص التغطية هو ما يميز هذا الـ Prompt عن طلب بسيط. يسرد الثغرات — الوصول المتزامن، التقريب الخاص بالمنطقة، إنفاذ القسائم مرة واحدة — وهي بالضبط الحالات التي ينسى المهندسون السؤال عنها حتى تفشل في الإنتاج.

النماذج المتوافقة

هذا الـ Prompt يعمل بأفضل أداء مع Claude Sonnet 4.6، الذي يتولى تنسيق المخرجات المنظمة بشكل موثوق ويولّد ملخصات تغطية دقيقة. GPT-4o و Gemini 1.5 Pro ينتجان أيضاً نتائج عالية الجودة. Gemini 2.5 Flash كافٍ لاختبارات دالة واحدة بسيطة. تجنب النماذج الأصغر أو المقطّرة للدوال ذات التبعيات الخارجية المتعددة — فهي تميل إلى إغفال حالات التكامل الحدودية.

متى تستخدم هذا الـ Prompt

استخدمه عند مراجعة Pull Request وتحتاج إلى تحليل سريع وشامل لثغرات الاختبار، أو عند بدء TDD على دالة جديدة وتريد هيكلاً كاملاً قبل كتابة التنفيذ، أو عند تدقيق كود قديم بتغطية اختبار ضئيلة. يغطي 80% من الحالات التي قد يفوتها مهندس متعجل في جزء صغير من الوقت.

testingdeveloper toolsai-promptsunit-testsqacoding
مشاركة: