Skip to main content

unittest-resources

Dynamic resource-based unittest suites made easy.

Online documentation available at https://ergoithz.gitlab.io/unittest-resources.

Usage

First and foremost, add this package as a project dependency on your setup.py.

from setuptools import setup

setup(
    name='mymodule',
    # ...
    test_suite='tests',
    tests_require=['unittest-resources'],
    #...
    )

That said, this module can be used in three ways:

And then you can run your tests as usual.

python setup.py test

Remember you still have to include your testing dependencies by yourself (like pycodestyle, pydocstyle, mypy or radon) even if they're used only through this package.

Other option, if you feel comfortable about installing all unittest-resources optional dependencies (ie. especially if you're using all classes defined on unittest_resources.testing), is to specify unittest-resources[testing] as dependency.

Using unittest_resources.ResourceTestCase base class

With unittest_resources.ResourceTestCase your base (replacing unittest.TestCase).

import unittest_resources


class MyTestCase(unittest_resources.ResourceTestCase):
    """
    TestCase class with tests autogenerated based on resources.

    This regular :class:`unittest.TestCase`-derived class generates
    test methods by recursively inspecting a module for resources,
    based on special class attributes (prefixed by `meta_`).

    The following functions from :mod:`importlib.resources` are
    available 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')

    def meta_test(self, module, resource):
        # type: (str, str) -> None
        """
        Function will be called with every resource found.

        This function received resource-containing module name, and the
        resource name itself.

        :param module: resource-containing module name
        :type module: str
        :param resource: resource name (usually a filename)
        :type resource: str
        """
        with self.path(module, resource) as path:
            self.assertPathStuff(str(path))

        with self.open_text(module, encoding='utf-8') as f:
            self.assertTextFileStuff(f)

        with self.open_binary(module) as f:
            self.assertBytesFileStuff(f)

        text = self.read_text(module, resource)
        self.assertTextStuff(text)

        data = self.read_binary(module, resource)
        self.assertBytesStuff(text)

Using unittest_resources.ResourceTestMeta metaclass

Or, alternatively, using the provided metaclass.

import unittest
import unittest_resources


class MyTestCase(unittest.TestCase,
                 metaclass=unittest_resources.ResourceTestMeta):
    """
    TestCase class with tests autogenerated based on resources.

    This regular :class:`unittest.TestCase`-derived class generates
    test methods by recursively inspecting a module for resources,
    based on special class attributes (prefixed by `meta_`).

    The following functions from :mod:`importlib.resources` are
    available 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')

    def meta_test(self, module, resource):
        # type: (str, str) -> None
        """
        Function will be called with every resource found.

        This function received resource-containing module name, and the
        resource name itself.

        :param module: resource-containing module name
        :type module: str
        :param resource: resource name (usually a filename)
        :type resource: str
        """
        with self.path(module, resource) as path:
            self.assertPathStuff(str(path))

        with self.open_text(module, encoding='utf-8') as f:
            self.assertTextFileStuff(f)

        with self.open_binary(module) as f:
            self.assertBytesFileStuff(f)

        text = self.read_text(module, resource)
        self.assertTextStuff(text)

        data = self.read_binary(module, resource)
        self.assertBytesStuff(text)

Using unittest_resources.testing tool integrations

This package provides ready-to-use integrations of some common libraries

import unittest
import unittest_resources.testing


class TestCodeStyle(unittest_resources.testing.CodeStyleTestCase):
    """
    This TestCase class applies pycodestyle to each python file located
    inside a python module.
    """

    meta_module = 'mymodule'

Refer to unittest_resources.testing module itself to see the available classes and unittest_resources.tests.test_code to see how to use them.

Examples

This package own test suite is self-explanatory, and covers covers the usage of this module to run pycodestyle, pydocstyle, mypy and radon over project code.

You can either subclass and customize them to run on your code, or use them as an example to integrate other tools.

License

MIT (see LICENSE file).

Changelog

See CHANGELOG file.

Release files for unittest-resources 0.2.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 unittest-resources 0.2.0
File Size Uploaded
unittest-resources-0.2.0.tar.gz 11.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for unittest-resources 0.2.0
File Interpreter ABI Platform
unittest_resources-0.2.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 24.0 kB

Release files / unittest-resources-0.2.0.tar.gz

Download URL unittest-resources-0.2.0.tar.gz
Size 11.5 kB
Tags Source
SHA-256 checksum
How to use checksums
98256993dd5ec3cc92df8841a16ee7c4cac72ec650c365c7ebd1f2c278daba72
BLAKE2b-256 checksum
How to use checksums
6b46bc41370dbe3c448ec3b0331e0c92aaa76df91de7c00e4db5c7a91e30b693
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.4

Release files / unittest_resources-0.2.0-py2.py3-none-any.whl

Download URL unittest_resources-0.2.0-py2.py3-none-any.whl
Size 12.5 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
c792c3e4bc4f18df3f4b4160dcb235a50c9dec84e177254e4e5af24a85cb0527
BLAKE2b-256 checksum
How to use checksums
a931503337c523e643911a4af2cd57c662d7df6cd3b150beabb80de1de3f3eb3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.4

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

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