Skip to main content

Unit test helpers

Project description

About

Unit Test Helpers (uth) is a simple library for Python containing some helper methods for unit testing.

I found myself writing a lot of setup code using unittest.mock.patch and unittest.TestCase.addCleanup:

class MyTestCase(unittest.TestCase):
    def setUp(self):
        patcher = unittest.mock.patch('module.function')
        patcher.start()
        self.addCleanup(patcher.stop)

        # Or if there was many functions and/or objects to patch
        # something like:
        for patcher in (
            unittest.mock.patch('function_a'),
            unittest.mock.patch('function_b'),
        ):
            patcher.start()
            self.addCleanup(patcher.stop)

Adding helper methods to TestCase classes (via mixin class) allows easier patching:

import unittest
import unittest.mock

import uth


def function_a():
    return None


class MyTestCase(unittest.TestCase, uth.MockMixin):
    def setUp(self):
        # The function function_a is immediately patched and patching is stopped
        # automatically after the test has run (using unittest.TestCase.addCleanup).
        mock = self.patched('{}.function_a'.format(__name__), return_value=self.sentinel.retval)

    def test_function_a(self):
        self.assertEqual(function_a(), self.sentinel.retval)

    def test_sentinel(self):
        self.assertEqual(self.sentinel.value, unittest.mock.sentinel.value)

    def test_mock(self):
        mock = self.mock(return_value=self.sentinel.retval)
        self.assertIsInstance(mock, unittest.mock.Mock)

The patched method is the only one that actually provides functionality not directly present in unittest.mock. The members mock, patch and sentinel are provided for convenience; you don’t need to import unittest.mock and you can access mock related functionality through self.

API

uth.MockMixin

A mixin class for unittest.TestCase (or any other class from which you want to use Mock, sentinel and/or patch).

The following members are added:

  • ANY

    unittest.mock.ANY

  • sentinel (property)

    Access to unittest.mock.sentinel

  • call(*args, **kwargs)

    unittest.mock.call

  • mock(*args, **kwargs)

    Calls unittest.mock.Mock with given arguments

  • magic_mock(*args, **kwargs)

    Calls unittest.mock.MagicMock with given arguments

  • patch(*args, **kwargs)

    Calls unittest.mock.patch with given arguments

  • patched(*args, **kwargs)

    Calls unittest.mock.patch with given arguments, starts the returned patcher, registers stopping the patcher using self.addCleanup and returns the return value of patcher.start (the Mock object).

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

uth-1.1.1.tar.gz (2.7 kB view details)

Uploaded Source

Built Distribution

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

uth-1.1.1-py3-none-any.whl (3.4 kB view details)

Uploaded Python 3

File details

Details for the file uth-1.1.1.tar.gz.

File metadata

  • Download URL: uth-1.1.1.tar.gz
  • Upload date:
  • Size: 2.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.2

File hashes

Hashes for uth-1.1.1.tar.gz
Algorithm Hash digest
SHA256 c62f952edcc6d6c321917ba9e2cfebed42a7dc2a71f9da740be2aa4c2c08a48d
MD5 3b3085aa0443bc151993f2a2d3eb9cd6
BLAKE2b-256 dff71ac0b2ee17699ad2efa12747ece1c9838b83901d2641d3dd2f8d9282f9b8

See more details on using hashes here.

File details

Details for the file uth-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: uth-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 3.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.2

File hashes

Hashes for uth-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8d6093162159ee7fa8b36584d84f3b827acfcfa726cc7ba8a30c85292cc9e17c
MD5 802df6f2c6db71ba6c6e70f898539043
BLAKE2b-256 72bd12206ceec2564d1043559678454ba4da848c9850744a954ec48aabdc03ef

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