Package: unittest-resources
unittest-resourcesDynamic resource-based unittest suites made easy.Online documentation available athttps://ergoithz.gitlab.io/unittest-resources.UsageFirst and foremost, add this package as a project dependency on yoursetup.py.fromsetuptoolsimportsetupsetup(name='mymodule',# ...test_suite='tests',tests_require=['unittest-resources'],#...)That said, this module can be used in three ways:unittest_resources.ResourceTestCasebase classunittest_resources.ResourceTestMetametaclassunittest_resources.testingtool integrationsAnd then you can run your tests as usual.pythonsetup.pytestRemember you still have to include your testing dependencies by yourself
(likepycodestyle,pydocstyle,mypyorradon) even if they're used only
through this package.Other option, if you feel comfortable about installing allunittest-resourcesoptional dependencies (ie. especially if you're
using all classes defined onunittest_resources.testing), is to
specifyunittest-resources[testing]as dependency.Usingunittest_resources.ResourceTestCasebase classWithunittest_resources.ResourceTestCaseyour base (replacingunittest.TestCase).importunittest_resourcesclassMyTestCase(unittest_resources.ResourceTestCase):"""TestCase class with tests autogenerated based on resources.This regular :class:`unittest.TestCase`-derived class generatestest methods by recursively inspecting a module for resources,based on special class attributes (prefixed by `meta_`).The following functions from :mod:`importlib.resources` areavailable as :class:`staticmethod`s:- :func:`importlib.resources.contents` as `contents`- :func:`importlib.resources.is_resource` as `is_resource`- :func:`importlib.resources.open_binary` as `open_binary`- :func:`importlib.resources.open_text` as `open_text`- :func:`importlib.resources.path` as `path`- :func:`importlib.resources.read_binary` as `read_binary`- :func:`importlib.resources.read_text` as `read_text`"""meta_module='mymodule'meta_prefix='mytest_'meta_resource_pattern=re.compile(r'\.test_file')defmeta_test(self,module,resource):# type: (str, str) -> None"""Function will be called with every resource found.This function received resource-containing module name, and theresource name itself.:param module: resource-containing module name:type module: str:param resource: resource name (usually a filename):type resource: str"""withself.path(module,resource)aspath:self.assertPathStuff(str(path))withself.open_text(module,encoding='utf-8')asf:self.assertTextFileStuff(f)withself.open_binary(module)asf:self.assertBytesFileStuff(f)text=self.read_text(module,resource)self.assertTextStuff(text)data=self.read_binary(module,resource)self.assertBytesStuff(text)Usingunittest_resources.ResourceTestMetametaclassOr, alternatively, using the provided metaclass.importunittestimportunittest_resourcesclassMyTestCase(unittest.TestCase,metaclass=unittest_resources.ResourceTestMeta):"""TestCase class with tests autogenerated based on resources.This regular :class:`unittest.TestCase`-derived class generatestest methods by recursively inspecting a module for resources,based on special class attributes (prefixed by `meta_`).The following functions from :mod:`importlib.resources` areavailable as :class:`staticmethod`s:- :func:`importlib.resources.contents` as `contents`- :func:`importlib.resources.is_resource` as `is_resource`- :func:`importlib.resources.open_binary` as `open_binary`- :func:`importlib.resources.open_text` as `open_text`- :func:`importlib.resources.path` as `path`- :func:`importlib.resources.read_binary` as `read_binary`- :func:`importlib.resources.read_text` as `read_text`"""meta_module='mymodule'meta_prefix='mytest_'meta_resource_pattern=re.compile(r'\.test_file')defmeta_test(self,module,resource):# type: (str, str) -> None"""Function will be called with every resource found.This function received resource-containing module name, and theresource name itself.:param module: resource-containing module name:type module: str:param resource: resource name (usually a filename):type resource: str"""withself.path(module,resource)aspath:self.assertPathStuff(str(path))withself.open_text(module,encoding='utf-8')asf:self.assertTextFileStuff(f)withself.open_binary(module)asf:self.assertBytesFileStuff(f)text=self.read_text(module,resource)self.assertTextStuff(text)data=self.read_binary(module,resource)self.assertBytesStuff(text)Usingunittest_resources.testingtool integrationsThis package provides ready-to-use integrations of some common librariesimportunittestimportunittest_resources.testingclassTestCodeStyle(unittest_resources.testing.CodeStyleTestCase):"""This TestCase class applies pycodestyle to each python file locatedinside a python module."""meta_module='mymodule'Refer tounittest_resources.testingmodule itself to see the available classes andunittest_resources.tests.test_codeto see how to use them.ExamplesThis package owntest suiteis self-explanatory, and covers covers the usage of this module to runpycodestyle,pydocstyle,mypyandradonover project code.You can either subclass and customize them to run on your code, or use
them as an example to integrate other tools.LicenseMIT (seeLICENSEfile).ChangelogSeeCHANGELOGfile.