5 ChatGPT Prompts for Unit Tests

The annoying part of writing unit tests is not the happy path. It is the second hour, when you are staring at a function wondering which edge case you already covered and which one will break in production. These five ChatGPT prompts for unit tests make you list the cases first, then write them one at a time.

What’s on this page

  • Five prompts that cover case design, writing, and review.
  • Each one works on a single function or module, not a whole codebase at once.
  • Written with the failure paths included from the start.
  • The [variables] to replace are marked in every prompt.

The prompts

1. List the cases before writing tests

Here is my function [function code] written in [language]. List the cases a test suite should cover: normal input, boundary values, invalid input, empty input, and anything specific to this logic. For each case give the input, the expected result, and the reason it matters. Do not write any test code yet.

What it does: Settles what to test before syntax gets in the way.

How to use: Paste the function, replace [language], and delete cases that cannot happen in your system.

2. Write tests for one function

Write unit tests for [function name] in [language] using [test framework]. Cover only these cases: [case list]. Use descriptive test names, one assertion per test where it makes sense, and no mocks unless the function needs an external dependency.

What it does: Produces tests in your framework that match the case list you already agreed on.

How to use: Replace [function name], [language], [test framework], and paste the case list from the first prompt.

3. Cover the failure paths

For this function [function code], write tests for the situations where it should fail or return an error: bad types, missing values, out-of-range numbers, and exceptions from anything it calls. Show the expected behaviour for each one, then write the tests in [test framework].

What it does: Tests the paths that actually break in production, not just the ones that pass.

How to use: Paste the function, replace [test framework], and confirm your error handling really behaves as the tests assume.

4. Write a test for a bug you just fixed

I just fixed this bug: [bug description]. The fix was in this code: [function code]. Write a test that fails on the old behaviour and passes on the fix, name it after the bug, and add a one-line comment explaining what went wrong.

What it does: Locks in the fix so the same bug cannot come back unnoticed.

How to use: Replace [bug description], paste the fixed code, and confirm the test fails if you revert the change.

5. Review a test file for gaps

Here is my test file [paste tests] and the code it covers [paste code]. List the cases that are missing, name the tests that assert almost nothing, and point out any test that would still pass if the function returned the wrong result. Suggest the fix for each issue.

What it does: Finds the tests that look reassuring but catch nothing.

How to use: Paste both files; treat weak assertions as the priority over adding new tests.

How to adapt these prompts

  • Name your test framework every time; the output differs a lot between them.
  • Paste the surrounding types or interfaces so the tests match your real signatures.
  • Ask for fewer, sharper tests when the generated suite starts repeating itself.

FAQ

How many unit tests does one function need?

As many as it takes to cover the cases you actually care about, and no more. If two tests would fail for the same reason, they are one test.

Should I test private functions?

Test them through the public function that uses them. If a private function is hard to reach, that is often a sign the code wants a different shape.

What if the generated tests pass immediately?

Check that they fail when you break the code on purpose. A test that passes against a wrong result is worse than no test.

Keep going

Every prompt on GuPrompt is free to copy — no signup, no paywall.

Leave a Comment