Read a small text fixture with an explicit encoding
Also published in our Blogger archive.
Read a small text fixture with an explicit encoding
A test fixture should state its expected text encoding when it includes non-ASCII content. Here, the setup writes known UTF-8 bytes for naïve into a file in TemporaryDirectory. Path.read_text(encoding="utf-8") then decodes those bytes into a Python string. The expected string includes its newline, making the fixture’s full content part of the assertion instead of treating trailing whitespace as incidental.
The first assertion confirms the decoded text. The second separately confirms the bytes initially placed in the fixture, which distinguishes byte input from Unicode decoding. For presentation only, rstrip() removes the newline before printing a stable fixture=naïve line. The fixture itself is not changed by that display operation, and the generated directory name never enters stdout.
Path.read_text and Path.write_bytes require Python 3.5 or later; TemporaryDirectory requires Python 3.2 or later. An explicit UTF-8 decoder can raise UnicodeDecodeError if input bytes are not valid UTF-8. It does not prove that arbitrary external files use UTF-8, and the assertions cover only this deliberately created fixture. The Path.read_text reference documents that the method opens and closes the file; TemporaryDirectory describes its context-managed cleanup.
AI-assistance disclosure: AI helped draft this educational article.
from pathlib import Path
from tempfile import TemporaryDirectory
with TemporaryDirectory() as directory:
fixture = Path(directory) / "greeting.txt"
fixture.write_bytes("naïve\n".encode("utf-8"))
greeting = fixture.read_text(encoding="utf-8")
assert greeting == "naïve\n"
assert fixture.read_bytes() == b"na\xc3\xafve\n"
print(f"fixture={greeting.rstrip()}")
fixture=naïve