Add tmp_path to a pytest test function's parameters. Pytest supplies a temporary directory as a Path object. Put the test's input and output files under that directory so each test can create and inspect its own files without using someone's Downloads folder or a checked-in report.
Let the test choose where the function writes
A file-producing function is easier to test when the caller can choose where it writes. A routine that always updates a file under the home directory forces tests to change global state or risk affecting a real file. Accept a path parameter so the command can use a normal location and a test can use its temporary path.
The example below defines one supported behavior. Exclusive creation means an existing summary should raise an error and preserve the old text.
import pytest
def write_summary(destination, count):
with destination.open("x", encoding="utf-8") as output:
output.write(f"Count: {count}\n")
def test_existing_summary_is_preserved(tmp_path):
destination = tmp_path / "summary.txt"
destination.write_text("Previous result\n", encoding="utf-8")
with pytest.raises(FileExistsError):
write_summary(destination, 5)
assert destination.read_text(encoding="utf-8") == "Previous result\n"
A real project would normally import its function from the module where it lives. The example keeps both pieces together so the intended behavior is visible before you try a similar test.
Verify the result at the correct layer
If the function promises an exact text format, read back the text and assert the expected value. If it promises exact bytes, compare bytes so newline conversions do not hide a change. If the contract is a valid structured document, parse it and compare the relevant fields. Choose which detail the test must protect before choosing the assertion.
Test one ordinary result and one meaningful failure. A permission error may depend on system setup, so do not assume identical filesystem permissions across Windows and Linux. A pre-existing destination with exclusive creation is a dependable case because it isolates the behavior from the user's permission configuration.
Keep fixtures small enough to understand
For three lines of input, create the data directly in the test. For a larger durable sample, store a synthetic fixture with the tests and copy it into the temporary directory when mutation is required. Do not share private exports or downloaded reports as convenient test data.
Allow pytest to manage its temporary directory retention. A failed test can display the path it used, letting you inspect the file on the same machine. Your test should not expect that path to be fixed or preserved forever.
Verify a normal write before the collision case
For a normal write, call the example function with a fresh destination under tmp_path, read the file, and compare it to the exact expected line. The collision case verifies a different outcome. A test that only checks file existence would pass even if the content were wrong. A test that calls the same formatter to produce its expected text could repeat the implementation's mistake.
If an application opens a file for writing and then fails before completing it, decide whether the contract allows a partial file. When preserving the prior result matters, test that behavior explicitly. A good check starts with a known prior result, provokes an explained failure, and examines what survives.
Watch for unrelated shared state
Using tmp_path helps only if the function uses the supplied path. A function may also look in the home directory for configuration or call a remote service. Keep those dependencies visible and replace or control them for a unit test. If the program writes files with names derived from the date, pass a fixed input or clock when the exact name is part of the contract. Shared state can hide behind a temporary destination.
Run the same test through any entry point
Save the example as test_write_summary.py and run python -m pytest test_write_summary.py in an environment with pytest installed. The collision example should pass once. If it appears in the terminal but disappears in the editor, follow the VS Code test discovery checks before editing the fixture.