Testing a Real Project · practice
Repair with the Test Still Watching
The red suite is already specific: one collected tmp_path test expected visible é, while the real UTF-8 file contained the JSON escape \\u00e9. The test did not fail because of discovery, an import, or an old repository artifact.
Open the production file only after fixing those facts:
nano catalog.py
In write_catalog_json, change only the JSON option from ensure_ascii=True to ensure_ascii=False. That restores the inherited Chapter 7 through 9 contract: non-ASCII text stays visible in the UTF-8 artifact.
Rerun every test:
python -m pytest -q
Now collection is nonzero, every test passes, and pytest exits 0. Do not weaken the expectation to accept both escaped and visible text. A regression test is a safety net only while it continues to state the intended behavior.
Add one more tmp_path check for the existing failure boundary. Add run_catalog to the import from catalog, then use this small shape:
def test_missing_input_preserves_existing_output(tmp_path, capsys):
missing = tmp_path / "missing.csv"
output = tmp_path / "existing.json"
output.write_text("keep me\n", encoding="utf-8")
result = run_catalog(missing, output, 0)
assert result == 1
assert output.read_text(encoding="utf-8") == "keep me\n"
assert capsys.readouterr().err.startswith(f"Could not read {missing}:")
Like tmp_path, capsys is supplied by pytest when you name it as a test capsys.readouterr() returns the captured output so far: .out holds stdout and .err holds stderr. Calling it also clears that captured buffer. Here the test inspects .err to check the expected diagnostic. Both file paths stay under tmp_path. The sentinel assertion checks the contract that matters: a read failure happens before the writer touches existing output.
Now prove the net still catches. Put ensure_ascii=True back, run the suite, and watch the same test go red again. Then set it to False and watch it go green.
That red-green-red-green cycle is the entire
The new Unicode test fails on "\\u00e9" not in raw. Which repair preserves a useful regression test?
Task
After reading the Lesson 4 failure, restore ensure_ascii=False in write_catalog_json and rerun the complete retained suite green. Add a tmp_path read-failure test whose pre-existing output sentinel remains exactly unchanged. The grader reintroduces the Unicode regression outside your workspace and requires the relevant tmp_path subset to fail.