Coding
AI unit test generation prompts
Ten copy-ready prompts to generate runnable unit tests with ChatGPT, Claude or Gemini. Covers Python, JavaScript, Java and C#, plus CI workflows and mocks.
The prompts
Copy a prompt, then replace the parts in [BRACKETS] with your own details.
You are a senior software engineer. Given the Python function below, produce a single runnable pytest module that covers happy path, boundary cases, and common error cases. Include imports, fixtures, and one-line comments per test describing intent. Ensure tests are runnable with this command: [TEST_COMMAND]. Keep total tests at most [MAX_TESTS]. If any test cannot compile or run, identify the failure and propose the minimal fix. Code: [PASTE_PYTHON_FILE] Project imports or helpers: [PASTE_HELPERS_IF_ANY]
Tip: Set MAX_TESTS to 5 to 12 for a focused, reviewable output.
You are a senior engineer. Given the JavaScript/TypeScript module below, produce a single Jest test file that includes mocks for network calls and covers error cases and boundary values. Include setup/teardown and run command [TEST_COMMAND]. Limit tests to [MAX_TESTS]. Code: [PASTE_JS_MODULE] Mocks: describe what to stub and provide example return values.
Tip: For TypeScript, include the tsconfig or type hints in the pasted code to avoid type errors.
You are an experienced engineer. Given the module below that uses ESM imports, generate a Vitest test file with explicit ESM-compatible imports, fixtures, and a command to run: [TEST_COMMAND]. Keep tests deterministic and include a note about any global setup needed. Code: [PASTE_MODULE]
Tip: Include NODE_OPTIONS or a config snippet if your project uses experimental ESM settings.
You are a senior Java engineer. For the Maven/Gradle module at path [MODULE_PATH], generate JUnit 5 test classes for the public classes listed below. Include imports, Mockito-based mocks for dependencies, and a Maven/Gradle run command [TEST_COMMAND]. Limit tests to core public methods and keep one test class per source class. Classes: [LIST_PUBLIC_CLASSES_AND_PATHS]
Tip: Provide the package names and any nonstandard test utilities to avoid missing imports.
You are an experienced C# engineer. Given the C# file below, generate an NUnit test class with setup methods and mocks using Moq. Ensure the tests run with this command: [TEST_COMMAND]. Keep total tests to [MAX_TESTS] and include a one-line comment per test explaining intent. Code: [PASTE_CSHARP_FILE]
Tip: Include the target .NET version to avoid API mismatches.
You are a senior test engineer. For the Python function below, generate Hypothesis-based property tests that cover invariants and boundary conditions. Include strategy definitions and run command [TEST_COMMAND]. Limit property tests to [MAX_TESTS]. Code: [PASTE_FUNCTION]
Tip: State any input constraints like max length or valid ranges to guide test generation.
You are an experienced engineer. For the module below that calls external services, generate unit tests that replace network calls with mocks or stubs. Provide example mock return values and ensure tests run with [TEST_COMMAND]. Keep tests deterministic and include teardown. Code: [PASTE_MODULE] External endpoints: [LIST_ENDPOINTS]
Tip: Give explicit example responses for each external endpoint to make mocks precise.
You are a senior engineer. Follow this multi-step workflow: 1) Generate tests for files at [REPO_PATH] limited to [MAX_FILES] files. 2) Output the test files only. 3) Simulate running tests with this command [TEST_COMMAND] and list failures. 4) Propose minimal code or test fixes for each failure. Code path or sample files: [REPO_SNIPPET_OR_FILE_LIST]
Tip: Use this prompt in an environment where you can actually run the tests and paste back failures for step 4.
You are a senior engineer. Given the module below, generate tests that try to reach this target: [COVERAGE_TARGET]% line coverage. Include test command [TEST_COMMAND] and recommend which branches to add if target is missed. Keep tests total under [MAX_TESTS]. Code: [PASTE_MODULE]
Tip: Set COVERAGE_TARGET to a realistic value like 70 or 80 to keep fixes manageable.
You are a senior engineer. I ran the tests and pasted the failing output below. Propose minimal changes to tests or code to fix each failure. Prioritise fixes that keep intent and avoid changing public APIs. Test run output: [PASTE_TEST_OUTPUT] Relevant code files: [PASTE_FILES]
Tip: Paste complete stack traces and exact test runner output for best fixes.
These prompts generate runnable unit tests when you provide the code, test framework, and a test run command, then run and iterate.
Who this pack is for
You are an engineer, QA, or maintainer who wants fast, reviewable unit tests. The pack covers Python pytest, JavaScript Jest and Vitest, Java JUnit, and C# NUnit. It also includes property tests, mock-heavy cases, and a CI-friendly multi-step workflow.
How to use the prompts
- Paste the exact code you want tested, including imports and small helper files.
- Specify language, test framework, and the test run command, for example "pytest -q" or "npm test".
- Limit tests per function to 5 to 15 to keep output focused and reviewable.
- Run the generated tests locally or in CI and paste failures back to the model for fixes.
Example
Ask: "Generate a pytest module for this function and keep total tests at most 10. Make tests runnable with 'pytest -q'." The model returns a single file with imports, fixtures, and one-line comments per test. Run the file and ask the model to fix any failures.
How to adapt these prompts
- To target coverage, include a numeric goal such as "aim for 80 percent line coverage" and give the test command that measures coverage.
- For mocks and external services, paste the calling code and request specific stub behavior with example return values.
- For CI automation, use the multi-step prompt that asks the model to generate tests, run them, and propose fixes. The OpenAI Cookbook shows a multi-step pattern and is a useful reference.
Common mistakes to avoid
Do not omit the test framework or the test command. Models will guess and tests may not compile. Do not send large private repositories to hosted LLMs without reviewing your data policy. Expect iterative work; studies find many generated tests need small human fixes.
Sources
See GitHub Docs "Writing tests with GitHub Copilot", RealPython "Write Unit Tests with ChatGPT", and the OpenAI Cookbook multi-step unit test notebook for examples and workflows.
Extras
You can export prompt outputs as printable files via PDF Tools. Use SEO Tools to refine prompt titles and meta text for sharing.
Frequently asked questions
Will the AI generated tests compile and pass right away?
Often they need edits. Research and community tests show a notable share of generated tests fail to compile or assert incorrectly. Run tests locally, capture output, and feed failures back to the model for focused fixes and small code changes.
What exact inputs give the best results?
Provide the exact code under test, the language, the test framework, and the test run command. Also include project imports, helper files, and any constraints such as no network calls or deterministic seeds for randomness.
How do I get the model to create mocks and fixtures?
Paste the code that calls external services and name the functions or endpoints. Provide example responses you expect and ask explicitly for mocks, fixtures, or dependency injection. That yields concrete stub code the tests can import and use.
Can I automate this in CI and what workflow works best?
Yes. Use a multi-step workflow: 1) generate tests, 2) run them in CI or locally, 3) capture failures, 4) ask the model to propose minimal fixes. The OpenAI Cookbook shows a similar loop you can adapt to your CI system.
How should I set coverage or quality targets in prompts?
Give numeric targets such as 70 or 80 percent line coverage and include the command that measures coverage. Ask the model to recommend additional branches or tests if it misses the target and to keep changes within the test files when possible.
More in Coding
Help keep every tool free
FreeToolr stays free because people help it grow. Share a tool you like, tell us what to build next, or support us on Ko-fi.