Write comprehensive unit tests covering happy paths, error cases, edge cases, and boundary conditions.
Fill in this template
The {{ }} placeholders are variables — drop in your own details, then copy the ready-to-use prompt. (Or copy the template as-is and let your AI assistant fill them in.)
0/4 filled
Preview filled prompt
You are a testing expert. Write comprehensive unit tests for the following code.
## Code Under Test
```{{LANGUAGE}}
{{CODE}}
```
## Function/Module Name
{{FUNCTION_NAME}}
## Test Framework
{{TEST_FRAMEWORK}}
## Language
{{LANGUAGE}}
## Test Generation Guidelines
### 1. Test Categories
Generate tests for each of these categories:
#### Happy Path Tests
- Test the primary use case with valid inputs
- Test with typical, expected data
- Verify the correct return value and side effects
#### Error Cases
- Test with invalid inputs (wrong type, format, range)
- Test error messages are descriptive
- Test that errors don't leave the system in a corrupt state
- Test async rejection handling
#### Edge Cases
- Empty inputs (empty string, empty array, null, undefined)
- Boundary values (0, -1, MAX_SAFE_INTEGER, Number.EPSILON)
- Unicode and special characters
- Very large inputs
- Concurrent invocations (if applicable)
#### State Transitions
- Test before and after state changes
- Test idempotency (calling twice produces same result)
- Test cleanup/teardown behavior
### 2. Test Structure
For each test:
- **Arrange**: Set up preconditions and inputs
- **Act**: Execute the function under test
- **Assert**: Verify the output and side effects
- Use descriptive test names: "should [expected behavior] when [condition]"
### 3. Mocking Strategy
- Mock external dependencies (APIs, databases, file system)
- Use the simplest mock possible (stub > spy > mock)
- Verify mock interactions only when the interaction is the behavior being tested
- Reset mocks between tests
### 4. Test Quality Checks
- Each test should test one thing
- Tests should be independent (no shared state between tests)
- Tests should be deterministic (no randomness, no time-dependence)
- Tests should be fast (mock I/O, avoid real network calls)
- Test names should document the expected behavior
## Output Format
Provide complete, runnable test file with:
1. Imports and setup
2. Organized test suites (describe blocks)
3. Individual test cases (it/test blocks)
4. Helper functions and test fixtures
5. Comments explaining non-obvious test cases
6. Coverage report expectation (which lines/branches are covered)Tags
testingunit-teststest-coveragetdd
Explore more prompts and rules
BeforeMerge offers hundreds of AI prompts, code review rules, guides, and detection patterns to help your team ship better code.