Sub topic 1
What an assistant does and does not guarantee
An AI coding assistant can draft unit tests and documentation in seconds. It cannot promise that either one is correct. This chapter explains why, so you can decide what to trust and what to check. The rest of the tutorial builds the checking habits on this foundation.
Prerequisites
This chapter assumes you have:
- Basic Python, including functions, exceptions, and type hints.
- Experience running tests with pytest.
- Basic Git and code review practice, such as reading a diff and commenting on a pull request.
- Some experience with an AI chat or coding assistant.
You do not need to know how language models are trained. The chapter covers only the parts of their behavior that affect test and documentation work.
How an assistant produces code and text
A coding assistant is built on a large language model. During training, the model learned statistical patterns from a very large body of text and code. When you send a prompt, it predicts a likely continuation one token (a small chunk of text) at a time. The result is the sequence it judged most plausible given your prompt and its training.
Three consequences follow for your work:
- Plausible is not the same as correct. The model optimizes for text that looks like a good answer. Code that looks like a good test can still assert the wrong thing.
- It sees only what is in the prompt. The model has no built-in view of your repository. It can use files you include or that a tool fetches for it. Anything else it guesses from naming and convention.
- Output varies. The same prompt can produce different tests on different runs. Some of that variation is harmless style. Some of it is a different interpretation of the behavior.
Many assistants can also run commands, such as executing pytest, when you give them that ability. A run changes what you know. A passing run proves the tests passed on that code at that moment. It says nothing about whether the tests check the right behavior.
The running example
The tutorial uses one small function with a documented contract:
def apply_discount(price: float, percent: float) -> float:
"""Return price reduced by percent, rounded to two decimals.
Raises ValueError if price is negative or percent is outside 0-100.
"""
if price < 0:
raise ValueError("price must be non-negative")
if not 0 <= percent <= 100:
raise ValueError("percent must be between 0 and 100")
return round(price * (1 - percent / 100), 2)An assistant will usually write correct tests for this function's obvious paths. The two gotchas below show where a draft can still go wrong.
Where drafts go wrong
Run this file with pytest. Both tests pass, and each one records a fact that a plausible draft could get wrong.
def apply_discount(price: float, percent: float) -> float:
"""Return price reduced by percent, rounded to two decimals.
Raises ValueError if price is negative or percent is outside 0-100.
"""
if price < 0:
raise ValueError("price must be non-negative")
if not 0 <= percent <= 100:
raise ValueError("percent must be between 0 and 100")
return round(price * (1 - percent / 100), 2)
def test_rounding_follows_float_representation():
# A draft may expect 2.68 by assuming "round half up".
# 2.675 is stored as slightly less than 2.675, so Python gives 2.67.
assert apply_discount(2.675, 0) == 2.67
def test_nan_price_is_not_rejected():
# The docstring says negative prices raise. NaN is not negative,
# so the function lets it through.
result = apply_discount(float("nan"), 10)
assert result != result # NaN is the only value not equal to itselfThe first test shows a numeric claim that sounds right and is wrong. Rounding in Python works on the binary float, not on the decimal you typed. The second shows a gap that no one stated. The docstring promises an error for negative prices. It says nothing about NaN, and an assistant reading only the docstring will not look for that gap.
The assistant did not misbehave in either case. It worked from the same incomplete information a hurried reviewer might use. The remedy is to verify, not to hope for a better model.
What you can reasonably expect
The following are reliable enough to build a workflow on, if you still review the results:
- Structure and boilerplate. Imports, fixtures, parametrize tables, and docstring skeletons are usually well formed.
- Coverage of stated behavior. If the docstring lists two error paths, the draft will usually test both.
- Speed through the routine parts. Writing twenty similar input and output rows by hand is slow. Reviewing them is faster.
- Consistent style, when you show it an existing test file to imitate.
- Suggestions you did not think of, such as extra boundary values. Treat these as prompts for your own judgment.
What you cannot expect
| Claim | Why it is not guaranteed |
|---|---|
| The tests are correct | Expected values come from the model's guess about behavior, not from running your specification. |
| The tests are complete | The model covers what the prompt and code suggest. Unstated cases, such as NaN above, are easy to miss. |
| The tests would catch a bug | A test that passes today may also pass on broken code if it asserts too little or restates the implementation. |
| The docs match the code | Docstrings are generated from your code and the model's expectations. Names and comments can mislead it. |
| The docs stay accurate | Documentation drifts when the code changes unless something checks it. |
| The output is repeatable | Another run can give different tests or wording. |
| The output is safe to share | Code you paste into a hosted assistant leaves your machine under that service's terms. |
Two of these rows deserve more detail.
Tests that pass for the wrong reason
If you ask an assistant to write tests for code that already exists, it can see the implementation. The easiest test to write is one that computes the expected value the same way the code does. Such a test passes by construction and can never fail when the logic is wrong. This is a tautological test, and it is the most common weak draft. Later chapters cover prompt wording that prevents it and review habits that find it.
Documentation with confident errors
A docstring that says "raises TypeError if price is a string" reads well. If the function never raises that, the sentence is false. Readers trust documentation more than they trust code comments, so a confident error does more harm than a gap. You check docs by running them or comparing them to the code, not by reading them for tone.
The division of labor
The workflow in this tutorial treats the assistant as a fast drafter and you as the person who answers for the result.
The checks in the diamond are yours. They include reading the assertions, running the tests, mutating the code to see if a test fails, and running the doctests. The assistant can help execute them, but it cannot be the only judge of its own output. A model asked "is this correct?" about its own draft tends to agree with itself.
Practical rules from this model
- Write down the contract before you ask. Whether it is a docstring, a ticket, or a comment, the contract is the source of truth. Expected values should come from it, not from the implementation.
- Ask for tests that can fail. Say explicitly that expected values must be derived by hand, not computed with the code under test.
- Run everything. Run the generated tests against the real code. Then break the code on purpose and confirm that a test fails.
- Check claims in docs. Run examples with
python -m doctest, and compare each stated error and default against the code. - Keep the diff small. Review generated code the way you review a colleague's pull request. Large unreviewed drafts are where weak tests hide.
- Decide what is allowed to leave your machine. Check your organization's policy before pasting proprietary code or data into a hosted assistant.
Where this chapter fits
The next chapters turn these limits into practice. You will define what makes a test worth keeping, write prompts that supply enough context, generate and review tests, and then do the same for documentation. The risks chapter returns to privacy and maintenance in more depth. The aim throughout is a repeatable workflow in which generated output is always a draft until you have checked it.
Exercise
An assistant generates ten passing tests for a function you just wrote. A teammate says, "They pass, so the function is correct." Give two reasons that conclusion is not justified, and name one check you would run next.
Show answer
Passing tests show only that the tests and the code agree. First, the expected values may have been derived from the implementation, so the tests would pass even if the logic were wrong. Second, the tests may miss cases the prompt never mentioned, such as unusual inputs like NaN. One check: temporarily change the code, for example flip a comparison or alter the rounding, and confirm at least one test fails. A test suite that stays green after you break the code is not protecting anything. Reading the assertions against the written contract is another acceptable check.
Exercise
A generated docstring says "Raises TypeError if price is not a number." Using the apply_discount code from this chapter, is that sentence accurate? How would you find out without trusting the docstring?
Show answer
It is not accurate as a promise. The function has no explicit type check. Passing a string for price would fail at price < 0 with a TypeError from the comparison, but that is incidental behavior, not a documented guarantee. Passing a string for percent would also fail in the comparison with a TypeError. To find out, call the function with those inputs in a REPL or write a test with pytest.raises(TypeError). Then either remove the sentence or add an explicit check so the claim is deliberate.