Skip to main content

Templates for testing with strings

Project description

ci CodeQL readthedocs.org python3.8 pypi codecov.io mit black

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"
>>>
>>> templatest.templates.registered.getids()
('example-template',)
>>>
>>> templatest.templates.registered.filtergroup('err').getids()
('example-template',)
>>>
>>> templatest.templates.registered.getgroup('err').getids()
()

The class’s properties will then be available in the templatest.templates.registered object as an instance of templatest.Template named tuple

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

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.2.0.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

templatest-0.2.0-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: templatest-0.2.0.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.8.11+ Linux/5.16.18-200.fc35.x86_64

File hashes

Hashes for templatest-0.2.0.tar.gz
Algorithm Hash digest
SHA256 f9fa40face64046f3d8acd9456b7034f8b0a6b125a42d98abd50f7cc5d8bb787
MD5 9da8f16294e160d30c7aeef67c348a3d
BLAKE2b-256 2ba11c8fbcbe13d248144b39e30da4c1dd8b35e5daa727ff75182a61b9400bcd

See more details on using hashes here.

File details

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

File metadata

  • Download URL: templatest-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.8.11+ Linux/5.16.18-200.fc35.x86_64

File hashes

Hashes for templatest-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ae97f74e7d6e15cf03aa4bdd41b2ea4ad1382e08b784bedea3b1b68487448101
MD5 d9efaffcbd3d9ce1c688e29884f06699
BLAKE2b-256 ff2f876c6ee8f1e918a1a51ec6d26f44c1a4621e8ef80daa30b5941cd112f471

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