Generate High-Quality Unit Tests
Design and generate high-quality unit tests that protect core behavior and edge cases while remaining maintainable and robust.
SCENARIO
Act as a senior Test Engineer and Quality Architect responsible for designing high-quality unit tests for production systems. Your task is to generate unit tests that protect critical behavior, detect regressions early, and remain stable as the code evolves.
CORE PRINCIPLE:
Unit tests exist to protect behavior and logic, not to mirror implementation or inflate coverage metrics.
CONTEXT:
The code under test may be newly written, recently modified, or historically fragile. The goal is to design unit tests that provide strong behavioral guarantees with minimal brittleness.
PRIMARY OBJECTIVE:
Generate unit tests that verify correct behavior across normal flows, edge cases, and failure conditions while remaining readable, deterministic, and maintainable.
TEST DESIGN ANALYSIS:
- Identify the public contract and intended behavior of the unit
- Enumerate valid inputs, invalid inputs, and boundary conditions
- Identify side effects, state changes, and error paths
BEHAVIOR COVERAGE:
- Test normal and representative use cases
- Cover boundary values, nulls, empties, and extreme inputs
- Verify error handling, exceptions, and failure responses
MOCKING & ISOLATION STRATEGY:
- Identify external dependencies that must be mocked or faked
- Avoid over-mocking core business logic
- Prefer testing real logic over internal interactions
ASSERTION QUALITY:
- Assert outcomes and state, not internal implementation steps
- Use precise, meaningful assertions
- Ensure failures produce clear, actionable signals
WHAT NOT TO DO:
- Do NOT write tests that simply mirror the code line by line
- Do NOT over-mock and hide real integration bugs
- Do NOT assert internal variables, call counts, or ordering unless required by contract
- Do NOT generate large numbers of low-value or redundant tests
OUTPUT EXPECTATIONS:
- A focused set of unit tests covering core behavior and edge cases
- Explanation of what each test protects and why it matters
- Notes on any risky or ambiguous behavior discovered during test design
QUALITY CHECK:
- Ensure tests fail before fixes and pass after
- Verify determinism and absence of flakiness
- Confirm tests protect behavior, not implementation
FINAL CHECK:
- If this logic changes incorrectly, will these tests catch it immediately?
- Are the most important invariants and contracts protected?
INPUT:
Code or function under test: [Insert Code] Expected behavior: [Describe intent] Dependencies: [Describe external calls or state]