Coding
testing
unit-tests
tdd

Unit Test Generator

Generate comprehensive unit tests for your code with edge cases, mocking strategies, and clear assertions.

The Prompt

(2 messages)
System
You are an expert test engineer. Generate thorough unit tests for the provided code.

Guidelines:
- Use the specified test framework and follow its conventions
- Test the happy path first, then edge cases and error conditions
- Each test should verify one behavior and have a descriptive name
- Use arrange-act-assert structure
- Mock external dependencies (APIs, databases, file system) — never make real calls
- Include tests for boundary values, null/undefined inputs, and error handling
- Keep tests independent — no test should depend on another test's state
User
Generate unit tests for this {{language}} code using {{framework}}:

```{{language}}
{{code}}
```

Test focus: {{focus}}

Variables

Fill in these inputs to customize your output:

{{language}}

Example: typescript

{{framework}}

Example: vitest

{{code}}

Example: export function parsePrice(input: string): number { const cleaned = input.replace(/[^0-9.]/g, '') if (!cleaned || cleaned === '.') throw new Error('Invalid price') const price = parseFloat(cleaned) if (isNaN(price) || price < 0) throw new Error('Invalid price') return Math.round(price * 100) / 100 }

{{focus}}

Example: edge cases and error handling

Example Output

Here's what this prompt generates with the sample inputs:

Sample outputclaude-sonnet-4-5
import { describe, it, expect } from 'vitest'
import { parsePrice } from './price'

describe('parsePrice', () => {
  // Happy path
  it('parses a simple decimal price', () => {
    expect(parsePrice('19.99')).toBe(19.99)
  })

  it('parses price with dollar sign', () => {
    expect(parsePrice('$49.99')).toBe(49.99)
  })

  it('parses price with commas', () => {
    expect(parsePrice('1,299.50')).toBe(1299.50)
  })

  it('rounds to two decimal places', () => {
    expect(parsePrice('9.999')).toBe(10.00)
  })

  // Edge cases
  it('parses zero', () => {
    expect(parsePrice('0')).toBe(0)
  })

  it('parses price with leading/trailing spaces', () => {
    expect(parsePrice('  24.99  ')).toBe(24.99)
  })

  // Error cases
  it('throws on empty string', () => {
    expect(() => parsePrice('')).toThrow('Invalid price')
  })

  it('throws on non-numeric input', () => {
    expect(() => parsePrice('abc')).toThrow('Invalid price')
  })

  it('throws on just a dot', () => {
    expect(() => parsePrice('.')).toThrow('Invalid price')
  })

  it('throws on just a dollar sign', () => {
    expect(() => parsePrice('$')).toThrow('Invalid price')
  })
})

Configuration

Optimized settings included when you add this prompt. You can adjust them later.

Provider

ANTHROPIC

Model

claude-sonnet-4-5

Temperature

0.2

Ready to use this prompt?

Add it to your workspace, customize the inputs, and generate your own results.

Use this prompt — free

More prompts