Skip to main content

Templates for testing with strings

Project description

License PyPI Build CodeQL pre-commit.ci status codecov.io readthedocs.org python3.8 Black isort docformatter pylint Security Status Known Vulnerabilities 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

>>> # 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

>>> templatest.templates.registered
<Registered [Template(name='example-template', template='Hello, world', expected='Expected result')]>

Organise tests by prefixing subclasses for common tests

>>> # 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

>>> # 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

>>> templatest.templates.registered.getindex('example-template')
0
>>> templatest.templates.registered.getindex('err-example-template')
1
>>> registered = templatest.templates.registered[0]
>>> registered.name
'example-template'
>>> registered.template
'Hello, world'
>>> registered.expected
'Expected result'
>>> 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

>>> 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

>>> # 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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

templatest-0.10.1.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

templatest-0.10.1-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file templatest-0.10.1.tar.gz.

File metadata

  • Download URL: templatest-0.10.1.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.8.13 Darwin/22.6.0

File hashes

Hashes for templatest-0.10.1.tar.gz
Algorithm Hash digest
SHA256 bbc61ca7fc77127f9f7761af7a9db08ed4b4963887b41a01fe7715543e02991a
MD5 dbe9859d96cf850c57332895dacc8262
BLAKE2b-256 26237bd26d9babea7ad1a6007cb2693b858a0f6531471197a41b9c435c08c317

See more details on using hashes here.

File details

Details for the file templatest-0.10.1-py3-none-any.whl.

File metadata

  • Download URL: templatest-0.10.1-py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.8.13 Darwin/22.6.0

File hashes

Hashes for templatest-0.10.1-py3-none-any.whl
Algorithm Hash digest
SHA256 cee4d29d1939ef735a0c9f2c64778f8db6c8559a04096ee435684172e04f48de
MD5 c0d951448023d63cfe7e1c8919e632ce
BLAKE2b-256 f5390df975a0a4d6694f7a82ac69548338d7a5d058fe719644247bac9ead7d4c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page