Package: templatest
Templates for testing with stringsDesigned withpytest.mark.parametrizein mindWork with subclasses inheriting from thetemplatest.BaseTemplateabstract base classTo use the inherited class decorate it withtemplatest.templates.registerand ensure the module it is in is
imported at runtimeAs 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>>>>>>importtemplatest>>>>>>@templatest.templates.register...class_ExampleTemplate(templatest.BaseTemplate):...@property...deftemplate(self)->str:...return"Hello, world"......@property...defexpected(self)->str:...return"Expected result"The class’s properties will then be available in thetemplatest.templates.registeredobject as an instance oftemplatest.Templatenamed 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...deftemplate(self)->str:...return"Goodbye, world..."......@property...defexpected(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.filtergroupcan be chained, but this won’t work forRegistered.getgroupMore succinctly, multiple prefixes can be used>>># tests/__init__.py>>>>>>@templatest.templates.register...class_MultiExampleTemplate(templatest.BaseTemplate):......@property...deftemplate(self)->str:...return"Hello world, and goodbye world..."......@property...defexpected(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>>>>>>importpytest>>>>>>fromtemplatest.templatesimportregisteredasr>>>>>>@pytest.mark.parametrize("n,t,e",r,ids=r.getids())...deftest_example_all(n:str,t:str,e:str)->None:...>>>>>>@pytest.mark.parametrize("n,t,e",r.filtergroup('err'),ids=r.filtergroup('err').getids())...deftest_example_no_errs(n:str,t:str,e:str)->None:...>>>>>>@pytest.mark.parametrize("n,t,e",r.getgroup('err'),ids=r.getgroup('err').getids())...deftest_example_errs(n:str,t:str,e:str)->None:...withpytest.raises(Exception)aserr:...raiseException(e)......assertstr(err.value)==e