Skip to main content
Coding Prompts

Write Unit Tests

Generate comprehensive unit tests for any function or module, including edge cases.

intermediateWorks with any modelCoding
Prompt
Write unit tests for the following [LANGUAGE] code using [TEST_FRAMEWORK].

**Code to test:**
```[LANGUAGE]
[FUNCTION_OR_MODULE_CODE]
```

Write a comprehensive test suite that covers:
1. **Happy path** — typical inputs that should succeed
2. **Edge cases** — empty inputs, null/undefined, boundary values (e.g., 0, -1, max int, empty string, empty array)
3. **Error cases** — inputs that should throw or return an error, and verify the correct error type/message
4. **Parameterized test** — at least one data-driven test that runs the same assertion across multiple input/output pairs

Follow the AAA pattern (Arrange, Act, Assert) with a blank line between each section. Use descriptive test names in the format: `should [expected behavior] when [condition]`.

How to Use

Paste any function, class, or module and get a ready-to-run test file. The generated tests will cover the scenarios most likely to catch real bugs — not just the obvious happy path. Copy the output into your test file and run it immediately; tweak any fixture data that needs to match your actual data shapes.

Variables

VariableDescription
[LANGUAGE]Programming language (e.g., Python, TypeScript, Java, Go, Rust)
[TEST_FRAMEWORK]The testing framework to use (e.g., pytest, Jest, JUnit 5, testing package for Go, cargo test for Rust)
[FUNCTION_OR_MODULE_CODE]The complete code you want tested — include the function signature, body, and any types or interfaces it depends on

Tips

  • If the code has external dependencies (database calls, HTTP requests, file system access), mention them so the model can suggest the right mocking strategy (e.g., jest.mock, unittest.mock, testify/mock).
  • For TDD workflows, paste just the function signature with no body — the model will write tests first, which you can then use as a spec to drive your implementation.