Skip to main content
Ryan Mitchell·

100% branch coverage on a gnarly pricing function, every edge case I forgot was in there

Generates complete unit test suites with edge cases, mocks, parameterized tests, and coverage for any function or class.

Comprehensive Unit Test Generator

You are a test engineer writing comprehensive unit tests. Generate a complete test suite that covers all paths. ## Code Under Test ```{{programming_language}} {{code_to_test}} ``` ## Test Requirements - Language: {{programming_language}} - Test framework: {{test_framework}} (jest, pytest, vitest, go test, etc.) - Mock library: {{mock_library}} - Target coverage: {{coverage_target}} (e.g., 90% line, 100% branch) ## Test Suite Requirements Generate tests covering: 1. **Happy Path** - All typical use cases with realistic inputs 2. **Edge Cases** - Empty inputs, boundary values, maximum sizes, null/undefined 3. **Error Cases** - All exception paths, invalid inputs, failure modes 4. **Parameterized Tests** - Use test.each/data-driven tests for similar cases 5. **Mock Verification** - Verify all external calls made correctly 6. **State Tests** - Verify state changes (if applicable) 7. **Async Tests** - Proper async/await handling if code is async 8. **Setup/Teardown** - Proper beforeEach/afterEach for shared state ## Output Rules - Use Arrange-Act-Assert pattern with clear comments - Name tests descriptively: `should [expected behavior] when [condition]` - Group related tests in describe blocks - Include 100% branch coverage - every if/else, catch, early return - Mock ALL external dependencies (DB, HTTP, file system, clock) - Include one test that verifies the complete happy path end-to-end Output only test code, no explanations. Make tests self-contained and runnable.

Ergebnisse

```typescript import { describe, it, expect, vi, beforeEach } from "vitest"; import { applyDiscount } from "../pricing"; describe("applyDiscount", () => { beforeEach(() => vi.useRealTimers()); it("should apply percentage discount when code is valid", () => { // Arrange const cart = { subtotal: 10000, code: "SAVE20" }; // Act const result = applyDiscount(cart); // Assert expect(result.total).toBe(8000); }); it.each([ ["empty cart", { subtotal: 0, code: "SAVE20" }, 0], ["no code", { subtotal: 5000, code: "" }, 5000], ["max discount cap", { subtotal: 100000, code: "SAVE90" }, 20000], ])("should handle %s", (_label, cart, expected) => { expect(applyDiscount(cart).total).toBe(expected); }); it("should throw when subtotal is negative", () => { expect(() => applyDiscount({ subtotal: -1, code: "SAVE20" })).toThrow( "invalid subtotal", ); }); it("should reject an expired code", () => { vi.setSystemTime(new Date("2027-01-01")); expect(() => applyDiscount({ subtotal: 5000, code: "EXPIRED" })).toThrow( /expired/, ); }); }); ```

Modell: Claude Sonnet 4

21 Likes7 SavesScore: 13

2 Kommentare

Priya Nair·

Sending this code generation one to my whole team.

Jonas Weber·

Okay this code generation output just saved me an afternoon.