Yes, AI can write unit tests, and quickly. Ask for a suite and you will get one, with good names and every test passing. The question worth asking is different: would any of these tests fail if the code were wrong?
Tests written from the code
The most common way to get AI tests is to hand over the code and ask for tests. That produces a description of what the code does, bugs included.
We planted exactly this in the practice repository for our debugging book. An assistant wrote a "due soon" feature and its tests in one go. Every test passed. A user still reported that the list showed a task they had finished and left out one due on the last day. One of the generated tests asserted that the last-day task should not appear. The test was not missing the bug. It was protecting it.
When code and tests come from the same prompt, they agree with each other, including where both are wrong.
Make every test fail once
The fix is one habit. Before you trust a new test, break the behaviour it covers and watch it go red:
- Change one thing in the code. Flip a condition, return the wrong value, delete a validation. Run the test. It should fail with a message that points at the problem. Then undo the change.
- For a bug fix, run the test against the old code. It must fail there and pass with the fix.
- For a whole suite, use mutation testing. Tools such as Stryker for JavaScript and TypeScript, mutmut for Python and PIT for Java make many small changes to your code and report the ones no test noticed. Each survivor is a missing or weak assertion.
If a test still passes when the behaviour is broken, it is not a test. Fix it or delete it.
Ask for cases, not code
Change the first prompt. Paste the requirement, leave the implementation out, and ask for a list of test cases: the input, the expected result and why it matters. Cut and add to that list yourself, then generate the tests from it.
Then read every assertion and ask what wrong behaviour would make it fail. Watch for the usual tells:
- an assertion that checks a value exists rather than what it is
- a mock of the very thing under test
- in JavaScript, a missing
await, so the check runs after the test has finished - a fixed sleep instead of waiting for something specific
So, should you use it?
Yes. AI is good at the dull parts: listing edge cases people skip, writing parametrised tables, turning manual cases into automation. It is bad at knowing what the code is supposed to do. That part stays with you.
The free Testing with AI guide turns this into a workflow, with copy-paste prompts and notes for Playwright, Cypress, pytest and API tests.