Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Pytest fixture - tmpdir

  • Probably the simples fixture that PyTest provides is the tmpdir.
  • Pytest will prepare a temporary directory and call the test function passing the path to the tmpdir.
  • PyTest will also clean up the temporary folder, though it will keep the 3 most recent ones. (this is configurable)
  • tmp_path is pathlib.Path.
import os


def test_something(tmpdir):
    print(tmpdir)      # /private/var/folders/ry/z60xxmw0000gn/T/pytest-of-gabor/pytest-14/test_read0
    print(type(tmpdir)) # _pytest._py.path.LocalPath

    d = tmpdir.mkdir("subdir")
    fh = d.join("config.ini")
    fh.write("Some text")

    filename = os.path.join( fh.dirname, fh.basename )

    # ...
pytest -sq test_tmpdir.py