Skip to main content

Dynamic resource-based unittest suites made easy.

Project description

unittest-resources

Dynamic resource-based unittest suites made easy.

Usage

This module can be used in three ways:

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 :module:`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 :module:`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.

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

unittest-resources-0.1.0.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

unittest_resources-0.1.0-py2.py3-none-any.whl (11.9 kB view details)

Uploaded Python 2Python 3

File details

Details for the file unittest-resources-0.1.0.tar.gz.

File metadata

  • Download URL: unittest-resources-0.1.0.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using 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.3

File hashes

Hashes for unittest-resources-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3a9726673da64050d3bd7e2e13ce9cbb87e3435fc47ad387d5f11ef2a41ec5ff
MD5 a687002d763bf78cd7ac3f604ec76526
BLAKE2b-256 ecbca86c92fed30a4dd3b083bac2f6c2f9c2fbfff843a810695946f6a021bc1d

See more details on using hashes here.

File details

Details for the file unittest_resources-0.1.0-py2.py3-none-any.whl.

File metadata

  • Download URL: unittest_resources-0.1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 11.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using 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.3

File hashes

Hashes for unittest_resources-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 2d7f2139d0e19d97d94661ad09bbdbaf92ecc258e3f5ff755932b476d4de4109
MD5 f8bfbdd21a85860e4e854616c34e4cdb
BLAKE2b-256 02717be69fbb66aec48d9a893940ef23b419c5677bfd4a95f357d51f2c5e0e9e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page