pytest-datadir


Namepytest-datadir JSON
Version 1.8.0 PyPI version JSON
download
home_pageNone
Summarypytest plugin for test data directories and files
upload_time2025-07-30 13:52:12
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseMIT
keywords pytest test unittest directory file
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # pytest-datadir

pytest plugin for manipulating test data directories and files.

[![Build Status](https://github.com/gabrielcnr/pytest-datadir/workflows/build/badge.svg?branch=master)](https://github.com/gabrielcnr/pytest-datadir/workflows/build/badge.svg?branch=master)
[![PyPI](https://img.shields.io/pypi/v/pytest-datadir.svg)](https://pypi.python.org/pypi/pytest-datadir)
[![CondaForge](https://img.shields.io/conda/vn/conda-forge/pytest-datadir.svg)](https://anaconda.org/conda-forge/pytest-datadir)
![Python Version](https://img.shields.io/badge/python-3.6+-blue.svg)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)


# 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-specific `test_*` folders)
- `shared_datadir` (for the global `data` folder)

```python
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.

```python
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 `joinpath` and `/` operations. While not fully backward-compatible with `datadir`, most tests can switch to `lazy_datadir` without modifications.

### lazy_shared_datadir

`lazy_shared_datadir` is similar to `lazy_datadir`, but applied to the shared data directory `shared_datadir`.
That is, instead of copying all files in `shared_datadir`, files are only copied as necessary when accessed via `joinpath` or the `/` operator.
This allows for a shared data directory to be pulled from lazily in the same manner as `lazy_datadir`.

```python
def test_read_global(lazy_shared_datadir):
    contents = (lazy_shared_datadir / "hello.txt").read_text()
    assert contents == "Hello World!\n"
```

# License

MIT.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "pytest-datadir",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "pytest, test, unittest, directory, file",
    "author": null,
    "author_email": "Gabriel Reis <gabrielcnr@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/b4/46/db060b291999ca048edd06d6fa9ee95945d088edc38b1172c59eeb46ec45/pytest_datadir-1.8.0.tar.gz",
    "platform": null,
    "description": "# pytest-datadir\n\npytest plugin for manipulating test data directories and files.\n\n[![Build Status](https://github.com/gabrielcnr/pytest-datadir/workflows/build/badge.svg?branch=master)](https://github.com/gabrielcnr/pytest-datadir/workflows/build/badge.svg?branch=master)\n[![PyPI](https://img.shields.io/pypi/v/pytest-datadir.svg)](https://pypi.python.org/pypi/pytest-datadir)\n[![CondaForge](https://img.shields.io/conda/vn/conda-forge/pytest-datadir.svg)](https://anaconda.org/conda-forge/pytest-datadir)\n![Python Version](https://img.shields.io/badge/python-3.6+-blue.svg)\n[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)\n\n\n# Usage\n\n`pytest-datadir` automatically looks for a directory matching your module's name or a global `data` folder.\n\nConsider the following directory structure:\n\n```\n.\n\u251c\u2500\u2500 data/\n\u2502\u00a0\u00a0 \u2514\u2500\u2500 hello.txt\n\u251c\u2500\u2500 test_hello/\n\u2502\u00a0\u00a0 \u2514\u2500\u2500 spam.txt\n\u2514\u2500\u2500 test_hello.py\n```\n\nYou can access file contents using the injected fixtures:\n\n- `datadir` (for module-specific `test_*` folders)\n- `shared_datadir` (for the global `data` folder)\n\n```python\ndef test_read_global(shared_datadir):\n    contents = (shared_datadir / \"hello.txt\").read_text()\n    assert contents == \"Hello World!\\n\"\n\n\ndef test_read_module(datadir):\n    contents = (datadir / \"spam.txt\").read_text()\n    assert contents == \"eggs\\n\"\n```\n\nThe contents of the data directory are copied to a temporary folder, ensuring safe file modifications without affecting other tests or original files.\n\nBoth `datadir` and `shared_datadir` fixtures return `pathlib.Path` objects.\n\n## lazy_datadir\n\nVersion 1.7.0 introduced the `lazy_datadir` fixture, which only copies files and directories when accessed via the `joinpath` method or the `/` operator.\n\n```python\ndef test_read_module(lazy_datadir):\n    contents = (lazy_datadir / \"spam.txt\").read_text()\n    assert contents == \"eggs\\n\"\n```\n\nUnlike `datadir`, `lazy_datadir` is an object that only implements `joinpath` and `/` operations. While not fully backward-compatible with `datadir`, most tests can switch to `lazy_datadir` without modifications.\n\n### lazy_shared_datadir\n\n`lazy_shared_datadir` is similar to `lazy_datadir`, but applied to the shared data directory `shared_datadir`.\nThat is, instead of copying all files in `shared_datadir`, files are only copied as necessary when accessed via `joinpath` or the `/` operator.\nThis allows for a shared data directory to be pulled from lazily in the same manner as `lazy_datadir`.\n\n```python\ndef test_read_global(lazy_shared_datadir):\n    contents = (lazy_shared_datadir / \"hello.txt\").read_text()\n    assert contents == \"Hello World!\\n\"\n```\n\n# License\n\nMIT.\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "pytest plugin for test data directories and files",
    "version": "1.8.0",
    "project_urls": {
        "Homepage": "http://github.com/gabrielcnr/pytest-datadir"
    },
    "split_keywords": [
        "pytest",
        " test",
        " unittest",
        " directory",
        " file"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "8f7a33895863aec26ac3bb5068a73583f935680d6ab6af2a9567d409430c3ee1",
                "md5": "0a62fd932bdf7c3cb612646975049d54",
                "sha256": "5c677bc097d907ac71ca418109adc3abe34cf0bddfe6cf78aecfbabd96a15cf0"
            },
            "downloads": -1,
            "filename": "pytest_datadir-1.8.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "0a62fd932bdf7c3cb612646975049d54",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 6512,
            "upload_time": "2025-07-30T13:52:11",
            "upload_time_iso_8601": "2025-07-30T13:52:11.525565Z",
            "url": "https://files.pythonhosted.org/packages/8f/7a/33895863aec26ac3bb5068a73583f935680d6ab6af2a9567d409430c3ee1/pytest_datadir-1.8.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "b446db060b291999ca048edd06d6fa9ee95945d088edc38b1172c59eeb46ec45",
                "md5": "b484cae6536726231dad90aa92487af3",
                "sha256": "7a15faed76cebe87cc91941dd1920a9a38eba56a09c11e9ddf1434d28a0f78eb"
            },
            "downloads": -1,
            "filename": "pytest_datadir-1.8.0.tar.gz",
            "has_sig": false,
            "md5_digest": "b484cae6536726231dad90aa92487af3",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 11848,
            "upload_time": "2025-07-30T13:52:12",
            "upload_time_iso_8601": "2025-07-30T13:52:12.518896Z",
            "url": "https://files.pythonhosted.org/packages/b4/46/db060b291999ca048edd06d6fa9ee95945d088edc38b1172c59eeb46ec45/pytest_datadir-1.8.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-07-30 13:52:12",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "gabrielcnr",
    "github_project": "pytest-datadir",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "pytest-datadir"
}
        
Elapsed time: 3.33095s