DEV Community

Cover image for How I Generate Unit Tests in 30 Seconds With One ChatGPT Prompt
Bristo Biju
Bristo Biju

Posted on

How I Generate Unit Tests in 30 Seconds With One ChatGPT Prompt

Writing unit tests is one of those tasks every developer knows they should do more of — and almost nobody enjoys doing.
It's repetitive, it's tedious, and it always feels like the least exciting part of shipping a feature. So I started using AI to do the boring parts for me.
Here's the prompt that generates a complete test suite in under a minute:
The Prompt
Write comprehensive unit tests for this function using [JEST / PYTEST / MOCHA — pick yours].

Cover all of these cases:

  • Happy path (expected inputs, expected outputs)
  • Edge cases (empty strings, zero values, max values)
  • Error conditions (what should throw or return an error)
  • Boundary values (min/max limits)
  • Null and undefined inputs

Add descriptive test names so each test reads like documentation.

Function: [PASTE YOUR FUNCTION HERE]
What You Get Back
Running this on a simple input validation function gave me 14 tests covering cases I would have never written manually — including a Unicode character edge case I genuinely hadn't thought about.
The test names alone are worth it. Instead of test('works correctly') you get test('returns false when email is missing @ symbol'). Your test suite becomes documentation.
The Secret Ingredient
The key is asking for named cases explicitly. When you say "write unit tests for this," AI writes 3-4 obvious tests and stops. When you list the specific types of tests you want, it's thorough.
Same principle applies to most AI prompts — specificity is the difference between mediocre and excellent output.
Pair It With This
After generating tests, run this follow-up prompt:
Look at these tests and identify any scenarios that are still missing.
Focus on: concurrency issues, integration points, and security-related inputs.

Tests: [PASTE GENERATED TESTS]
Nine times out of ten it finds at least one thing.
The Bigger Picture
I've built up a collection of 40 prompts like these — for code review, debugging, documentation, system design, DevOps, and AI engineering. If you want them all in one place: [b4m.gumroad.com/l/wehfa]
What's your least favorite part of writing tests? Happy path, mocks, or async testing? Let me know in the comments.

Top comments (0)