pytest-datafiles
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.
Common errors
-
AttributeError: 'PosixPath' object has no attribute 'join'
cause 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.fixRefactor 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
cause 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.fixEnsure `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
cause 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`.fixUpgrade your pytest installation to a compatible version: `pip install --upgrade pytest>=6.2.0`.
Warnings
- breaking Breaking 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.
- breaking Breaking 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.
- gotcha The 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.
- breaking Breaking 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.
Install
-
pip install pytest-datafiles
Imports
- pytest.mark.datafiles
from pytest_datafiles import datafiles
import pytest from pathlib import Path
Quickstart
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.