_pytest.tmpdir module

support for providing temporary directories to test functions.

class TempPathFactory(given_basetemp, trace, basetemp: Optional[pathlib.Path] = None)[source]

Bases: object

Factory for temporary directories under the common base temp directory.

The base directory can be configured using the --basetemp option.

classmethod from_config(config)_pytest.tmpdir.TempPathFactory[source]
Parameters

config – a pytest configuration

_ensure_relative_to_basetemp(basename: str)[source]
mktemp(basename: str, numbered: bool = True)pathlib.Path[source]

Creates a new temporary directory managed by the factory.

Parameters
  • basename – Directory base name, must be a relative path.

  • numbered – If True, ensure the directory is unique by adding a number prefix greater than any existing one: basename="foo" and numbered=True means that this function will create directories named "foo-0", "foo-1", "foo-2" and so on.

Returns

The path to the new directory.

getbasetemp()pathlib.Path[source]

return base temporary directory.

class TempdirFactory(tmppath_factory: _pytest.tmpdir.TempPathFactory)[source]

Bases: object

backward comptibility wrapper that implements :class:py.path.local for :class:TempPathFactory

mktemp(basename: str, numbered: bool = True) → py._path.local.LocalPath[source]

Same as TempPathFactory.mkdir(), but returns a py.path.local object.

getbasetemp() → py._path.local.LocalPath[source]

backward compat wrapper for _tmppath_factory.getbasetemp

get_user() → Optional[str][source]

Return the current user name, or None if getuser() does not work in the current environment (see #1010).

tmpdir_factory(tmp_path_factory)_pytest.tmpdir.TempdirFactory[source]

Return a _pytest.tmpdir.TempdirFactory instance for the test session.

tmp_path_factory(request: _pytest.fixtures.FixtureRequest)_pytest.tmpdir.TempPathFactory[source]

Return a _pytest.tmpdir.TempPathFactory instance for the test session.

_mk_tmp(request: _pytest.fixtures.FixtureRequest, factory: _pytest.tmpdir.TempPathFactory)pathlib.Path[source]
tmpdir(tmp_path) → py._path.local.LocalPath[source]

Return a temporary directory path object which is unique to each test function invocation, created as a sub directory of the base temporary directory. The returned object is a py.path.local object.

tmp_path(request: _pytest.fixtures.FixtureRequest, tmp_path_factory: _pytest.tmpdir.TempPathFactory)pathlib.Path[source]

Return a temporary directory path object which is unique to each test function invocation, created as a sub directory of the base temporary directory. The returned object is a pathlib.Path object.

Note

in python < 3.6 this is a pathlib2.Path