templatest
==========
.. image:: https://img.shields.io/badge/License-MIT-yellow.svg
:target: https://opensource.org/licenses/MIT
:alt: License
.. image:: https://img.shields.io/pypi/v/templatest
:target: https://pypi.org/project/templatest/
:alt: PyPI
.. image:: https://github.com/jshwi/templatest/actions/workflows/build.yaml/badge.svg
:target: https://github.com/jshwi/templatest/actions/workflows/build.yaml
:alt: Build
.. image:: https://github.com/jshwi/templatest/actions/workflows/codeql-analysis.yml/badge.svg
:target: https://github.com/jshwi/templatest/actions/workflows/codeql-analysis.yml
:alt: CodeQL
.. image:: https://results.pre-commit.ci/badge/github/jshwi/templatest/master.svg
:target: https://results.pre-commit.ci/latest/github/jshwi/templatest/master
:alt: pre-commit.ci status
.. image:: https://codecov.io/gh/jshwi/templatest/branch/master/graph/badge.svg
:target: https://codecov.io/gh/jshwi/templatest
:alt: codecov.io
.. image:: https://readthedocs.org/projects/templatest/badge/?version=latest
:target: https://templatest.readthedocs.io/en/latest/?badge=latest
:alt: readthedocs.org
.. image:: https://img.shields.io/badge/python-3.8-blue.svg
:target: https://www.python.org/downloads/release/python-380
:alt: python3.8
.. image:: https://img.shields.io/badge/code%20style-black-000000.svg
:target: https://github.com/psf/black
:alt: Black
.. image:: https://img.shields.io/badge/%20imports-isort-%231674b1?style=flat&labelColor=ef8336
:target: https://pycqa.github.io/isort/
:alt: isort
.. image:: https://img.shields.io/badge/%20formatter-docformatter-fedcba.svg
:target: https://github.com/PyCQA/docformatter
:alt: docformatter
.. image:: https://img.shields.io/badge/linting-pylint-yellowgreen
:target: https://github.com/PyCQA/pylint
:alt: pylint
.. image:: https://img.shields.io/badge/security-bandit-yellow.svg
:target: https://github.com/PyCQA/bandit
:alt: Security Status
.. image:: https://snyk.io/test/github/jshwi/templatest/badge.svg
:target: https://snyk.io/test/github/jshwi/templatest/badge.svg
:alt: Known Vulnerabilities
.. image:: https://snyk.io/advisor/python/templatest/badge.svg
:target: https://snyk.io/advisor/python/templatest
:alt: templatest
Templates for testing with strings
----------------------------------
Designed with ``pytest.mark.parametrize`` in mind
Work with subclasses inheriting from the ``templatest.BaseTemplate`` abstract base class
To use the inherited class decorate it with ``templatest.templates.register`` and ensure the module it is in is
imported at runtime
As there will be no need to import anything from this module related to this package, this can be ensured by
placing it in tests/__init__.py
.. code-block:: python
>>> # tests/__init__.py
>>>
>>> import templatest
>>>
>>> @templatest.templates.register
... class _ExampleTemplate(templatest.BaseTemplate):
... @property
... def template(self) -> str:
... return "Hello, world"
...
... @property
... def expected(self) -> str:
... return "Expected result"
The class's properties will then be available in the ``templatest.templates.registered`` object as an instance of
``templatest.Template`` named tuple
.. code-block:: python
>>> templatest.templates.registered
<Registered [Template(name='example-template', template='Hello, world', expected='Expected result')]>
Organise tests by prefixing subclasses for common tests
.. code-block:: python
>>> # tests/__init__.py
>>>
>>> @templatest.templates.register
... class _ErrExampleTemplate(templatest.BaseTemplate):
...
... @property
... def template(self) -> str:
... return "Goodbye, world..."
...
... @property
... def expected(self) -> str:
... return "Goodbye, world..."
>>>
>>> templatest.templates.registered.getids()
('example-template', 'err-example-template')
>>>
>>> templatest.templates.registered.filtergroup('err').getids()
('example-template',)
>>>
>>> templatest.templates.registered.getgroup('err').getids()
('err-example-template',)
``Registered.filtergroup`` can be chained, but this won't work for ``Registered.getgroup``
More succinctly, multiple prefixes can be used
.. code-block:: python
>>> # tests/__init__.py
>>>
>>> @templatest.templates.register
... class _MultiExampleTemplate(templatest.BaseTemplate):
...
... @property
... def template(self) -> str:
... return "Hello world, and goodbye world..."
...
... @property
... def expected(self) -> str:
... return "Hello world, and goodbye world..."
>>>
>>> templatest.templates.registered.filtergroup('err').filtergroup('multi').getids()
('example-template',)
>>>
>>> templatest.templates.registered.getgroup('err').getgroup('multi').getids()
()
>>>
>>> templatest.templates.registered.filtergroup('err', 'multi').getids()
('example-template',)
>>>
>>> templatest.templates.registered.getgroup('err', 'multi').getids()
('err-example-template', 'multi-example-template')
Additionally, templates can be referenced by index
.. code-block::
>>> templatest.templates.registered.getindex('example-template')
0
>>> templatest.templates.registered.getindex('err-example-template')
1
.. code-block:: python
>>> registered = templatest.templates.registered[0]
>>> registered.name
'example-template'
>>> registered.template
'Hello, world'
>>> registered.expected
'Expected result'
.. code-block:: python
>>> name, template, expected = templatest.templates.registered[1]
>>> name
'err-example-template'
>>> template
'Goodbye, world...'
>>> expected
'Goodbye, world...'
And a template can be returned by name
.. code-block::
>>> templatest.templates.registered.getbyname('example-template')
Template(name='example-template', template='Hello, world', expected='Expected result')
>>> templatest.templates.registered.getbyname('err-example-template')
Template(name='err-example-template', template='Goodbye, world...', expected='Goodbye, world...')
Example usage with a parametrized test
**************************************
.. code-block:: python
>>> # tests/_test.py
>>>
>>> import pytest
>>>
>>> from templatest.templates import registered as r
>>>
>>> @pytest.mark.parametrize("n,t,e", r, ids=r.getids())
... def test_example_all(n: str, t: str, e: str) -> None: ...
>>>
>>> @pytest.mark.parametrize("n,t,e", r.filtergroup('err'), ids=r.filtergroup('err').getids())
... def test_example_no_errs(n: str, t: str, e: str) -> None: ...
>>>
>>> @pytest.mark.parametrize("n,t,e", r.getgroup('err'), ids=r.getgroup('err').getids())
... def test_example_errs(n: str, t: str, e: str) -> None:
... with pytest.raises(Exception) as err:
... raise Exception(e)
...
... assert str(err.value) == e
Raw data
{
"_id": null,
"home_page": "https://pypi.org/project/templatest/",
"name": "templatest",
"maintainer": "jshwi",
"docs_url": null,
"requires_python": ">=3.8,<4.0",
"maintainer_email": "stephen@jshwisolutions.com",
"keywords": "expected,pytest,strings,template,unittest",
"author": "jshwi",
"author_email": "stephen@jshwisolutions.com",
"download_url": "https://files.pythonhosted.org/packages/26/23/7bd26d9babea7ad1a6007cb2693b858a0f6531471197a41b9c435c08c317/templatest-0.10.1.tar.gz",
"platform": null,
"description": "templatest\n==========\n.. image:: https://img.shields.io/badge/License-MIT-yellow.svg\n :target: https://opensource.org/licenses/MIT\n :alt: License\n.. image:: https://img.shields.io/pypi/v/templatest\n :target: https://pypi.org/project/templatest/\n :alt: PyPI\n.. image:: https://github.com/jshwi/templatest/actions/workflows/build.yaml/badge.svg\n :target: https://github.com/jshwi/templatest/actions/workflows/build.yaml\n :alt: Build\n.. image:: https://github.com/jshwi/templatest/actions/workflows/codeql-analysis.yml/badge.svg\n :target: https://github.com/jshwi/templatest/actions/workflows/codeql-analysis.yml\n :alt: CodeQL\n.. image:: https://results.pre-commit.ci/badge/github/jshwi/templatest/master.svg\n :target: https://results.pre-commit.ci/latest/github/jshwi/templatest/master\n :alt: pre-commit.ci status\n.. image:: https://codecov.io/gh/jshwi/templatest/branch/master/graph/badge.svg\n :target: https://codecov.io/gh/jshwi/templatest\n :alt: codecov.io\n.. image:: https://readthedocs.org/projects/templatest/badge/?version=latest\n :target: https://templatest.readthedocs.io/en/latest/?badge=latest\n :alt: readthedocs.org\n.. image:: https://img.shields.io/badge/python-3.8-blue.svg\n :target: https://www.python.org/downloads/release/python-380\n :alt: python3.8\n.. image:: https://img.shields.io/badge/code%20style-black-000000.svg\n :target: https://github.com/psf/black\n :alt: Black\n.. image:: https://img.shields.io/badge/%20imports-isort-%231674b1?style=flat&labelColor=ef8336\n :target: https://pycqa.github.io/isort/\n :alt: isort\n.. image:: https://img.shields.io/badge/%20formatter-docformatter-fedcba.svg\n :target: https://github.com/PyCQA/docformatter\n :alt: docformatter\n.. image:: https://img.shields.io/badge/linting-pylint-yellowgreen\n :target: https://github.com/PyCQA/pylint\n :alt: pylint\n.. image:: https://img.shields.io/badge/security-bandit-yellow.svg\n :target: https://github.com/PyCQA/bandit\n :alt: Security Status\n.. image:: https://snyk.io/test/github/jshwi/templatest/badge.svg\n :target: https://snyk.io/test/github/jshwi/templatest/badge.svg\n :alt: Known Vulnerabilities\n.. image:: https://snyk.io/advisor/python/templatest/badge.svg\n :target: https://snyk.io/advisor/python/templatest\n :alt: templatest\n\nTemplates for testing with strings\n----------------------------------\n\nDesigned with ``pytest.mark.parametrize`` in mind\n\nWork with subclasses inheriting from the ``templatest.BaseTemplate`` abstract base class\n\nTo use the inherited class decorate it with ``templatest.templates.register`` and ensure the module it is in is\nimported at runtime\n\nAs there will be no need to import anything from this module related to this package, this can be ensured by\nplacing it in tests/__init__.py\n\n.. code-block:: python\n\n >>> # tests/__init__.py\n >>>\n >>> import templatest\n >>>\n >>> @templatest.templates.register\n ... class _ExampleTemplate(templatest.BaseTemplate):\n ... @property\n ... def template(self) -> str:\n ... return \"Hello, world\"\n ...\n ... @property\n ... def expected(self) -> str:\n ... return \"Expected result\"\n\n\nThe class's properties will then be available in the ``templatest.templates.registered`` object as an instance of\n``templatest.Template`` named tuple\n\n.. code-block:: python\n\n >>> templatest.templates.registered\n <Registered [Template(name='example-template', template='Hello, world', expected='Expected result')]>\n\nOrganise tests by prefixing subclasses for common tests\n\n.. code-block:: python\n\n >>> # tests/__init__.py\n >>>\n >>> @templatest.templates.register\n ... class _ErrExampleTemplate(templatest.BaseTemplate):\n ...\n ... @property\n ... def template(self) -> str:\n ... return \"Goodbye, world...\"\n ...\n ... @property\n ... def expected(self) -> str:\n ... return \"Goodbye, world...\"\n >>>\n >>> templatest.templates.registered.getids()\n ('example-template', 'err-example-template')\n >>>\n >>> templatest.templates.registered.filtergroup('err').getids()\n ('example-template',)\n >>>\n >>> templatest.templates.registered.getgroup('err').getids()\n ('err-example-template',)\n\n``Registered.filtergroup`` can be chained, but this won't work for ``Registered.getgroup``\n\nMore succinctly, multiple prefixes can be used\n\n.. code-block:: python\n\n >>> # tests/__init__.py\n >>>\n >>> @templatest.templates.register\n ... class _MultiExampleTemplate(templatest.BaseTemplate):\n ...\n ... @property\n ... def template(self) -> str:\n ... return \"Hello world, and goodbye world...\"\n ...\n ... @property\n ... def expected(self) -> str:\n ... return \"Hello world, and goodbye world...\"\n >>>\n >>> templatest.templates.registered.filtergroup('err').filtergroup('multi').getids()\n ('example-template',)\n >>>\n >>> templatest.templates.registered.getgroup('err').getgroup('multi').getids()\n ()\n >>>\n >>> templatest.templates.registered.filtergroup('err', 'multi').getids()\n ('example-template',)\n >>>\n >>> templatest.templates.registered.getgroup('err', 'multi').getids()\n ('err-example-template', 'multi-example-template')\n\nAdditionally, templates can be referenced by index\n\n.. code-block::\n\n >>> templatest.templates.registered.getindex('example-template')\n 0\n >>> templatest.templates.registered.getindex('err-example-template')\n 1\n\n.. code-block:: python\n\n >>> registered = templatest.templates.registered[0]\n >>> registered.name\n 'example-template'\n >>> registered.template\n 'Hello, world'\n >>> registered.expected\n 'Expected result'\n\n.. code-block:: python\n\n >>> name, template, expected = templatest.templates.registered[1]\n >>> name\n 'err-example-template'\n >>> template\n 'Goodbye, world...'\n >>> expected\n 'Goodbye, world...'\n\nAnd a template can be returned by name\n\n.. code-block::\n\n >>> templatest.templates.registered.getbyname('example-template')\n Template(name='example-template', template='Hello, world', expected='Expected result')\n >>> templatest.templates.registered.getbyname('err-example-template')\n Template(name='err-example-template', template='Goodbye, world...', expected='Goodbye, world...')\n\nExample usage with a parametrized test\n**************************************\n\n.. code-block:: python\n\n >>> # tests/_test.py\n >>>\n >>> import pytest\n >>>\n >>> from templatest.templates import registered as r\n >>>\n >>> @pytest.mark.parametrize(\"n,t,e\", r, ids=r.getids())\n ... def test_example_all(n: str, t: str, e: str) -> None: ...\n >>>\n >>> @pytest.mark.parametrize(\"n,t,e\", r.filtergroup('err'), ids=r.filtergroup('err').getids())\n ... def test_example_no_errs(n: str, t: str, e: str) -> None: ...\n >>>\n >>> @pytest.mark.parametrize(\"n,t,e\", r.getgroup('err'), ids=r.getgroup('err').getids())\n ... def test_example_errs(n: str, t: str, e: str) -> None:\n ... with pytest.raises(Exception) as err:\n ... raise Exception(e)\n ...\n ... assert str(err.value) == e\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Templates for testing with strings",
"version": "0.10.1",
"project_urls": {
"Documentation": "https://templatest.readthedocs.io/en/latest",
"Homepage": "https://pypi.org/project/templatest/",
"Repository": "https://github.com/jshwi/templatest"
},
"split_keywords": [
"expected",
"pytest",
"strings",
"template",
"unittest"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "f5390df975a0a4d6694f7a82ac69548338d7a5d058fe719644247bac9ead7d4c",
"md5": "c0d951448023d63cfe7e1c8919e632ce",
"sha256": "cee4d29d1939ef735a0c9f2c64778f8db6c8559a04096ee435684172e04f48de"
},
"downloads": -1,
"filename": "templatest-0.10.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "c0d951448023d63cfe7e1c8919e632ce",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8,<4.0",
"size": 9823,
"upload_time": "2023-10-21T00:05:51",
"upload_time_iso_8601": "2023-10-21T00:05:51.331927Z",
"url": "https://files.pythonhosted.org/packages/f5/39/0df975a0a4d6694f7a82ac69548338d7a5d058fe719644247bac9ead7d4c/templatest-0.10.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "26237bd26d9babea7ad1a6007cb2693b858a0f6531471197a41b9c435c08c317",
"md5": "dbe9859d96cf850c57332895dacc8262",
"sha256": "bbc61ca7fc77127f9f7761af7a9db08ed4b4963887b41a01fe7715543e02991a"
},
"downloads": -1,
"filename": "templatest-0.10.1.tar.gz",
"has_sig": false,
"md5_digest": "dbe9859d96cf850c57332895dacc8262",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8,<4.0",
"size": 9423,
"upload_time": "2023-10-21T00:05:53",
"upload_time_iso_8601": "2023-10-21T00:05:53.120211Z",
"url": "https://files.pythonhosted.org/packages/26/23/7bd26d9babea7ad1a6007cb2693b858a0f6531471197a41b9c435c08c317/templatest-0.10.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-10-21 00:05:53",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "jshwi",
"github_project": "templatest",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "templatest"
}