Sub topic 9
Verify generated code with tests and review
This chapter assumes you can run Python code and write basic pytest tests. It also assumes you have already written a prompt with a goal, constraints, and examples, as in the earlier chapters. Here you check what the assistant gives back.
Why generated code needs checking
An assistant produces code that looks plausible. It does not run the code, and it cannot see your whole project. The code can be wrong in ways that are easy to miss: an unhandled edge case, a function that does not exist, or a constraint that was quietly ignored. Reading the code and thinking "that looks fine" is not verification. Running it is.
Treat generated code like a pull request from a new teammate. You review it, and you test it before you merge it.
Turn your prompt into a test
The examples you put in the prompt already describe the expected behavior. Copy them into a test. The test then checks the function against your own specification instead of against the assistant's claims.
Suppose the assistant returned this function for the slugify prompt.
import re
def slugify(text: str) -> str:
text = text.strip().lower()
text = re.sub(r"[^a-z0-9]+", "-", text)
return text.strip("-")Save it as slugify_module.py. Then write a test file from the four examples in the prompt.
import pytest
from slugify_module import slugify
@pytest.mark.parametrize(
"text, expected",
[
("Hello, World!", "hello-world"),
(" Multiple spaces ", "multiple-spaces"),
("C++ & Rust", "c-rust"),
("Already-slugged", "already-slugged"),
],
)
def test_slugify(text, expected):
assert slugify(text) == expectedRun it with pytest. If all four cases pass, the function meets the examples you wrote. If one fails, paste the failure output into your next prompt. The iteration chapter covers that loop.
Add cases the prompt did not cover
Passing your own examples is the minimum. The assistant saw those examples, so it is likely to handle them. Bugs tend to sit in inputs you did not mention. Add a few of these:
- Empty string.
- A string with only symbols, such as
"!!!". - Digits, such as
"Top 10 Tips". - Very long input.
- Input that breaks an assumption. The prompt said titles are plain ASCII, so decide what should happen with
"Café". The function above turns it into"caf", which silently drops a letter.
That last case shows why this step matters. The behavior is consistent with the stated constraint, but you may not want it. Writing the test forces you to decide.
def test_empty_string():
assert slugify("") == ""
def test_only_symbols():
assert slugify("!!!") == ""
def test_digits_are_kept():
assert slugify("Top 10 Tips") == "top-10-tips"Review the code itself
Tests show behavior. Review shows whether the code fits your project. Check these points against your prompt.
| Check | Question to ask |
|---|---|
| Constraints | Does it use only the allowed libraries and the requested language version? |
| Scope | Did it change only what you asked for? |
| Names and APIs | Does every imported module and called function really exist? |
| Errors | What happens on bad input, and is that acceptable? |
| Style | Does it match your formatting and naming rules? |
Invented functions and packages are a common failure. If you see an import you do not recognize, look it up in the official documentation before you install anything.
The verification loop
Tooling that helps
Tests are not the only automated check. Run your usual linter, type checker, and formatter on the generated code. A type checker such as mypy catches wrong signatures. A linter catches unused imports and likely mistakes. These checks are cheap and need no judgment from you.
Exercises
Exercise
The assistant's slugify passes all four examples from your prompt. Why is that not enough to accept it, and what two kinds of checks should you add?
Show answer
The assistant saw those examples, so passing them shows little about inputs you did not list. Add edge-case tests (empty string, only symbols, digits, non-ASCII text) and a code review of constraints, scope, and whether the APIs used exist. Linters and type checkers are a useful third check.
Exercise
What should slugify("Café") return under the function shown, and why might that be a problem?
Show answer
It returns "caf". The regex [^a-z0-9]+ replaces the non-ASCII letter "é" with a hyphen, and the trailing hyphen is then stripped, so a letter is lost without any error. If your titles may contain accents, the prompt needs a rule for them, plus a test.