pytest-datadir is a pytest plugin designed for easily managing test data directories and files within your test suite. It provides fixtures that automatically locate and copy test data, ensuring tests operate on isolated, temporary copies. The library is actively maintained, with version 1.8.0 being the latest, and receives frequent minor updates with occasional major releases.
Warnings
breaking Python 2.7, 3.4, 3.5, and 3.7 are no longer supported. Version 1.4.0 dropped support for Python 2.7, 3.4, and 3.5. Version 1.5.0 dropped support for Python 3.7. The current minimum Python version required is 3.8.
Fix: Upgrade to Python 3.8 or a newer supported version.
breaking pytest version 7.0 or newer is now required. Older versions of pytest are not supported by recent releases of pytest-datadir.
Fix: Upgrade your `pytest` installation to version 7.0 or higher: `pip install --upgrade pytest`.
gotcha The `datadir` and `shared_datadir` fixtures eagerly copy all associated test data files to a temporary directory at the start of the test. For large data sets or numerous small files, this can introduce performance overhead.
Fix: For versions 1.7.0 and later, consider using `lazy_datadir` or `lazy_shared_datadir` fixtures. These fixtures only copy files or directories when they are explicitly accessed via `joinpath` or the `/` operator, reducing initial setup time and resource usage.
gotcha The `original_datadir` fixture changed its scope from function-scoped to module-scoped in version 1.6.0. This might affect tests that relied on its previous behavior.
Fix: Review tests that use `original_datadir` and adjust them if they implicitly depended on a function-level scope. If you need function-level isolation for original data, you might need to manually copy from `original_datadir` within your test function or use `datadir` for the temporary copy behavior.
deprecated The underlying pytest `tmpdir` fixture, which returns a `py.path.local` object, has been deprecated by pytest itself in favor of `tmp_path`, which returns a `pathlib.Path` object. While `pytest-datadir` internally transitioned to `tmp_path` in v1.4.1, users migrating older tests or using `tmpdir` directly should be aware.
Fix: Prefer `tmp_path` over `tmpdir` in your own fixtures and test code. `pytest-datadir` fixtures (`datadir`, `shared_datadir`, etc.) already return `pathlib.Path` objects, aligning with the modern `pytest` approach.
Install
pip install pytest-datadirInstall stable version
Imports
datadir
def test_something(datadir):
pytest-datadir provides fixtures directly; no explicit 'import from pytest_datadir' statements are typically needed in test files. The fixtures `datadir`, `shared_datadir`, `lazy_datadir`, and `lazy_shared_datadir` are automatically discovered by pytest.
shared_datadir
def test_something(shared_datadir):
pytest-datadir provides fixtures directly; no explicit 'import from pytest_datadir' statements are typically needed in test files. The fixtures `datadir`, `shared_datadir`, `lazy_datadir`, and `lazy_shared_datadir` are automatically discovered by pytest.
lazy_datadir
def test_something(lazy_datadir):
Introduced in v1.7.0. pytest-datadir provides fixtures directly; no explicit 'import from pytest_datadir' statements are typically needed in test files. The fixtures `datadir`, `shared_datadir`, `lazy_datadir`, and `lazy_shared_datadir` are automatically discovered by pytest.
lazy_shared_datadir
def test_something(lazy_shared_datadir):
Introduced in v1.8.0. pytest-datadir provides fixtures directly; no explicit 'import from pytest_datadir' statements are typically needed in test files. The fixtures `datadir`, `shared_datadir`, `lazy_datadir`, and `lazy_shared_datadir` are automatically discovered by pytest.
Quickstart
To use `pytest-datadir`, organize your test data in a `data` directory (for global data) or subdirectories named after your test modules (e.g., `test_module/`) within your test folder. The `datadir` fixture provides access to module-specific data, while `shared_datadir` provides access to global data. Both return `pathlib.Path` objects pointing to temporary copies of your data.
# File: data/hello.txt
Hello World!
# File: test_hello/spam.txt
eggs
# File: test_hello.py
import pytest
def test_read_global(shared_datadir):
# Accesses data from the global 'data' directory
contents = (shared_datadir / "hello.txt").read_text()
assert contents == "Hello World!\n"
def test_read_module(datadir):
# Accesses data from the module-specific 'test_hello' directory
contents = (datadir / "spam.txt").read_text()
assert contents == "eggs\n"