tmp_path
A built-in pytest fixture that gives each test its own fresh temporary directory as a pathlib.Path.
In more detail
Because the directory is unique to one test, nothing another test wrote can make it pass by accident. pytest creates it under the system temporary directory and cleans up old ones automatically, keeping the last few runs for inspection.
Example
def test_report_is_written(tmp_path):
path = write_report(tmp_path, "daily.txt", "ready")Related terms
- pathlib — Python's standard library module for working with file paths as objects rather than plain strings.
- fixture — A named piece of setup, defined with @pytest.fixture, that a test receives by listing the fixture's name as a parameter.
- test isolation — The property that each test sets up everything it needs and leaves nothing behind, so tests pass in any order.
- /tmp — The shared directory where any user or program can create temporary files, often cleared on reboot.
Taught in
- Testing The Filesystem — Automated Testing, TDD & Quality Engineering