Unit Test Plan and Test Code for an Existing Function

Builds a behavior-based test matrix for a function or module and then generates test code in your framework. Covers edge cases, failures, and mocking boundaries, with coverage gaps stated honestly.

You get: Behavior summary, test matrix table and test code in one fenced block per file

More like this

The prompt

unit-test-plan-and-test-code-for-an-existing-function.md
Role: You are a test engineer who writes maintainable {test_framework} tests for {language} code.Context: The user needs to decide whether this unit is safe to refactor or release. Tests must verify behavior, not implementation details. You cannot run the tests, so do not claim they pass.
Task: Produce a test plan and test code for the supplied code.

Inputs:
- Code under test: {code_under_test}
- Dependencies and interfaces to mock: {dependencies}
- Business rules or expected behavior: {requirements}
- Existing tests and conventions: {existing_tests}
- Known bugs or past incidents: {known_issues}
- Coverage or risk priority: {priority}

Workflow:
1. List missing information that affects test design. Ask at most 3 questions, or proceed with stated assumptions.
2. State assumptions separately.
3. Identify the public behaviors, inputs, outputs, side effects, and error paths.
4. Build a test matrix covering happy paths, boundaries, invalid input, null or empty values, error handling, concurrency or time if relevant, and regression cases from the known issues.
5. Mark which cases need mocks, and explain why each boundary is mocked.
6. Write the test code following the conventions in {existing_tests}.
7. Identify what remains untested and why.

Constraints:
- One behavior per test. Use descriptive names in the form should_<result>_when_<condition>, adapted to the project convention.
- No tests that depend on the system clock, network, random values, or execution order unless controlled.
- Do not change the code under test. If you find a defect, report it separately with a failing test.
- Do not assert on private internals.

Output format:
1. Behavior summary.
2. Assumptions and open questions.
3. Test matrix table: ID, Behavior, Input, Expected result, Type (happy, edge, error, regression), Mocks needed.
4. Test code in one fenced block per file.
5. Suspected defects found, with the failing test ID.
6. Coverage gaps and risks.
7. Next actions: how to run the tests and what to review.

Acceptance criteria:
- Every row in the matrix has a matching test in the code.
- The tests would fail if the key behavior were broken.
- The tests are deterministic and follow the existing conventions.
- The gaps are stated, not hidden.

Quality checks:
- Check each test for a meaningful assertion.
- Check that the mocks match the real interface signatures supplied.
- Check that the names describe behavior.
- Check that no test duplicates another.

Tool notes: In Cursor or GitHub Copilot Chat, reference the source file and a sibling test file so the output matches the project style. In ChatGPT or Claude, paste one existing test as a style example.

Example output

AI-written example
# tests/test_calculate_order_total.py
#
# BEHAVIOR SUMMARY
#   calculate_order_total(items, region, promo_code=None, *, tax_client, promo_repo, now)
#   -> Decimal. Subtotal = sum(unit_price * qty); percent promo applied before tax;
#   tax rate from tax_client.get_rate(region); result rounded ROUND_HALF_UP to 2 places.
#
# ASSUMPTIONS (confirm before relying on these tests)
#   A1. A promo is valid through its expires_at instant (inclusive).
#   A2. Empty items
Generated from this prompt by AI. Your results will vary.

Tips and common edits

Works best on one function or class at a time. Include one existing test so the output follows your conventions. Run the generated tests yourself and treat any failures as information about either the tests or a real defect.

Questions about this prompt

More in Coding

Characterization Tests Before a Refactor

Writes tests that lock in a module's current behavior, including edge cases and quirks, so you can refactor safely.

Pull Request Review with Merge Recommendation

Reviews a diff against your standards and returns a merge, merge-with-changes, or do-not-merge recommendation. Findings are ranked by severity, with evidence, suggested fixes, and open questions.

Codebase Tour for a Specific Task

Explores a repo through the lens of your task and explains the architecture, key files and data flow, then says where to start.

Failing Test First Bug Fix

Reproduces a bug with a failing test, finds the root cause, applies the smallest fix and shows the test passing.

Root Cause Analysis from Stack Trace and Logs

Turns an error report, stack trace, and logs into ranked root-cause hypotheses. Includes the cheapest checks to confirm each one, a fix proposal, and a regression test plan.

Architecture Decision Record for a Technology Choice

Compares realistic options against your constraints and produces a decision record. The record has a weighted comparison, risks, a reversal plan, and a recommendation the team can approve or challenge.

Edge-Case Test Suite for One Function in Your Existing Test Framework

Writes tests for a single function that cover normal inputs, boundaries, bad input and error paths, in the test framework your project already uses.

Explain This Error with Ranked Causes

Explains an error and stack trace in plain words, ranks the three most likely causes and gives a confirming check for each.

Regex with Match and Non-Match Test Strings

Writes a regex for your pattern, explains each part and provides 8 strings that should match and 8 that should not.

SQL Query from Schema and Question

Writes a SQL query that answers your question from your schema, explains each clause and suggests indexes worth adding.

Cursor .mdc Rules to Portable AGENTS.md Converter

Converts your pasted Cursor .mdc rules into a single AGENTS.md that Codex, Copilot and Gemini CLI can read, plus a table of what does not carry over.

20-Case Agent Eval Set with Pass/Fail Rubric for Normal, Edge and Unsafe Requests

Builds 20 test cases for your agent's task, with expected behavior, failure signals and a pass/fail rubric covering normal, edge and unsafe requests.