💻 SaaS & Dev
Unit Test Suite Generator (TDD-First)
Design comprehensive unit test suites using TDD principles. Includes test cases for happy paths, edge cases, and error handling.
#testing#tdd#unit-tests#quality#qa
You are a QA engineer specializing in test-driven development and comprehensive test coverage.
Design a unit test suite for:
**Function/Module:** {{FUNCTION_NAME}}
**Language:** {{LANGUAGE}}
**Framework:** {{FRAMEWORK}} (e.g., Jest, Pytest, JUnit, RSpec)
**Purpose:** {{PURPOSE}}
**Input Types:** {{INPUT_TYPES}}
**Expected Outputs:** {{OUTPUTS}}
**Deliverable: Production-Ready Test Suite**
1. **Test Planning**
**Happy Path (Normal case):**
{{DESCRIPTION}} → Should output {{EXPECTED_OUTPUT}}
**Edge Cases (Boundary conditions):**
- Edge case 1: {{EDGE_CASE_1}} → Expected: {{EXPECTED}}
- Edge case 2: {{EDGE_CASE_2}} → Expected: {{EXPECTED}}
- Edge case 3: {{EDGE_CASE_3}} → Expected: {{EXPECTED}}
**Error Cases (Invalid input handling):**
- Error case 1: {{ERROR_CASE_1}} → Should throw: {{ERROR_TYPE}}
- Error case 2: {{ERROR_CASE_2}} → Should throw: {{ERROR_TYPE}}
- Error case 3: {{ERROR_CASE_3}} → Should throw: {{ERROR_TYPE}}
**Performance Cases (If applicable):**
- Large input (1000 items): Should complete in < {{TIMEOUT}}ms
- Empty input: Should handle gracefully
2. **Test Suite Structure**
```{{LANGUAGE}}
describe('{{FUNCTION_NAME}}', () => {
describe('Happy Path', () => {
test('should {{DESCRIPTION}}', () => {
// ARRANGE: Set up test data
const input = {{INPUT}};
const expected = {{EXPECTED}};
// ACT: Call the function
const result = {{FUNCTION}}(input);
// ASSERT: Verify the result
expect(result).toBe(expected);
});
});
describe('Edge Cases', () => {
test('should handle {{EDGE_CASE_1}}', () => {
const input = {{INPUT}};
const expected = {{EXPECTED}};
const result = {{FUNCTION}}(input);
expect(result).toBe(expected);
});
// More edge case tests...
});
describe('Error Handling', () => {
test('should throw {{ERROR_TYPE}} when {{ERROR_CONDITION}}', () => {
const input = {{INVALID_INPUT}};
expect(() => {{FUNCTION}}(input)).toThrow({{ERROR_TYPE}});
});
// More error handling tests...
});
});
```
3. **Individual Test Cases (Detailed)**
**Test 1: Basic Functionality**
- **Test Name:** should {{OUTCOME}}
- **Input:** {{INPUT_VALUE}}
- **Expected Output:** {{EXPECTED_VALUE}}
- **Why this test matters:** Validates the primary use case
**Test 2: Edge Case - Boundary Value**
- **Test Name:** should handle {{EDGE_DESCRIPTION}}
- **Input:** {{BOUNDARY_VALUE}}
- **Expected Output:** {{EXPECTED}}
- **Why this test matters:** Ensures behavior at limits
**Test 3: Edge Case - Null/Undefined**
- **Test Name:** should handle null input gracefully
- **Input:** null or undefined
- **Expected Output:** {{EXPECTED}} or throw {{ERROR}}
- **Why this test matters:** Prevents null pointer exceptions
**Test 4-N:** [Continue for all critical cases]
4. **Test Coverage Analysis**
```
COVERAGE BREAKDOWN:
Code Paths Tested: {{PATHS}}/{{TOTAL}} ({{PERCENT}}%)
Conditional Branches: {{BRANCHES}}/{{TOTAL}} ({{PERCENT}}%)
Exception Paths: {{EXCEPTIONS}}/{{TOTAL}} ({{PERCENT}}%)
TARGET: 80%+ coverage minimum (90%+ ideal)
NOT COVERED (intentional):
- Path 1: {{WHY_NOT_TESTED}}
- Path 2: {{WHY_NOT_TESTED}}
```
5. **Mock/Stub Strategy** (If testing dependencies)
**If function calls external API:**
```
test('should call API with correct parameters', () => {
const mockAPI = jest.fn().mockResolvedValue({{MOCK_RESPONSE}});
const result = {{FUNCTION}}(mockAPI, {{PARAMS}});
expect(mockAPI).toHaveBeenCalledWith({{EXPECTED_PARAMS}});
});
```
**If function uses database:**
```
test('should query database correctly', () => {
const mockDB = { query: jest.fn().mockResolvedValue({{DATA}}) };
const result = {{FUNCTION}}(mockDB, {{PARAMS}});
expect(mockDB.query).toHaveBeenCalledWith({{SQL}});
});
```
6. **Performance Test Example**
```
test('should process 1000 items in < 100ms', () => {
const largeInput = Array.from({length: 1000}, (_, i) => i);
const startTime = performance.now();
const result = {{FUNCTION}}(largeInput);
const endTime = performance.now();
expect(endTime - startTime).toBeLessThan(100);
expect(result.length).toBe(1000);
});
```
7. **Running the Test Suite**
**Command:**
```
npm test {{FUNCTION_NAME}}.test.js
```
**Expected output:**
```
PASS src/{{FUNCTION_NAME}}.test.js
{{FUNCTION_NAME}}
Happy Path
✓ should {{TEST_1}} (5ms)
✓ should {{TEST_2}} (2ms)
Edge Cases
✓ should {{TEST_3}} (3ms)
✓ should {{TEST_4}} (4ms)
Error Handling
✓ should {{TEST_5}} (2ms)
Test Suites: 1 passed, 1 total
Tests: {{TOTAL}} passed, {{TOTAL}} total
Snapshots: 0 total
Time: {{TIME}}s
Coverage: {{COVERAGE}}%
```
8. **Test Maintenance Notes**
- **When to add more tests:** If you find a bug, write a test for it first
- **When to remove tests:** Never remove passing tests (mark as skip, not delete)
- **How to update tests:** If function signature changes, update all related tests
- **Continuous integration:** Run tests on every commit (pre-commit hook)
9. **Common Testing Pitfalls** (Avoid these)
✗ Tests that are too brittle (fail if implementation detail changes)
✗ Tests that test too much (should test one thing per test)
✗ Tests that depend on execution order
✗ Tests that leave side effects (create temp files, databases)
✓ Instead: Make tests independent, focused, and maintainable