Sub topic 3
Giving the assistant the context it needs
This chapter assumes you can run pytest, read a Python function signature and docstring, and have used an AI chat or coding assistant before. It covers what to put in a prompt so the first draft is close to usable.
Why context decides quality
An assistant produces output from the text you give it. It cannot see your repository, your test layout, or your team's habits unless you include them. When something is missing, it guesses. Typical guesses are the wrong test framework, an import path that does not exist, or behavior the function never had.
Better context does not guarantee correct output. It narrows the space of plausible answers, so fewer of them are wrong.
What to include
| Context | Example | What it prevents |
|---|---|---|
| The code under test | The full function, not a summary | Invented signatures |
| The contract | Docstring or a written rule | Tests that copy bugs |
| Framework and layout | pytest, `tests/test_pricing.py` | Wrong runner or file location |
| Import statement | `from pricing import apply_discount` | Broken imports |
| Coverage expectations | Boundaries and error paths | Happy-path-only tests |
| Constraints | Do not change the function | Silent edits to your code |
The function under test
The examples in this chapter use one small function with a documented contract and two error paths.
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)A weak prompt and a strong one
A weak prompt looks like this:
Write tests for this function.The assistant has to guess the framework, the file name, the import, and which cases matter. A stronger prompt answers those questions up front:
Write pytest tests for the function below.
Context:
- Project uses pytest; tests live in tests/test_pricing.py.
- Import the function with: from pricing import apply_discount
- Use pytest.mark.parametrize for input/output tables.
Requirements:
- Cover normal values, the boundaries 0 and 100 for percent, and every ValueError path.
- Assert on expected values you derive from the docstring, not by re-implementing the function.
- Do not change the function under test.
<code of apply_discount here>The prompt has three parts, and each does a different job:
- Context states facts the assistant cannot discover: framework, file location, import line, and preferred style.
- Requirements say what "done" means. Naming the boundaries and error paths pushes the assistant past the obvious cases.
- Constraints rule out known failure modes, such as editing your code or writing a test that only repeats the implementation.
Point the assistant at the contract
The line "derive expected values from the docstring" matters most. If the assistant reads the implementation and computes expected values the same way, a bug in the code produces a test that agrees with the bug. The docstring, or a short written rule, is an independent statement of what the function should do. Tests checked against it can fail when the code is wrong.
If the function has no docstring, write one or two sentences of the intended behavior into the prompt. Do not ask the assistant to infer intent from the code alone.
Build the prompt in order
Keep the prompt as short as it can be while still containing every item. Extra background about unrelated modules adds noise. If you reuse the structure often, save it as a template and swap in the new code.
Common mistakes
- Pasting only a signature. The assistant then invents the body's behavior.
- Leaving out the import line, which produces tests that fail at collection.
- Asking for "good coverage" without naming cases. Name the boundaries and errors instead.
- Omitting "do not change the function". Some assistants fix a perceived bug rather than reporting it.
Exercise
You ask an assistant for tests and it returns unittest classes, even though your project uses pytest. Which part of the prompt was missing, and what would you add?
Show answer
The Context section was missing. Add the framework and layout facts, for example "Project uses pytest; tests live in tests/test_pricing.py", plus the import line and a request to use pytest.mark.parametrize for tables.