pytest-dataset


Namepytest-dataset JSON
Version 0.3.2 PyPI version JSON
download
home_pagehttps://github.com/Lavisx/pytest-dataset
SummaryPlugin for loading different datasets for pytest by prefix from json or yaml files
upload_time2023-09-01 13:33:09
maintainer
docs_urlNone
authorLavisx
requires_python
licenseBSD 3-Clause License
keywords py.test resources files data directory directories dataset json yaml
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            dataset plugin for pytest |pypi-badge|
=========================================

The ``dataset`` plugin for pytest_ provides the ``dataset``, ``dataset_case_data``
and ``dataset_copy`` fixtures which allow test functions to easily access resources
in data directories. It was inspired by the `pytest-datadir-ng plugin` .
The ``dataset`` return data from files in ``json`` or ``yaml`` formats.
The main function is usage parameter ``--dataset-prefix`` in options or ``dataset_prefix`` in ini file.

The files are searched in directories in order:

- {dataset_prefix}_{name}.yaml
- {dataset_prefix}_{name}.json
- {name}.yaml
- {name}.json


This plugin provides three fixtures:

- The dataset_ fixture allows test functions and methods to access resources in
  so-called "data directories".
- The `dataset_copy`_ fixture is similar to the dataset_ fixture, but it copies the
  requested resources to a temporary directory first so that test functions or
  methods can modify their resources on-disk without affecting other test functions
  and methods.
- The dataset_case_data_ fixture return dataset named by the test function name.

Installation
------------

Just do::

    pip install pytest-dataset

.. _dataset:

The dataset fixture
-------------------

The "dataset" fixture allows test functions and methods to access resources in
so-called "data directories".

The fixture behaves like a dictionary. Currently, only retrieving items using the
``d[key]`` syntax is supported. Things like iterators, ``len(d)`` etc. are not.
The pytest is called with option ``--dataset-prefix testing``

.. code:: python

    def test_func(dataset):
        data_path = dataset["test_data"]

        # ...

The file ``test_two.py`` contains the following class:

.. code:: python

    class TestClass(object):
        def test_method(self, dataset):
            strings_path = dataset["test_data_two"]

            # ...

When the ``test_func()`` function asks for the ``test_data`` resource, the
following directories are searched for a file in this order.

Files:

- ``testing_test_data.yaml``
- ``testing_test_data.json``
- ``test_data.yaml``
- ``test_data.json``

Directories:

- ``tests/test_one/test_func/``
- ``tests/test_one/``
- ``tests/data/test_one/test_func/``
- ``tests/data/test_one/``
- ``tests/data/``

The path to the first existing file is returned as data. In this case, the returned data would be from file
``tests/test_one/test_func/testing_test_data.yaml``.

When the ``test_method()`` method asks for the ``test_data_two`` resource,
the following directories are searched for a file with the name in this order:

Files:

- ``testing_test_data_two.yaml``
- ``testing_test_data_two.json``
- ``test_data_two.yaml``
- ``test_data_two.json``

Directories:

- ``tests/test_two/TestClass/test_method/``
- ``tests/test_two/TestClass/``
- ``tests/test_two/``
- ``tests/data/test_two/TestClass/test_method/``
- ``tests/data/test_two/TestClass/``
- ``tests/data/test_two/``
- ``tests/data/``

Here, this would return the data from
``tests/test_two/TestClass/test_method/testing_test_data_two.yaml``.

As you can see, the searched directory hierarchy is slighly different if a
*method* instead of a *function* asks for a resource. This allows you to
load different resources based on the name of the test class, if you wish.

Finally, if a test function or test method would ask for a resource named
``global``, then the resulting file would be ``tests/data/{filename}``
since no other directory in the searched directory hierarchy contains
the file. In other words, the ``tests/data/`` directory
is the place for global (or fallback) resources.

If a resource cannot be found in *any* of the searched directories, a
`KeyError` is raised.

.. _dataset_session:

The dataset_session fixture
---------------------------

Similar to "dataset" only used for scope "session".
The path, where are searched the dataset files are reduced to only {rootdir}/data/ .

.. _dataset_package:

The dataset_package fixture
---------------------------

Similar to "dataset" only used for scope "package".
The path, where are searched the dataset files are reduced to only {rootdir}/data/ .

.. _dataset_module:

The dataset_module fixture
--------------------------

Similar to "dataset" only used for scope "module".
The path, where are searched the dataset files are reduced to only possible for "module" scope.

.. _dataset_class:

The dataset_class fixture
--------------------------

Similar to "dataset" only used for scope "class".
The path, where are searched the dataset files are reduced to only possible for "class" scope.

.. _dataset_copy:

The dataset_copy fixture
------------------------

The "dataset_copy" fixture is similar to the dataset_ fixture, but copies the requested resources to a
temporary directory first so that test functions or methods can modify their resources on-disk without affecting
other test functions and methods.

Each test function or method gets its own temporary directory and thus its own fresh copies of the resources it
requests.

.. _dataset_case_data:

The dataset_case_data fixture
-----------------------------

