In pytest, monkeypatch temporarily changes attributes, dictionary items and environment variables during a test, and tmp_path gives you a fresh temporary directory as a pathlib.Path. Together, pytest monkeypatch and tmp_path let you test code that reads the environment, the clock or the file system without touching your real machine. Both are built-in fixtures, so you just name them as test arguments.
This guide uses pytest 8 on Python 3.13. Everything monkeypatch changes is undone automatically when the test ends.
A fixture is a function pytest runs for you and passes into the test by parameter name. These two are built in and function-scoped: each test gets its own copy and cleans up afterwards.
| Fixture | Gives you | Cleanup |
|---|---|---|
monkeypatch | Methods to patch attributes, items and env vars | All changes undone after the test |
tmp_path | A unique pathlib.Path directory | Directories from recent runs are kept for inspection, older ones are removed |
tmp_path is a real, empty directory unique to the test. Use it whenever code reads or writes files.
Because each test gets a different directory, tests cannot interfere with each other. The older tmpdir fixture returns a legacy py.path.local object; prefer tmp_path. Use tmp_path_factory when a larger-scoped fixture needs a directory, for example a session fixture that builds a shared data folder.
Code that reads configuration from the environment is a classic monkeypatch case:
Pass raising=False to delenv so the test does not fail when the variable is not set on the machine running it. Without this, the test would behave differently on different machines.
monkeypatch.setattr(target, name, value) replaces an attribute for the duration of the test. It is the simplest way to fake a function that is slow or non-deterministic.
The key rule is to patch the name where it is looked up. Here greeting() calls now from the greeting module, so that is where you replace it. If another module did from greeting import now, you would patch the attribute in that module instead.
You can also use the string form, monkeypatch.setattr("os.getcwd", lambda: "/fake"), and monkeypatch.setitem(mapping, key, value) for dictionaries such as sys.modules or a config dict.
Two helpers cover common process-wide state:
monkeypatch.chdir changes the current directory and restores it afterwards. monkeypatch.syspath_prepend(path) adds an import location in the same reversible way.
Real tests often use both. This one checks a function that stores data in a home directory:
Your real home directory is never read, and nothing is written outside the temporary folder.
unittest.mock is more flexible; monkeypatch is best for simple replacement.monkeypatch.context() as a context manager when you need a change to apply only to part of a test.Yes. After the test finishes, pytest restores every attribute, item, environment variable and directory that the fixture changed.
Under the base temporary directory of the system, in a folder named for the test run. pytest keeps the most recent runs so you can look at the files after a failure.
Not the built-in function-scoped one. Use pytest.MonkeyPatch() as a context manager inside a larger-scoped fixture instead.
tmp_path returns a pathlib.Path, while tmpdir returns an older py.path.local object. New code should use tmp_path.
Use tmp_path whenever a test touches files, and monkeypatch to change environment variables, attributes, dictionary items, the working directory or sys.path for one test only. Patch names where they are looked up, pass raising=False when deleting things that may not exist, and rely on automatic cleanup so tests stay independent.