Unit Test Generator

Generates thorough unit test suites covering happy paths, edge cases, error handling, and boundary conditions for any function.

Coding & Development · intermediate · GitHub Copilot

Prompt template

Generate comprehensive unit tests for the following {{language}} function.

```{{language}}
{{function_code}}
```

Testing framework: {{test_framework}}
Dependencies to mock: {{dependencies}}

Please create tests covering:
1. Happy path - Expected inputs produce correct outputs
2. Edge cases - Empty inputs, boundary values, null/undefined
3. Error handling - Invalid inputs, exceptions, error messages
4. Type validation - Wrong types, missing required fields
5. Integration points - Mocked external dependencies

Each test should have a descriptive name following the pattern:
'should {{expected_behavior}} when {{condition}}'

Example output

```javascript
describe('calculateDiscount', () => {
  it('should return 10% discount when order total exceeds $100', () => {
    expect(calculateDiscount(150)).toBe(15);
  });

  it('should return 0 discount when order total is below threshold', () => {
    expect(calculateDiscount(50)).toBe(0);
  });

  it('should throw Error when amount is negative', () => {
    expect(() => calculateDiscount(-10)).toThrow('Amount must be positive');
  });
});
```

Use cases

  • Developers adding test coverage to existing code
  • Teams implementing test-driven development
  • CI/CD pipelines requiring minimum test coverage

Tags

testing, unit-tests, TDD, code-quality