Sub topic 1
What an Assistant Does and Does Not Guarantee
Prerequisites
This chapter assumes basic Python, familiarity with pytest, basic Git and code review, and experience using an AI chat or coding assistant.
What an Assistant Does and Does Not Guarantee
An AI assistant predicts useful code from the context you provide. It can draft tests quickly, suggest missing cases, and turn code into documentation. It does not prove that the output is correct.
A generated test may pass while checking the wrong behavior. It may repeat the implementation instead of checking the public contract. It may also miss important inputs, error paths, security rules, or integration behavior.
Treat generated output as a draft. You remain responsible for understanding the requirement, reviewing the code, and running the checks.
What Makes a Test Worth Keeping
A useful test describes an expected behavior that matters to users or callers. It has a clear input, a derived expected result, and an assertion that would fail if the behavior changed.
Good tests cover normal values, boundaries, and documented failures. They avoid tautological assertions, which are assertions that merely repeat the implementation. They also isolate external systems when the unit under test does not need a real network or database.
Giving the Assistant the Context It Needs
State the framework, file locations, import path, behavior, and constraints. Include the real function and its docstring. Ask for specific cases, including every documented error path. Tell the assistant not to modify the function and not to re-implement it in the test.
For example, ask for pytest tests for apply_discount, require cases for 0 and 100 percent, and require pytest.raises(ValueError) for invalid input. This context narrows plausible but incorrect output.
Generating Your First Unit Tests
For the discount function, a focused prompt can produce a test module like this:
import pytest
from pricing import apply_discount
@pytest.mark.parametrize(
"price, percent, expected",
[(100, 10, 90.0), (19.99, 0, 19.99), (50, 100, 0.0)],
)
def test_apply_discount(price, percent, expected):
assert apply_discount(price, percent) == expected
@pytest.mark.parametrize("price, percent", [(-1, 10), (10, -5), (10, 101)])
def test_rejects_invalid_input(price, percent):
with pytest.raises(ValueError):
apply_discount(price, percent)Review the expected values independently before committing the file.
Exercise
Why should you review a generated test that passes?
Show answer
Because it can assert the wrong behavior, omit important cases, or accidentally mirror the implementation.