Prompt

Unit Test Generator

testingunit teststdd

Unit Test Generator: Comprehensive Tests in Seconds

Writing unit tests is essential but time-consuming. This tool generates thorough test suites covering happy paths, edge cases, and error conditions for any function.

Full Prompt
Write a thorough unit test suite that validates correct behavior, guards against edge cases, and verifies error handling, producing tests that serve as living documentation of the function's contract.

TESTING STRATEGY:

1. Understand the Function Contract
   - Analyze the function signature, parameters, return type, and side effects
   - Identify the implicit and explicit preconditions and postconditions
   - Determine what constitutes a "unit" here (pure function, method with dependencies, async operation, etc.)

2. Happy Path Tests
   - Test the primary use case with typical, realistic input
   - Test with multiple valid input variations to cover different code branches
   - Verify both the return value AND any side effects (state changes, calls to dependencies)
   - Test with minimum valid input and maximum valid input

3. Edge Case Tests
   - Boundary values: zero, one, empty collections, single-element collections, max values
   - Null, undefined, or missing optional parameters
   - Unicode, special characters, and extremely long strings if strings are accepted
   - Concurrent or repeated calls if the function has state
   - Type coercion boundaries if the language allows it
   - Empty objects, nested nulls, or partial data structures

4. Error Handling Tests
   - Invalid input types and out-of-range values
   - Missing required parameters
   - Dependency failures (network errors, database timeouts, file not found)
   - Verify the correct error type/message is thrown or returned
   - Ensure errors do not leave the system in an inconsistent state

5. Test Quality Principles
   - Each test should have a single, clear assertion focus (one logical concept per test)
   - Test names should describe the scenario and expected outcome in plain language
   - Use the Arrange-Act-Assert pattern consistently
   - Mock external dependencies; never test the framework or standard library
   - Tests must be independent and runnable in any order

OUTPUT CONSTRAINTS:
- Produce complete, runnable test code (not pseudocode)
- Include all necessary imports, setup, teardown, and mock configuration
- Use descriptive test names following the pattern: "should [expected behavior] when [condition]"
- Add a brief comment above each test group explaining what aspect is being tested
- If the function has dependencies that need mocking, set up mocks with realistic behavior
- Target at minimum: 3 happy path tests, 3 edge case tests, 2 error handling tests

FORMAT:
[Complete test file with imports, setup, and organized test groups]

// Group: Happy Path
// Group: Edge Cases
// Group: Error Handling

---

MY INFO:

Testing Framework: (required - e.g., Jest, pytest, JUnit, Go testing, Mocha)

Function to Test: (required - include the full function code)

Programming Language: (optional - can usually be inferred from the function)

Dependencies to Mock: (optional - external services, databases, APIs the function calls)

Specific Scenarios to Cover: (optional - any particular cases you want tested)

Test Coverage Types

Happy Path: Normal, expected inputs Edge Cases: Boundary values, empty inputs, extremes Error Handling: Invalid inputs, exceptions Integration Points: Mock external dependencies

Supported Frameworks

  • Jest (JavaScript/TypeScript)
  • pytest (Python)
  • JUnit (Java)
  • Go testing
  • RSpec (Ruby)
  • And more...

Generated Test Structure

  1. Setup/Arrange: Prepare test data and mocks
  2. Execute/Act: Call the function under test
  3. Assert: Verify expected outcomes
  4. Cleanup: Reset state if needed

Best Practices

  • Test one thing per test
  • Use descriptive test names
  • Keep tests independent
  • Mock external dependencies

Increase your test coverage without spending hours writing tests manually.