Registry / testing / pytest-datafiles

pytest-datafiles

JSON →
library3.0.1pypypi✓ verified 87d ago

pytest-datafiles is a pytest plugin that simplifies testing by providing a convenient way to make files and directories available to your tests within a temporary directory (leveraging pytest's `tmp_path` fixture). It uses a decorator to specify which files or directories from your source tree should be copied. The current version is 3.0.1, with releases typically happening for bug fixes, new features, or breaking changes in underlying dependencies like pytest or Python versions.

pip install pytest-datafiles
INSTALL
IMPORT
SIG · PYTEST-DATAFILES
P
pytest-datafiles
testingpythonv3.0.1
Install
2.6s avg
Import
399ms
Disk
29MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v3.0.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.423s · 30.7MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 2.6s · import 0.375s · 31MB
29MB installed
● package 29MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

pytest.mark.datafiles
import pytest from pathlib import Path
from pytest_datafiles import datafiles
The primary interaction is via the `pytest.mark.datafiles` decorator. The `datafiles` is a fixture, not a direct importable symbol from the package itself that users typically import directly.

This quickstart demonstrates how to use the `@pytest.mark.datafiles` decorator to specify files to be copied into a temporary directory for your test. The `datafiles` fixture, a `pathlib.Path` object, is then used within the test function to access these files. The example sets up temporary dummy data files for demonstration purposes.

import pytest from pathlib import Path import os # Create a dummy 'data' directory and files for the example # In a real project, these would be part of your test resources data_dir = Path(__file__).parent / "data" data_dir.mkdir(exist_ok=True) (data_dir / "hello.txt").write_text("Hello, pytest-datafiles!") (data_dir / "config.json").write_text('{"key": "value"}') @pytest.mark.datafiles(data_dir / "hello.txt", data_dir / "config.json") def test_read_datafile(datafiles: Path): # datafiles is a pathlib.Path object pointing to the temporary directory # where your specified files have been copied. assert datafiles.is_dir() hello_file = datafiles / "hello.txt" assert hello_file.exists() assert hello_file.read_text() == "Hello, pytest-datafiles!" config_file = datafiles / "config.json" assert config_file.exists() assert config_file.read_text() == '{"key": "value"}' # To run this example, save it as a .py file, create a 'data' directory # next to it, and run `pytest` in that directory.
Debug
Known issues
breakingBreaking Change in 3.0.0: The plugin migrated from `py.path` objects to `pathlib.Path` objects. This affects the type of the `datafiles` fixture passed to your test function.
fix
Update your tests to use `pathlib.Path` methods (e.g., `/` for joining paths, `.read_text()`, `.exists()`) instead of `py.path` methods (e.g., `.join()`, `.read()`). For example, `datafiles.join('my_file.txt')` becomes `datafiles / 'my_file.txt'`.
affects: >=3.0.0
breakingBreaking Change in 3.0.0: Dropped support for older Python versions. Python 2.7 and Python 3.x versions <= 3.6 are no longer supported.
fix
Upgrade your Python environment to Python 3.7 or newer. Python 3.8+ is recommended.
affects: >=3.0.0
gotchaThe actual minimum required pytest version is higher than previously stated. Version 3.0.1 corrected the minimum pytest version requirement from >=3.6 to >=6.2.0.
fix
Ensure your pytest installation is `pytest>=6.2.0`. If you experience issues like 'fixture 'tmp_path' not found', your pytest version might be too old.
affects: all versions
breakingBreaking Change in 2.0: Symlinks are now copied as symbolic links into the temporary directory, not as copies of their targets. This changes behavior when dealing with symlinked files.
fix
Adjust your tests to account for symlinks being preserved as links rather than being dereferenced during the copy operation. If you need the target content, you'll need to resolve the symlink within your test (e.g., `symlink_path.resolve()`).
affects: >=2.0
Errors
Common errors & fixes
AttributeError: 'PosixPath' object has no attribute 'join'
You are trying to use a `py.path` method (`.join()`) on a `pathlib.Path` object. This typically occurs after upgrading to `pytest-datafiles` version 3.0.0 or newer.
fix
Refactor your path manipulation to use `pathlib.Path` methods. For path joining, replace `.join('filename')` with `/ 'filename'`. Example: `datafiles / 'my_file.txt'`.
PytestUnknownMarkWarning: Unknown pytest.mark.datafiles
This warning indicates that pytest doesn't recognize the `datafiles` marker. While `pytest-datafiles` version 2.0.1 and later correctly register the mark, this might still occur if pytest-datafiles is not properly installed, or if your pytest configuration specifically ignores markers.
fix
Ensure `pytest-datafiles` is installed (`pip install pytest-datafiles`). If the issue persists, check your `pytest.ini` or `pyproject.toml` for `markers` configuration that might override or incorrectly list markers. This warning usually doesn't stop tests from running, but indicates a potential setup issue.
Fixture 'tmp_path' not found
The `pytest-datafiles` plugin relies heavily on the `tmp_path` fixture, which was introduced in `pytest>=3.6`. While `pytest-datafiles` itself had a loose requirement, version 3.0.1 clarified it to `pytest>=6.2.0`.
fix
Upgrade your pytest installation to a compatible version: `pip install --upgrade pytest>=6.2.0`.
Upgrade
Version history
3.0.1latest on PyPI · released Jan 4, 2026
Audit
Dependencies
pytestrequiredThis is a pytest plugin and requires pytest to run. Version 3.0.1 explicitly requires pytest >= 6.2.0 for the `tmp_path` fixture and other functionalities.
Agent activity
6 hits · last 30 days
node
6
Resources
pytest-datafiles — pip install pytest-datafiles · libregistry