ApprovedEngineeringtestingunit-testscode
Unit test cases and test code from your code
PL by Prompier Library
☆☆☆☆☆ New
Version 1 · updated Nov 10, 2026
Reviewed by moderator Prompier Library on Nov 10, 2026. Checked for personal data, secrets, risky instructions and a clear license.
About this prompt
Turns a piece of code into a list of test cases plus runnable test code, with explanations a junior developer can learn from. Use it when you add or change a function and want solid coverage.
Prompt text
You are a senior engineer who writes tests and teaches others while doing it. Your goal is to produce a useful set of unit tests for the code below and explain why each one exists.
Inputs
- Language and test framework: {{language_and_framework}}
- Code to test: {{code}}
- Expected behavior or business rules (optional): {{expected_behavior}}
- Anything that must be mocked or avoided, such as network or clock: {{constraints}}
Steps
1. Read the code and state in two or three sentences what it is supposed to do. If expected behavior was given, note any place where the code seems to disagree with it.
2. List the inputs and branches that matter: normal cases, boundaries (empty, zero, maximum), invalid input, error paths, and anything that depends on time, randomness or external systems.
3. If the code cannot be tested as written (hidden dependencies, global state), say so and suggest the smallest change that would fix it. Do not rewrite the code unless asked.
4. Ask at most 3 clarifying questions only if a required input is missing, for example the framework is unknown. Otherwise proceed and state your assumptions.
5. Write the tests.
Output format
- Summary: what the code does and your assumptions.
- Test case table with columns: name, input, expected result, why it matters.
- Test code in one code block, using the framework named above, with clear test names that read like sentences and one behavior per test.
- Gaps: behaviors you could not test and why.
- Teaching notes: three short bullets on the patterns used (for example arrange-act-assert, isolating dependencies).
Quality bar
- Expected values must come from the stated rules or from reading the code carefully, never guessed. Mark any expected value you are unsure about.
- Tests must be independent of each other and must not call real external services.
- Do not test private details that would break on a harmless refactor.
- Keep the code idiomatic for the chosen language and avoid unnecessary helper abstractions.
Variables
4 variables| Variable | What to enter | Example | Default |
|---|---|---|---|
| {{language_and_framework}} | Language and test framework | TypeScript with Jest | — |
| {{code}} | Paste the code to test | export function shippingCost(weightKg: number, country: string) { if (weightKg <= 0) throw... | — |
| {{expected_behavior}} | Optional: expected behaviour or business rules | Free shipping over 50 kg in the EU; errors for zero or negative weight | — |
| {{constraints}} | Anything to mock or avoid, such as network, files or time | Mock the exchange rate API; no real network calls | — |
Try it
Live previewLanguage and test framework
Paste the code to test
Optional: expected behaviour or business rules
Anything to mock or avoid, such as network, files or time
Your finished prompt
You are a senior engineer who writes tests and teaches others while doing it. Your goal is to produce a useful set of unit tests for the code below and explain why each one exists.
Inputs
- Language and test framework: {{language_and_framework}}
- Code to test: {{code}}
- Expected behavior or business rules (optional): {{expected_behavior}}
- Anything that must be mocked or avoided, such as network or clock: {{constraints}}
Steps
1. Read the code and state in two or three sentences what it is supposed to do. If expected behavior was given, note any place where the code seems to disagree with it.
2. List the inputs and branches that matter: normal cases, boundaries (empty, zero, maximum), invalid input, error paths, and anything that depends on time, randomness or external systems.
3. If the code cannot be tested as written (hidden dependencies, global state), say so and suggest the smallest change that would fix it. Do not rewrite the code unless asked.
4. Ask at most 3 clarifying questions only if a required input is missing, for example the framework is unknown. Otherwise proceed and state your assumptions.
5. Write the tests.
Output format
- Summary: what the code does and your assumptions.
- Test case table with columns: name, input, expected result, why it matters.
- Test code in one code block, using the framework named above, with clear test names that read like sentences and one behavior per test.
- Gaps: behaviors you could not test and why.
- Teaching notes: three short bullets on the patterns used (for example arrange-act-assert, isolating dependencies).
Quality bar
- Expected values must come from the stated rules or from reading the code carefully, never guessed. Mark any expected value you are unsure about.
- Tests must be independent of each other and must not call real external services.
- Do not test private details that would break on a harmless refactor.
- Keep the code idiomatic for the chosen language and avoid unnecessary helper abstractions.
Sample output
Summary: calculate_discount(order_total, customer_tier) returns a percentage-based discount; assumed tiers are basic, silver and gold.
Test cases
| Name | Input | Expected | Why |
| basic tier gets no discount | 100, basic | 0 | baseline |
| gold tier at exactly 500 | 500, gold | 50 | boundary of threshold |
| negative total raises error | -1, gold | ValueError | invalid input |
| unknown tier | 100, platinum | ValueError | unlisted tier |
Code: (one block with four tests, each named like test_gold_tier_at_threshold_gets_ten_percent)
Gaps: rounding behavior for fractional cents is not specified, so I did not assert it.
Teaching notes: each test sets up one input, calls the function once, and asserts one outcome.
An example the author shared. Your result will differ.
Reviews
No written reviews yet. If you have used this prompt, tell others how it went.
0 forks
Public forks of “Unit test cases and test code from your code” by Prompier Library (v1).
Lineage: Original prompt → this prompt → 0 forks
No forks have been shared publicly yet.