{"id":10139,"library":"pytest-datafiles","title":"pytest-datafiles","description":"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.","status":"active","version":"3.0.1","language":"en","source_language":"en","source_url":"https://github.com/omarkohl/pytest-datafiles","tags":["pytest","testing","fixtures","files","data","pathlib"],"install":[{"cmd":"pip install pytest-datafiles","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"This 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.","package":"pytest","optional":false}],"imports":[{"note":"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.","wrong":"from pytest_datafiles import datafiles","symbol":"pytest.mark.datafiles","correct":"import pytest\nfrom pathlib import Path"}],"quickstart":{"code":"import pytest\nfrom pathlib import Path\nimport os\n\n# Create a dummy 'data' directory and files for the example\n# In a real project, these would be part of your test resources\ndata_dir = Path(__file__).parent / \"data\"\ndata_dir.mkdir(exist_ok=True)\n(data_dir / \"hello.txt\").write_text(\"Hello, pytest-datafiles!\")\n(data_dir / \"config.json\").write_text('{\"key\": \"value\"}')\n\n@pytest.mark.datafiles(data_dir / \"hello.txt\", data_dir / \"config.json\")\ndef test_read_datafile(datafiles: Path):\n    # datafiles is a pathlib.Path object pointing to the temporary directory\n    # where your specified files have been copied.\n    assert datafiles.is_dir()\n\n    hello_file = datafiles / \"hello.txt\"\n    assert hello_file.exists()\n    assert hello_file.read_text() == \"Hello, pytest-datafiles!\"\n\n    config_file = datafiles / \"config.json\"\n    assert config_file.exists()\n    assert config_file.read_text() == '{\"key\": \"value\"}'\n\n    # To run this example, save it as a .py file, create a 'data' directory\n    # next to it, and run `pytest` in that directory.","lang":"python","description":"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."},"warnings":[{"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'`.","message":"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.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Upgrade your Python environment to Python 3.7 or newer. Python 3.8+ is recommended.","message":"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.","severity":"breaking","affected_versions":">=3.0.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.","message":"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.","severity":"gotcha","affected_versions":"all versions"},{"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()`).","message":"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.","severity":"breaking","affected_versions":">=2.0"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Refactor your path manipulation to use `pathlib.Path` methods. For path joining, replace `.join('filename')` with `/ 'filename'`. Example: `datafiles / 'my_file.txt'`.","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.","error":"AttributeError: 'PosixPath' object has no attribute 'join'"},{"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.","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.","error":"PytestUnknownMarkWarning: Unknown pytest.mark.datafiles"},{"fix":"Upgrade your pytest installation to a compatible version: `pip install --upgrade pytest>=6.2.0`.","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`.","error":"Fixture 'tmp_path' not found"}]}