pytest plugin for test data directories and files
Maintained. Last release 4 weeks ago, commits in 2 of the last 26 weeks. Weaker on commit activity and release cadence.
pip install pytest-datadirpytest-datadir
pytest plugin for manipulating test data directories and files.
Usage
pytest-datadir automatically looks for a directory matching your module's name or a global data folder.
Consider the following directory structure:
.
├── data/
│ └── hello.txt
├── test_hello/
│ └── spam.txt
└── test_hello.py
You can access file contents using the injected fixtures:
datadir(for module-specifictest_*folders)shared_datadir(for the globaldatafolder)
def test_read_global(shared_datadir):
contents = (shared_datadir / "hello.txt").read_text()
assert contents == "Hello World!\n"
def test_read_module(datadir):
contents = (datadir / "spam.txt").read_text()
assert contents == "eggs\n"
The contents of the data directory are copied to a temporary folder, ensuring safe file modifications without affecting other tests or original files.
Both datadir and shared_datadir fixtures return pathlib.Path objects.
lazy_datadir
Version 1.7.0 introduced the lazy_datadir fixture, which only copies files and directories when accessed via the joinpath method or the / operator.
def test_read_module(lazy_datadir):
contents = (lazy_datadir / "spam.txt").read_text()
assert contents == "eggs\n"
Unlike datadir, lazy_datadir is an object that only implements and operations. While not fully backward-compatible with , most tests can switch to without modifications.