تولید سوئیتهای تست کامل از روی توضیح یک تابع

Why this prompt matters
Skipping edge cases is where most production bugs live. A function that works for normal inputs but silently returns wrong results for empty strings, zero values, or concurrent writes will pass a cursory manual test and still break in production. Developers without a structured checklist miss 30-50% of edge cases on average — this prompt enforces completeness every time.
What we use it for
You've just written a pricing function, an authentication handler, or a data-processing pipeline — and you need to ship it tomorrow. You paste the function into this prompt and get back a full test suite with happy paths, boundary values, and error scenarios already written, so you can move to code review instead of spending 90 minutes hand-crafting tests.
Prompt
Act as a senior QA engineer and software testing expert with 10+ years of experience writing test suites for production-grade code. I have a function or feature that needs comprehensive test coverage. Here is the description: [PASTE YOUR FUNCTION/FEATURE DESCRIPTION, CODE SNIPPET, OR SPECIFICATION HERE] Technology stack: [YOUR LANGUAGE AND FRAMEWORK — e.g., Python/pytest, JavaScript/Jest, TypeScript/Vitest, Java/JUnit, Go/testing] Testing framework: [YOUR TESTING FRAMEWORK] Code maturity: [NEW CODE / EXISTING CODE WITH KNOWN BUGS / REFACTORING IN PROGRESS] Your task: 1. Generate a complete set of unit tests covering: - Happy path (standard inputs with expected outputs) - Edge cases (boundary values, empty inputs, maximum inputs, special characters) - Error scenarios (invalid inputs, type mismatches, null/undefined, out-of-range values) - State mutations (if the function has side effects, test them explicitly) 2. List integration test outlines (what you'd test at the integration layer, without writing full code) 3. Identify any untestable scenarios and explain why (dependency on external systems, timing issues, etc.) 4. Add a one-line comment above each test explaining what it's asserting and why it matters Constraints: - Tests must be independent — no test should rely on another's side effects - Use descriptive test names in the format: [method]_[scenario]_[expected outcome] - Do NOT mock anything unless you explain exactly why the mock is necessary - Prioritize tests by impact: mark each as HIGH / MEDIUM / LOW risk Output format: ## Unit Tests (with priority labels) [Full test code in [YOUR TESTING FRAMEWORK]] ## Integration Test Outlines [Bullet list of integration scenarios to cover] ## Untestable Scenarios [List with explanations] ## Coverage Summary [Estimated line/branch coverage and what's missing]
Result
Here is the full test suite for calculate_discount(price, coupon_code, user_tier) using Python/pytest:
import pytest
from pricing import calculate_discount
# [HIGH] Standard discount applied correctly for a valid coupon and premium user
def test_calculate_discount_valid_coupon_premium_user_applies_discount():
assert calculate_discount(100.0, "SAVE20", "premium") == 80.0
# [HIGH] Free users receive no additional tier discount beyond coupon
def test_calculate_discount_valid_coupon_free_user_no_tier_bonus():
assert calculate_discount(100.0, "SAVE20", "free") == 80.0
# [HIGH] Invalid coupon code returns full price without discount
def test_calculate_discount_invalid_coupon_returns_full_price():
assert calculate_discount(100.0, "FAKECODE", "premium") == 100.0
# [HIGH] Price of zero returns zero regardless of coupon or tier
def test_calculate_discount_zero_price_returns_zero():
assert calculate_discount(0.0, "SAVE20", "premium") == 0.0
# [MEDIUM] Negative price raises ValueError — cannot discount negative amounts
def test_calculate_discount_negative_price_raises_value_error():
with pytest.raises(ValueError, match="Price must be non-negative"):
calculate_discount(-50.0, "SAVE20", "premium")
# [MEDIUM] Empty coupon string treated as no coupon — full price returned
def test_calculate_discount_empty_coupon_no_discount():
assert calculate_discount(100.0, "", "premium") == 100.0
# [MEDIUM] None coupon treated as no coupon — does not raise, returns full price
def test_calculate_discount_none_coupon_no_discount():
assert calculate_discount(100.0, None, "premium") == 100.0
# [MEDIUM] Unknown user_tier defaults to free-tier behavior, no tier bonus
def test_calculate_discount_unknown_tier_defaults_to_free():
assert calculate_discount(100.0, "SAVE20", "vip") == 80.0
# [LOW] Very large price value does not cause overflow or floating-point error
def test_calculate_discount_very_large_price_precision():
result = calculate_discount(1_000_000.0, "SAVE20", "premium")
assert abs(result - 800_000.0) < 0.01
# [LOW] Coupon code is case-insensitive — "save20" works like "SAVE20"
def test_calculate_discount_coupon_case_insensitive():
assert calculate_discount(100.0, "save20", "premium") == 80.0
Integration Test Outlines
- Apply coupon from the database — verify the discount matches the stored percentage for that coupon record
- Apply discount to a cart with multiple line items — verify totals are recalculated correctly
- Expired coupon in the database — verify
calculate_discountreturns full price and logs the expiry event - Concurrent requests with the same coupon (single-use coupon) — verify only one request succeeds
Untestable Scenarios
- Real-time coupon expiry race condition — coupon expires between the validity check and the discount application; requires a database transaction test outside unit scope
- External pricing API availability — if
calculate_discountcalls a third-party pricing service, that call cannot be reliably tested without a staging environment
Coverage Summary
Estimated line coverage: 94%. Estimated branch coverage: 87%. Missing: the internal retry logic on database timeout (lines 47-52) — requires a mock of the DB connection pool, which should be covered in an integration test.
نوشتن تست جزو کارهایی است که توسعهدهندگان میدانند باید کامل انجام دهند ولی به ندرت این کار را میکنند. نه از روی تنبلی، بلکه به خاطر فشار زمانی و هزینه ذهنی شمارش تکتک edge cases از صفر. این Prompt بار آن شمارش را به AI میسپارد تا شما بتوانید به جای تولید، روی بررسی خروجی تمرکز کنید.
چه چیزی این Prompt را متفاوت میکند
بیشتر Promptهای «برای این کد تست بنویس» فقط چند assertion مسیر خوشبینانه برمیگردانند و کار را تمام شده میدانند. اما این Prompt یک قرارداد متفاوت را تحمیل میکند: AI باید قبل از پایان کار، چهار دسته مجزا را پوشش دهد — مسیر خوشبینانه، حالات مرزی، سناریوهای خطا و تغییرات حالت. این ساختار ۳۰ تا ۵۰ درصد از edge cases را که توسعهدهندگان در شرایط فشار زمانی معمولاً از قلم میاندازند، میگیرد.
قالب خروجی هم مهم است. درخواست نامهای توصیفی برای تستها به الگوی [method]_[scenario]_[expected outcome] تضمین میکند که سوئیت تست ماهها بعد، وقتی نویسنده اصلی دیگر نیست، خوانا باشد. همچنین درخواست یک کامنت توضیحی روی هر تست باعث میشود یک توسعهدهنده جدید بفهمد هر assertion از چه چیزی محافظت میکند — نه فقط اینکه چه کاری انجام میدهد.
چگونه از آن استفاده کنیم
تابع، توضیح ساده به انگلیسی یا حتی یک سند مشخصات را در فیلد داخل براکت قرار دهید. زبان و فریمورک خود را مشخص کنید — AI کد قابل اجرا تولید میکند، نه pseudocode. پرچم بلوغ کد را روی EXISTING CODE WITH KNOWN BUGS تنظیم کنید، و AI همچنین مسیرهای مستعد نقص را بر اساس ساختار کد شناسایی میکند.
محدودیت Mock عمدی است. Mock کردن همه چیز سادهترین راه برای ساختن یک سوئیت تست است که پاس میشود اما اعتماد ایجاد نمیکند. با الزام AI به توجیه هر moci که معرفی میکند، سوئیت تست را بر اساس رفتار واقعی نگه میدارید.
برچسبهای اولویت
هر تست یک برچسب خطر HIGH / MEDIUM / LOW دریافت میکند. این به شما اجازه میدهد در زمان انتشار محدود، تستهای LOW را رد کنید و در اسپرینت بعدی به آنها برگردید — بدون اینکه ردپای موارد رد شده را گم کنید. همچنین بازبینی کد را سریعتر میکند: بازبینها میتوانند در یک نگاه ببینند آیا مسیرهای با خطر HIGH پوشش داده شدهاند یا نه.
با چه مدلهایی بهترین عملکرد را دارد
Claude Sonnet 4.6 یا GPT-4o. برای توابع پیچیده با شرطهای تو در تو، Claude معمولاً پوشش شاخهای کاملتری تولید میکند. برای کدهای جدید در فریمورکهای تازه، GPT-4o به همان اندازه تواناست. هر دو مدل این Prompt را به طور قابل اعتماد مدیریت میکنند — برای این کار از مدل کوچکتر استفاده نکنید، زیرا اغلب سناریوهای خطا را حذف میکنند.