The "dataset_case_data" fixture allows test functions and methods to access resources used the function name as seareched dataset name.

.. code:: python

    class TestClass(object):
        def test_method(self, dataset_case_data):

            # ...

When the ``test_method()`` method is called than dataset_case_data directly contain the data searched in order:

Files:

- ``testing_test_method.yaml``
- ``testing_test_method.json``
- ``test_method.yaml``
- ``test_method.json``

Directories:

- ``tests/test_two/TestClass/test_method/``
- ``tests/test_two/TestClass/``
- ``tests/test_two/``
- ``tests/data/test_two/TestClass/test_method/``
- ``tests/data/test_two/TestClass/``
- ``tests/data/test_two/``
- ``tests/data/``

Here, this would return the data from
``tests/test_two/TestClass/test_method/testing_test_method.yaml``.

..
    NB: Without a trailing question mark in the following image URL, the
        generated HTML will contain an <object> element instead of an <img>
        element, which apparently cannot be made into a link (i. e. a
        "clickable" image).
.. |pypi-badge| image:: https://img.shields.io/pypi/v/pytest-dataset.svg?
    :align: middle
    :target: https://pypi.python.org/pypi/pytest-dataset

.. _pytest: http://pytest.org/
.. _pytest-dataset plugin: https://github.com/Lavisx/pytest-dataset



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/Lavisx/pytest-dataset",
    "name": "pytest-dataset",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "py.test resources files data directory directories dataset json yaml",
    "author": "Lavisx",
    "author_email": "lavis@seznam.cz",
    "download_url": "https://files.pythonhosted.org/packages/c1/b8/3d6d1c5eefe27a9b1222290609d264f3c3127bda0fdb31fbe5260463415a/pytest-dataset-0.3.2.tar.gz",
    "platform": null,
    "description": "dataset plugin for pytest |pypi-badge|\n=========================================\n\nThe ``dataset`` plugin for pytest_ provides the ``dataset``, ``dataset_case_data``\nand ``dataset_copy`` fixtures which allow test functions to easily access resources\nin data directories. It was inspired by the `pytest-datadir-ng plugin` .\nThe ``dataset`` return data from files in ``json`` or ``yaml`` formats.\nThe main function is usage parameter ``--dataset-prefix`` in options or ``dataset_prefix`` in ini file.\n\nThe files are searched in directories in order:\n\n- {dataset_prefix}_{name}.yaml\n- {dataset_prefix}_{name}.json\n- {name}.yaml\n- {name}.json\n\n\nThis plugin provides three fixtures:\n\n- The dataset_ fixture allows test functions and methods to access resources in\n  so-called \"data directories\".\n- The `dataset_copy`_ fixture is similar to the dataset_ fixture, but it copies the\n  requested resources to a temporary directory first so that test functions or\n  methods can modify their resources on-disk without affecting other test functions\n  and methods.\n- The dataset_case_data_ fixture return dataset named by the test function name.\n\nInstallation\n------------\n\nJust do::\n\n    pip install pytest-dataset\n\n.. _dataset:\n\nThe dataset fixture\n-------------------\n\nThe \"dataset\" fixture allows test functions and methods to access resources in\nso-called \"data directories\".\n\nThe fixture behaves like a dictionary. Currently, only retrieving items using the\n``d[key]`` syntax is supported. Things like iterators, ``len(d)`` etc. are not.\nThe pytest is called with option ``--dataset-prefix testing``\n\n.. code:: python\n\n    def test_func(dataset):\n        data_path = dataset[\"test_data\"]\n\n        # ...\n\nThe file ``test_two.py`` contains the following class:\n\n.. code:: python\n\n    class TestClass(object):\n        def test_method(self, dataset):\n            strings_path = dataset[\"test_data_two\"]\n\n            # ...\n\nWhen the ``test_func()`` function asks for the ``test_data`` resource, the\nfollowing directories are searched for a file in this order.\n\nFiles:\n\n- ``testing_test_data.yaml``\n- ``testing_test_data.json``\n- ``test_data.yaml``\n- ``test_data.json``\n\nDirectories:\n\n- ``tests/test_one/test_func/``\n- ``tests/test_one/``\n- ``tests/data/test_one/test_func/``\n- ``tests/data/test_one/``\n- ``tests/data/``\n\nThe path to the first existing file is returned as data. In this case, the returned data would be from file\n``tests/test_one/test_func/testing_test_data.yaml``.\n\nWhen the ``test_method()`` method asks for the ``test_data_two`` resource,\nthe following directories are searched for a file with the name in this order:\n\nFiles:\n\n- ``testing_test_data_two.yaml``\n- ``testing_test_data_two.json``\n- ``test_data_two.yaml``\n- ``test_data_two.json``\n\nDirectories:\n\n- ``tests/test_two/TestClass/test_method/``\n- ``tests/test_two/TestClass/``\n- ``tests/test_two/``\n- ``tests/data/test_two/TestClass/test_method/``\n- ``tests/data/test_two/TestClass/``\n- ``tests/data/test_two/``\n- ``tests/data/``\n\nHere, this would return the data from\n``tests/test_two/TestClass/test_method/testing_test_data_two.yaml``.\n\nAs you can see, the searched directory hierarchy is slighly different if a\n*method* instead of a *function* asks for a resource. This allows you to\nload different resources based on the name of the test class, if you wish.\n\nFinally, if a test function or test method would ask for a resource named\n``global``, then the resulting file would be ``tests/data/{filename}``\nsince no other directory in the searched directory hierarchy contains\nthe file. In other words, the ``tests/data/`` directory\nis the place for global (or fallback) resources.\n\nIf a resource cannot be found in *any* of the searched directories, a\n`KeyError` is raised.\n\n.. _dataset_session:\n\nThe dataset_session fixture\n---------------------------\n\nSimilar to \"dataset\" only used for scope \"session\".\nThe path, where are searched the dataset files are reduced to only {rootdir}/data/ .\n\n.. _dataset_package:\n\nThe dataset_package fixture\n---------------------------\n\nSimilar to \"dataset\" only used for scope \"package\".\nThe path, where are searched the dataset files are reduced to only {rootdir}/data/ .\n\n.. _dataset_module:\n\nThe dataset_module fixture\n--------------------------\n\nSimilar to \"dataset\" only used for scope \"module\".\nThe path, where are searched the dataset files are reduced to only possible for \"module\" scope.\n\n.. _dataset_class:\n\nThe dataset_class fixture\n--------------------------\n\nSimilar to \"dataset\" only used for scope \"class\".\nThe path, where are searched the dataset files are reduced to only possible for \"class\" scope.\n\n.. _dataset_copy:\n\nThe dataset_copy fixture\n------------------------\n\nThe \"dataset_copy\" fixture is similar to the dataset_ fixture, but copies the requested resources to a\ntemporary directory first so that test functions or methods can modify their resources on-disk without affecting\nother test functions and methods.\n\nEach test function or method gets its own temporary directory and thus its own fresh copies of the resources it\nrequests.\n\n.. _dataset_case_data:\n\nThe dataset_case_data fixture\n-----------------------------\n\nThe \"dataset_case_data\" fixture allows test functions and methods to access resources used the function name as seareched dataset name.\n\n.. code:: python\n\n    class TestClass(object):\n        def test_method(self, dataset_case_data):\n\n            # ...\n\nWhen the ``test_method()`` method is called than dataset_case_data directly contain the data searched in order:\n\nFiles:\n\n- ``testing_test_method.yaml``\n- ``testing_test_method.json``\n- ``test_method.yaml``\n- ``test_method.json``\n\nDirectories:\n\n- ``tests/test_two/TestClass/test_method/``\n- ``tests/test_two/TestClass/``\n- ``tests/test_two/``\n- ``tests/data/test_two/TestClass/test_method/``\n- ``tests/data/test_two/TestClass/``\n- ``tests/data/test_two/``\n- ``tests/data/``\n\nHere, this would return the data from\n``tests/test_two/TestClass/test_method/testing_test_method.yaml``.\n\n..\n    NB: Without a trailing question mark in the following image URL, the\n        generated HTML will contain an <object> element instead of an <img>\n        element, which apparently cannot be made into a link (i. e. a\n        \"clickable\" image).\n.. |pypi-badge| image:: https://img.shields.io/pypi/v/pytest-dataset.svg?\n    :align: middle\n    :target: https://pypi.python.org/pypi/pytest-dataset\n\n.. _pytest: http://pytest.org/\n.. _pytest-dataset plugin: https://github.com/Lavisx/pytest-dataset\n\n\n",
    "bugtrack_url": null,
    "license": "BSD 3-Clause License",
    "summary": "Plugin for loading different datasets for pytest by prefix from json or yaml files",
    "version": "0.3.2",
    "project_urls": {
        "Homepage": "https://github.com/Lavisx/pytest-dataset"
    },
    "split_keywords": [
        "py.test",
        "resources",
        "files",
        "data",
        "directory",
        "directories",
        "dataset",
        "json",
        "yaml"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c1b83d6d1c5eefe27a9b1222290609d264f3c3127bda0fdb31fbe5260463415a",
                "md5": "01c6abb4589e9015a4997e8dca80679c",
                "sha256": "15d018f589b38f690408936fa29bce84a9a16bd9f4cea39e384470ff70920cb6"
            },
            "downloads": -1,
            "filename": "pytest-dataset-0.3.2.tar.gz",
            "has_sig": false,
            "md5_digest": "01c6abb4589e9015a4997e8dca80679c",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 7694,
            "upload_time": "2023-09-01T13:33:09",
            "upload_time_iso_8601": "2023-09-01T13:33:09.811093Z",
            "url": "https://files.pythonhosted.org/packages/c1/b8/3d6d1c5eefe27a9b1222290609d264f3c3127bda0fdb31fbe5260463415a/pytest-dataset-0.3.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-09-01 13:33:09",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Lavisx",
    "github_project": "pytest-dataset",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "pytest-dataset"
}
        
Elapsed time: 0.13085s