{ "info": { "author": "Jan Vl\u010dinsk\u00fd", "author_email": "jan.vlcinsky@tamtamresearch.com", "bugtrack_url": null, "classifiers": [], "description": "=======================\npytest fixture \"xfiles\"\n=======================\n\nEach test function deserves a companion - a small or large (YAML, JSON or other) file sitting beside.\n\nProviding such data can be as simple as:\n\n.. code-block:: python\n\n \"\"\"Test Romeo y Julieta by Shakespeare.\n \"\"\"\n\n\n def test_romeo(function_yaml):\n print(function_yaml)\n\n\n def test_julieta(function_yaml):\n print(function_yaml)\n\n\n def test_play(module_yaml):\n print(module_yaml)\n\n\n def test_author(package_yaml):\n print(package_yaml)\n\nRun the test:\n\n.. code-block:: bash\n\n $ pytest -sv tests/shakespeare/test_romeoyjulieta.py\n ============================= test session starts ==============================\n ...\n\n tests/shakespeare/test_romeoyjulieta.py::test_romeo {'name': 'Romeo', 'mindmap': ['Julieta', 'Julieta', 'Julieta'], 'spot': 'ladder'}\n PASSED\n tests/shakespeare/test_romeoyjulieta.py::test_julieta {'name': 'Julieta', 'mindmap': ['Romeo', 'Romeo', 'Romeo'], 'spot': 'balcony'}\n PASSED\n tests/shakespeare/test_romeoyjulieta.py::test_play {'story': 'Romeo y Julieta', 'main_characters': ['Romeo', 'Julieta'], 'location': 'Verona'}\n PASSED\n tests/shakespeare/test_romeoyjulieta.py::test_author {'name': 'William', 'surname': 'Shakespeare'}\n PASSED\n\n =========================== 4 passed in 0.03 seconds ===========================\n\nWhere are the files?\n\n.. code-block:: bash\n\n $ ls tests/shakespeare/test_romeoyjulieta.*\n tests/shakespeare/test_romeoyjulieta.julieta.yaml\n tests/shakespeare/test_romeoyjulieta.py\n tests/shakespeare/test_romeoyjulieta.romeo.yaml\n tests/shakespeare/test_romeoyjulieta.yaml\n\nand one more (for the whole package `tests.shakespeare`):\n\n.. code-block:: bash\n\n $ ls tests/shakespeare/__init__.yaml\n tests/shakespeare/__init__.yaml\n\nFile formats supported\n======================\nThe sample above shows use of YAML formatted data. Out of the box, JSON format is also supported via `function_json`, `module_json` and `package_json` fixtures.\n\nMore formats can be supported (see below).\n\nThe `function`, the `module` and the `package`\n==============================================\n\nThe file `tests/shakespeare/__init__.py` defines a test **package**.\n\nThe file `tests/shakespeare/test_romeoyjulieta` is a test **module**\n\nThe `def test_romeo` as in:\n\n.. code-block:: python\n\n def test_romeo(function_yaml):\n print(function_yaml)\n\ndefines a test **function**.\n\nCurrently there is no notion of test **class** as I did not need it. It may appear later on.\n\nNames of data files\n===================\nData file names are derived from related object (package, module, function) and use format specific extensions (`.json`, `.yaml`, special `._x_`, other can be added).\n\nPackage data file has name `__init__.py` with extension changed to format specific one, e.g. `__init__.json`.\n\nModule data file has name such as `test_romeoyjulieta.py` with extension changed to format specific one, e.g. `test_romeoyjulieta.json`.\n\nIn case of test function, test function name must be added. To make files more readable, the `test_` part of the function is removed. For `test_romeo` function can be e.g. `test_romeoyjulieta.romeo.json`.\n\nThe files are typically in the same directory as relevant python test suite code.\n\nWhat are the `._x_` files?\n==========================\nThere are special fixtures `function_xfile`, `module_xfile` and `package_xfile`, which only return path to a file with extension `._x_` (and do not attempt to load the content).\n\nThe `._x_` files are used as base for implementing other fixtures, e.g. as for JSON:\n\n.. code-block:: python\n\n @pytest.fixture(scope=\"function\")\n def function_json(function_xfile):\n path = function_xfile.with_suffix(\".json\")\n with path.open(encoding=\"utf-8\") as f:\n import json\n return json.load(f)\n\nAs shown, the `function_json` simply takes the `._x_` file path, replaces the extension with it's own `.json` and returns data loaded from such file.\n\nAdding support for other data formats (e.g. CSV)\n================================================\nFollowing the `function_json` example above, we may load data from any other data file, e.g. for `.csv`:\n\n.. code-block:: python\n\n from csv import reader\n\n import pytest\n\n\n @pytest.fixture(scope=\"function\")\n def function_csv(function_xfile):\n path = function_xfile.with_suffix(\".csv\")\n with path.open(encoding=\"utf-8\") as f:\n return list(reader(f))\n\n\n def test_codes(function_csv):\n print(function_csv)\n\n.. warning::\n\n Unlike the `{function,module,package}_json` and `{function,module,package}_yaml` fixtures, the `function_csv` (and all the variants) fixture is not provided by this pytest plugin.\n\n Such fixture is intentionally not implemented as it shall be easy to implement it using your prefered extension, delimiter, encoding, type of returned object (data, iterator...) etc.\n\nCreating fixtures based on provided data\n========================================\nIt is easy to take any of data availalbe and use it to create object of your preference. E.g. assuming that the `package_yaml` returns information about author in form of dictionary with keys \"name\" and \"surname\", one can create fixture `classy_author` returning specific class instance. Put following into `conftest.py`:\n\n.. code-block:: python\n\n class Author(object):\n def __init__(self, name, surname):\n self.name = name\n self.surname = surname\n\n @property\n def full_name(self):\n return \"{self.name} {self.surname}\".format(self=self)\n\n\n @pytest.fixture(scope=\"module\")\n def classy_author(package_yaml):\n return Author(package_yaml[\"name\"], package_yaml[\"surname\"])\n\nand use it from test `test_classy_author.py`:\n\n.. code-block:: python\n\n def test_custom_fixture(classy_author):\n print(classy_author.full_name)\n\n\nFixtures provided\n=================\nHere is summary of fixtures provided. In all cases we assume we have `tests/sub/test_thing.py` with a test function `test_fun` and all required data files are available.\n\n`{scope}_xfile` family\n----------------------\nEach fixture provides path to a file with base name derived from current function, module or package and with an extension `\"._x_\"`:\n\n- `function_xfile`: `tests/sub/test_thing.test_fun._x_`\n- `module_xfile`: `tests/sub/test_thing._x_`\n- `package_xfile`: `tests/sub/__init__._x_`\n\n\n`{scope}_json` family\n---------------------\nFixtures provide data loaded from the JSON formatted files:\n\n- `function_json`: `tests/sub/test_thing.test_fun.json`\n- `module_json`: `tests/sub/test_thing.json`\n- `package_json`: `tests/sub/__init__.json`\n\n\n`{scope}_yaml` family\n---------------------\nFixtures provide data loaded from the YAML formatted files:\n\n- `function_yaml`: `tests/sub/test_thing.test_fun.yaml`\n- `module_yaml`: `tests/sub/test_thing.yaml`\n- `package_yaml`: `tests/sub/__init__.yaml`\n\nNote on YAML package and YAML version\n=====================================\nSwitched from `pyyaml` to `ruamel.yaml` as it supports YAML version 1.2.\n\nIf you require YAML files using version 1.1, use `% YAML 1.1` in your YAML file.\n\n\n\n", "description_content_type": null, "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/vlcinsky/pytest_xfiles", "keywords": "pytest", "license": "", "maintainer": "", "maintainer_email": "", "name": "pytest-xfiles", "package_url": "https://pypi.org/project/pytest-xfiles/", "platform": "", "project_url": "https://pypi.org/project/pytest-xfiles/", "project_urls": { "Homepage": "https://github.com/vlcinsky/pytest_xfiles" }, "release_url": "https://pypi.org/project/pytest-xfiles/0.2.0/", "requires_dist": [ "ruamel.yaml", "pathlib; (python_version<'3.4')", "pyyaml; extra == 'yaml'" ], "requires_python": "", "summary": "Pytest fixtures providing data read from function, module or package related (x)files.", "version": "0.2.0" }, "last_serial": 3619209, "releases": { "0.1.0": [ { "comment_text": "", "digests": { "md5": "89e177809fda8a8770d240c4a33f5778", "sha256": "c9e464f4893d5f52de06d71dc2edd0194499f44cdf193fa9b56a39477d2311b0" }, "downloads": -1, "filename": "pytest_xfiles-0.1.0-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "89e177809fda8a8770d240c4a33f5778", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 6326, "upload_time": "2018-01-04T15:38:02", "url": "https://files.pythonhosted.org/packages/9a/48/78c68216762dbfa3508565bf64cc2bb39f82aadebf4f8de9b83418f873b7/pytest_xfiles-0.1.0-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "9fcd0dc6abf909e5d7f1097938c92b8d", "sha256": "996af0c22a8d24bb3e99a91c3208736835c0bcbc86f894272034187fe9026365" }, "downloads": -1, "filename": "pytest_xfiles-0.1.0.tar.gz", "has_sig": false, "md5_digest": "9fcd0dc6abf909e5d7f1097938c92b8d", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6166, "upload_time": "2018-01-04T15:38:05", "url": "https://files.pythonhosted.org/packages/f3/c6/8295b2ab9534089d74346d3120ad9ff67cfd570c2e965be3059c604cd3c7/pytest_xfiles-0.1.0.tar.gz" } ], "0.1.1": [ { "comment_text": "", "digests": { "md5": "b55fedb0087e8b540513c16dd4b35c7a", "sha256": "075cb39fb09b2848564bef5fcf1b1874732b3a90efa888f2cff3553623436af5" }, "downloads": -1, "filename": "pytest_xfiles-0.1.1-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "b55fedb0087e8b540513c16dd4b35c7a", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 7423, "upload_time": "2018-01-04T19:35:03", "url": "https://files.pythonhosted.org/packages/0a/7c/32f45a1384974d822127b7a25b2ae6ed76a68a49721179fb8b3909059443/pytest_xfiles-0.1.1-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "54b5fad8006b22fe1411db4057e6d515", "sha256": "c69cf96f29b4343c01c821885fd1d04f7081fdf06997956a82874ee75347272a" }, "downloads": -1, "filename": "pytest_xfiles-0.1.1.tar.gz", "has_sig": false, "md5_digest": "54b5fad8006b22fe1411db4057e6d515", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6830, "upload_time": "2018-01-04T19:35:05", "url": "https://files.pythonhosted.org/packages/f6/e6/5d17d06f581d9ba8c0a8d91c9968ea09988c331042daa349e3a3f5385468/pytest_xfiles-0.1.1.tar.gz" } ], "0.2.0": [ { "comment_text": "", "digests": { "md5": "5d5634d825d9666524df8f754d907071", "sha256": "004a77071923007c5829387f509a158c0292b193616c61b57143f2f1b7c8ebcd" }, "downloads": -1, "filename": "pytest_xfiles-0.2.0-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "5d5634d825d9666524df8f754d907071", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 7685, "upload_time": "2018-02-27T00:01:12", "url": "https://files.pythonhosted.org/packages/80/ed/dd0575893e79d905f98020426631344a155131c6c51072c4180b7a889a76/pytest_xfiles-0.2.0-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "156d292491814b36612898fb8e9bf154", "sha256": "5fa1aa54c3010f9211fd9f023cce7152437b8e20daef6f046435180227da3c38" }, "downloads": -1, "filename": "pytest_xfiles-0.2.0.tar.gz", "has_sig": false, "md5_digest": "156d292491814b36612898fb8e9bf154", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7070, "upload_time": "2018-02-27T00:01:15", "url": "https://files.pythonhosted.org/packages/6c/c7/637c043a0d71b20bfb34e6f1ff828ad49965596bc0ea430414c55783e741/pytest_xfiles-0.2.0.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "5d5634d825d9666524df8f754d907071", "sha256": "004a77071923007c5829387f509a158c0292b193616c61b57143f2f1b7c8ebcd" }, "downloads": -1, "filename": "pytest_xfiles-0.2.0-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "5d5634d825d9666524df8f754d907071", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 7685, "upload_time": "2018-02-27T00:01:12", "url": "https://files.pythonhosted.org/packages/80/ed/dd0575893e79d905f98020426631344a155131c6c51072c4180b7a889a76/pytest_xfiles-0.2.0-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "156d292491814b36612898fb8e9bf154", "sha256": "5fa1aa54c3010f9211fd9f023cce7152437b8e20daef6f046435180227da3c38" }, "downloads": -1, "filename": "pytest_xfiles-0.2.0.tar.gz", "has_sig": false, "md5_digest": "156d292491814b36612898fb8e9bf154", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7070, "upload_time": "2018-02-27T00:01:15", "url": "https://files.pythonhosted.org/packages/6c/c7/637c043a0d71b20bfb34e6f1ff828ad49965596bc0ea430414c55783e741/pytest_xfiles-0.2.0.tar.gz" } ] }