Skip to main content

License PyPI Build CodeQL pre-commit.ci status codecov.io readthedocs.org python3.10 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

Metadata

Release files for templatest 0.12.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for templatest 0.12.0
File Size Uploaded Provenance Actions
templatest-0.12.0.tar.gz 9.7 kB Without provenance Details for templatest-0.12.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for templatest 0.12.0
File Interpreter ABI Platform Actions
templatest-0.12.0-py3-none-any.whl Python 3 none any Details for templatest-0.12.0-py3-none-any.whl

Total release size: 19.7 kB

Release files / templatest-0.12.0.tar.gz

Download URL templatest-0.12.0.tar.gz
Size 9.7 kB
Tags Source
SHA-256 checksum
How to use checksums
c96b3a488eb5835910f6c8ac23ccb82fe60ab9782958c95f42f9e178cb4febf4
BLAKE2b-256 checksum
How to use checksums
0fa2cbc7d1a734611cd9b13d24d28a954a7732df7db7c744cebc60faa87b756c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.8.13 Darwin/24.6.0

Release files / templatest-0.12.0-py3-none-any.whl

Download URL templatest-0.12.0-py3-none-any.whl
Size 9.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dcee45151261eda1a1406f09c969a1d404d749fe3c63cfc62794fc128b38aac3
BLAKE2b-256 checksum
How to use checksums
ea8af0be078e97af47323adff8281be031171a354a9e89ba79fc3856470998c4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.8.13 Darwin/24.6.0

Security for templatest 0.12.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page