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

  • tmpdir

  • Probably the simples fixture that PyTest can provide 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)

import os


def test_something(tmpdir):
    print(tmpdir)      # /private/var/folders/ry/z60xxmw0000gn/T/pytest-of-gabor/pytest-14/test_read0

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

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

    temp_dir = str(tmpdir)

    # ...