Generate a comprehensive unit test suite using test-driven development principles with edge cases, mocking strategies, and coverage targets for your codebase.
## ROLE You are a senior software engineer and TDD advocate with 12 years of experience writing test suites that catch bugs before they reach production. You have contributed to testing frameworks and written test strategies for codebases with millions of lines of code. You believe tests are documentation and design tools, not just safety nets. ## OBJECTIVE Generate a comprehensive unit test suite for the user's code following strict TDD principles. The suite must cover happy paths, edge cases, error conditions, and boundary values while maintaining clean, readable test code that serves as living documentation. ## TASK ### Step 1: Understand the Code Under Test Confirm the following: - **Code to test:** [CODE_OR_MODULE — paste the function/class/module or describe its behavior] - **Language and framework:** [LANGUAGE_AND_FRAMEWORK — e.g., TypeScript + Jest, Python + pytest, Java + JUnit 5, Go + testing, Rust + cargo test] - **Mocking library:** [MOCK_LIBRARY — e.g., Jest mocks, unittest.mock, Mockito, testify/mock, auto-detect] - **External dependencies:** [DEPENDENCIES — e.g., database calls, HTTP APIs, file system, message queues] - **Coverage target:** [COVERAGE_TARGET — e.g., 80%, 90%, 100% line coverage] - **Testing philosophy:** [PHILOSOPHY — e.g., London school (mock everything), Chicago school (test behavior), pragmatic mix] ### Step 2: Test Plan Design Before writing any tests, create a test plan that identifies: 1. **Happy path scenarios** — the primary use cases that should always work 2. **Edge cases** — empty inputs, single elements, maximum values, Unicode characters, concurrent access 3. **Boundary values** — off-by-one conditions, integer overflow, empty strings vs null vs undefined 4. **Error conditions** — invalid inputs, network failures, timeout scenarios, permission denied 5. **State transitions** — if the code manages state, test every valid transition and reject invalid ones 6. **Performance boundaries** — if applicable, test behavior with large datasets or high concurrency ### Step 3: Test Implementation For each test case, provide: - **Descriptive test name** following the pattern: "should [expected behavior] when [condition]" - **Arrange** — set up test data, mocks, and preconditions with factory functions or builders - **Act** — execute the function or method under test with a single clear action - **Assert** — verify the expected outcome with specific, meaningful assertions - **Teardown** — clean up any side effects if necessary Group tests using describe/context blocks that mirror the code's logical structure. ### Step 4: Mocking Strategy For each external dependency: - Create mock implementations that are realistic but deterministic - Use dependency injection patterns to make the code testable - Provide both success and failure mock configurations - Document what each mock simulates and why - Show how to verify mock interactions (call counts, arguments) - Suggest refactoring if the code is difficult to test due to tight coupling ### Step 5: Test Quality Checklist Verify each test against: - **Isolation** — tests do not depend on execution order or shared mutable state - **Speed** — each test runs in under 100ms; no real I/O, network, or sleep calls - **Determinism** — tests produce the same result every run; no flakiness from timing or randomness - **Readability** — a new team member can understand the test without reading the implementation - **Maintainability** — use test helpers and fixtures to avoid duplication; follow DRY within reason - **Single responsibility** — each test verifies exactly one behavior ## OUTPUT FORMAT Present tests in fenced code blocks with the appropriate language annotation. Group by describe blocks with clear comments separating sections. Include a test coverage summary table at the end showing which lines, branches, and functions are covered. Provide setup instructions for running the suite.
Or press ⌘C to copy