You inherit a Python project with no tests. You ask an AI tool for a pytest suite. It returns a file that passes on the first run. That feels like progress, but it may be a suite that never fails, even when the code is wrong. Real Python's guide on generating Python tests with AI calls this out directly: a one-line prompt often gives you tests that pass without catching real bugs.
In this article (5)Expand βCollapse β
What happened
Real Python published a guide on building a pytest suite with AI that you can trust. The guide warns that a suite which passes against buggy code is worse than no suite, because it tells you everything is fine when it is not. It lists three qualities a reliable suite needs: accurate assertions, complete coverage of boundaries and error paths, and robustness against behavior changes. It then describes a workflow: set testing conventions, map the project, extract intended behavior, generate tests to a coverage goal, and confirm the suite catches real bugs.
Why it matters to you
If you are a junior developer, the first thing you reach for when you inherit untested code is an AI tool. You paste a function, ask for tests, and commit whatever comes back. The problem is that AI models are good at writing tests that match the code in front of them. If the code has a bug, the test will often assert the buggy behavior as correct.
Here is a small example. Suppose you have this function:
# discount.py
def apply_discount(price, percent):
return price - (price * percent / 100)You ask an AI for tests. It might write:
# test_discount.py
from discount import apply_discount
def test_apply_discount():
assert apply_discount(100, 10) == 90.0That test passes. But the function has a bug: it does not validate percent. If someone passes 150, the function returns a negative price. The AI test never checks that. The suite is green, and you feel safe. You are not.
This is the core failure mode. AI-generated tests tend to cover the happy path and mirror the current implementation. They rarely probe boundaries or error paths unless you ask. Real Python's guide calls this out as the difference between tests that are accurate and tests that just pass.
The habit that changes: do not trust a passing suite until you have seen it fail. A test that has never failed is a test you have not verified.
What to do about it
Use a loop: generate, break, verify. Do this on one small function before you scale to a whole project.
Step 1: Generate tests with a clear prompt. Do not ask for "tests for this function." Ask for tests that cover boundaries, error paths, and edge cases. For the discount function, a better prompt is: "Write pytest tests for apply_discount. Cover normal values, zero percent, 100 percent, negative percent, percent above 100, and non-numeric input. Assert what the function should do, not what it currently does."
The AI might return:
import pytest
from discount import apply_discount
def test_normal_discount():
assert apply_discount(100, 10) == 90.0
def test_zero_percent():
assert apply_discount(100, 0) == 100.0
def test_full_discount():
assert apply_discount(100, 100) == 0.0
def test_negative_percent_raises():
with pytest.raises(ValueError):
apply_discount(100, -10)
def test_percent_above_100_raises():
with pytest.raises(ValueError):
apply_discount(100, 150)Run it. The first three tests pass. The last two fail, because the function does not raise ValueError. That is good. The tests are doing their job.
Step 2: Deliberately break the code. If all tests pass, you have not verified anything. Change the function on purpose. For example, change price - (price * percent / 100) to price + (price * percent / 100). Run the suite. If the tests still pass, they are not checking the arithmetic. Rewrite them until they fail.
This is a manual version of mutation testing. Mutation testing means changing the code in small ways and checking whether tests catch the change. You do not need a tool for this. A text editor and one deliberate bug are enough.
Step 3: Fix the code and re-run. Once the tests fail on the broken version, fix the function. Add validation for percent:
def apply_discount(price, percent):
if percent < 0 or percent > 100:
raise ValueError("percent must be between 0 and 100")
return price - (price * percent / 100)Run the suite again. All tests should pass. Now you have a test that has been shown to fail when the code is wrong. That is a test you can trust.
The rule for when to trust AI-written tests: trust a test only after you have seen it fail on a known bug and pass on correct code. If you have not seen it fail, treat it as unverified.
This loop is slow at first. But it scales. Once you have a few trusted tests, you can use them as examples when prompting for more. You can also run the loop on a whole module by breaking one function at a time.
A coverage goal alone is not enough. Coverage tells you which lines ran, not whether the assertions would catch a bug. Real Python's guide makes this point: a suite can be complete by coverage and still be inaccurate. Pair coverage with deliberate breakage.
If you are already using AI coding agents, this loop fits naturally. The same discipline applies to any AI-generated code: verify before you trust. You can read more about that in using AI coding agents without letting your skills rust. And if you want to build a habit of checking outputs, the eval harness approach in build a tiny eval harness before you touch that prompt again is a similar idea applied to prompts.
The limit: this loop does not prove your tests are complete. It proves they catch the bugs you thought to introduce. You still need to think about boundaries and error paths. AI can help you list them, but you have to check the list.
Further reading
- One Process, One Server, SQLite: Debuggable Stack Lessons
- Build a Minimal LLM Eval Harness in Python This Weekend