Sub topic 11
Choose between a fake and a recording
This chapter assumes you can write pytest tests, read basic Python classes, and pass a dependency as an argument. It also assumes you have met the ChatClient seam, the scripted FakeClient, and the RecordingClient from earlier chapters. Here you decide which one a given test should use.
Two tools, two jobs
A scripted fake answers the question "what does my code do when the model says X?" You write X by hand. You control it fully, including failures that a real model rarely produces.
A recording answers a different question: "does my code handle what the model really said?" The reply came from a real call, so it has the odd formatting, extra fields, and wording that a hand-written string tends to miss.
Neither one proves the live API still behaves the same way. That is the job of a smoke test, covered later.
Compare them
| Question | Scripted fake | Recording |
|---|---|---|
| Who wrote the reply? | You | The real model |
| Can it produce errors and malformed output on demand? | Yes, directly | Only if you captured one |
| Setup cost | A few lines in the test | One live run, plus an API key once |
| Does a prompt change break it? | No, unless you assert on the prompt | Yes, the hash no longer matches |
| Realism of the reply | As realistic as you make it | Real at the time of recording |
| Review cost | Reply is visible in the test | Reply lives in a separate file |
| Can it go stale? | Only if your assumptions about the API are wrong | Yes, as the model and API change |
Decision rules
Work through these questions in order for each test.
- Is the behavior about your code's reaction to a specific reply or failure, such as a retry, a timeout, or a missing JSON key? Use a fake. You need an exact, repeatable input.
- Is the behavior about parsing a realistic, messy reply, such as a long answer with markdown or nested fields? Use a recording.
- Do you need to assert on the exact prompt your code sent? A fake keeps the prompts in a list. A recording also checks the prompt, but only indirectly, because a changed prompt fails the lookup.
- Will the reply change often as you edit the prompt? Prefer a fake, because every prompt edit forces a re-recording.
Worked example
Take extract_total. Three tests cover it, and each uses the tool that fits.
import json
import pytest
def test_retry_after_bad_json():
# Behavior: reaction to a failure. A fake is the right tool.
client = FakeClient(["not json", '{"total": 10}'])
assert extract_total_with_retry(client, "x") == 10.0
def test_timeout_is_not_swallowed():
# Behavior: an API error must reach the caller.
client = FakeClient([TimeoutError("slow")])
with pytest.raises(TimeoutError):
extract_total(client, "x")
def test_real_invoice_reply_parses(tmp_path):
# Behavior: a real reply is parsed. A recording is the right tool.
# The file was recorded once with a real client and committed.
client = RecordingClient("tests/recordings/invoice.json")
assert extract_total(client, REAL_INVOICE_TEXT) == 1287.40The first two tests need failures that a live model almost never gives you. The third needs the shape of a real reply, such as a reply wrapped in extra text, so a recording earns its place.
Common mistakes
- Using recordings for every test. The suite then breaks on each prompt edit, and people start re-recording without reading the diff.
- Using only fakes. The hand-written replies drift away from what the model really sends, and the parser meets surprises in production.
- Recording a failure you could script. A fake is smaller and easier to review.
- Asserting on a whole prompt string in many tests. One wording change then breaks them all. Check for the key piece, as the earlier example does with the invoice text.
A practical split is many fakes for logic, a handful of recordings for realistic parsing, and one small live smoke test.
Exercise
You are testing code that retries when the model returns a reply missing a required field. A teammate suggests recording a real call that happened to omit the field. Which tool would you use, and why?
Show answer
Use a scripted fake. The test needs a specific malformed reply on demand, followed by a good one. A fake produces that exactly and keeps the test readable, while a recording depends on capturing a rare real failure and breaks whenever the prompt changes.
Exercise
Your code parses long, formatted summaries from the model, and you want confidence that it handles real output. The prompt is stable. Which tool fits, and what risk remains?
Show answer
Use a recording of a real reply. The risk is staleness: the model or API may change later, and replay will not notice. A small live smoke test covers that.