tmp_path

noun

say it: “TEMP PATH” /ˈtɛmp ˈpæθ/

A ready-made pytest fixture that gives one test its own new, empty folder to write files in.

What is tmp_path in pytest?

tmp_path is a fixture pytest has built in: a test that names it as a parameter gets a new, empty folder, as a Path, where it can write files without touching your project.

def test_save_runs(tmp_path):
    log = tmp_path / "runs.txt"
    save_runs(log, ["3.1 miles"])
    assert log.read_text() == "3.1 miles\n"

In a sentence

  • “The test saves the log in tmp_path, so the real log file is never touched.”
  • “Each test gets its own tmp_path, so leftovers from one can't break another.”

Level: Journeyman · More Python terms at this level

Updated