DeepSeek Unit Test Generator Prompt

You are a senior software engineer specialising in TDD with expertise in tests that catch production bugs.

Category
💻 Coding
Difficulty
Intermediate
Models
3
Last Updated
2026-06-28
💻 Coding Intermediate unit tests testing jest pytest
Works with
📋 Prompt
You are a senior software engineer specialising in TDD with expertise in tests that catch production bugs.

Function/module: [function to test]
Language: [programming language]
Framework: [testing framework — Jest/pytest/JUnit/Go test]
Edge cases: [edge cases to cover]

Paste the code:
[code]

Task: Write comprehensive unit tests:

1. HAPPY PATH TESTS: Normal use cases that should succeed
2. EDGE CASE TESTS: Empty inputs, null/undefined, boundary values, type mismatches
3. ERROR CASE TESTS: What should throw or return error
4. MOCKING: External dependencies that need mocking
5. COVERAGE REPORT: Estimated % coverage with explanation
6. GAPS: What you couldn't test and why

Format: Complete test file, ready to run. Inline comments explaining non-obvious test logic.
// calculateMonthlyPayment.test.js
import { calculateMonthlyPayment } from './mortgageCalculator';

describe('calculateMonthlyPayment', () => {

// Happy path
it('calculates correct payment for standard mortgage', () => {
// £200K loan, 4% annual, 25 years
expect(calculateMonthlyPayment(200000, 4, 25)).toBeCloseTo(1055.68, 2);
});

it('handles 0% interest (interest-free loan = principal ÷ months)', () => {
expect(calculateMonthlyPayment(120000, 0, 10)).toBeCloseTo(1000, 2);
});

// Edge cases
it('throws RangeError for zero or negative principal', () => {
expect(() => calculateMonthlyPayment(0, 4, 25)).toThrow(RangeError);
expect(() => calculateMonthlyPayment(-5000, 4, 25)).toThrow(RangeError);
});

it('throws TypeError for string inputs', () => {
expect(() => calculateMonthlyPayment('200000', 4, 25)).toThrow(TypeError);
});

it('handles extreme interest rate without NaN or Infinity', () => {
const result = calculateMonthlyPayment(10000, 50, 1);
expect(result).not.toBeNaN();
expect(result).not.toBe(Infinity);
expect(result).toBeGreaterThan(10000);
});

// Precision
it('rounds to 2 decimal places (pence)', () => {
const result = calculateMonthlyPayment(150000, 3.75, 20);
const decimals = result.toString().split('.')[1]?.length ?? 0;
expect(decimals).toBeLessThanOrEqual(2);
});
});

// Coverage: ~85% — gaps: concurrent calls (pure function, N/A)
🏆
Best model for this prompt
DeepSeek
DeepSeek V3 / R1
💡 Pro Tips
Write test names as full sentences — 'returns null when user not found' beats 'test null'
Tests must be isolated — run alone, in any order, same result every time
Mock Date.now() in time-dependent functions — otherwise tests break at midnight
Test behaviour, not implementation — after refactoring, tests should pass unchanged
⚠️ Common Mistakes
Testing private methods instead of public behaviour — breaks on every refactor
Tests with no assertions — always pass, catch nothing
Overly complex setup — if you need 50 lines of setup, the function under test does too much
Not testing error paths — most production bugs live in the code that handles errors
❓ FAQ 🔗 Related Prompts