pytest-datadir

1.8.0 · active · verified Sat Apr 11

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

Install

Imports

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"

view raw JSON →