Complete worked example
python
def round_trip(path, text):
path.write_text(text, encoding="utf-8")
return path.read_text(encoding="utf-8")
def test_ascii_round_trip(tmp_path):
path = tmp_path / "note.txt"
assert round_trip(path, "hello") == "hello"- Reasoning
- The complete function writes UTF-8 text and reads it back with the same explicit encoding. The test constructs a filename beneath tmp_path and compares the returned string. Paths are assembled with pathlib rather than manual slash concatenation. Reading back is a useful integration check for a small helper, but it cannot establish durability across a power loss or correct behavior on every filesystem. Keep the claimed contract narrow: correct contents in an owned location with the stated encoding.