Skip to main content

The utility for writing succinct and maximum shortest unit tests

Project description

one-patch

Tests codecov Mypy

Unit-test writing revolution!!

The one-patch way is:

  • One unit test for one function or method.
  • One patch for one unit test.

Or simply:

  • One test - OnePatch.

Installation

pip install one-patch

The problem

Let's try to write unit-test for something like this:

# my_code.py
def failed_function(x):
    raise RuntimeError(f'failed_one: {x}')


class FirstClass:
    first_class_const = '_first_class_const'

    def failed_method(self, x, y):
        raise RuntimeError('_failed_method')

    @classmethod
    def failed_class_method(cls, x, y):
        raise RuntimeError('_failed_class_method')

    @staticmethod
    def failed_static_method(x, y):
        raise RuntimeError('_failed_static_method')

    def success_method(self, method_argument):
        id_ = id(method_argument)

        self.failed_method(1, 2)
        self.failed_class_method(1, 2)
        self.failed_static_method(1, 2)
        return failed_function(id_)
# pytest__my_code.py
import my_code as tm
from unittest.mock import patch, MagicMock


class TestFirstClass:
    def test_success_method(self):
        """ Minimal test (without asserts) """
        with patch.object(tm, 'id', create=True) as mock_id:  # 1
            with patch.object(tm.FirstClass, 'failed_method') as mock_failed_method:  # 2
                with patch.object(tm.FirstClass, 'failed_class_method') as mock_failed_class_method:  # 3
                    with patch.object(tm.FirstClass, 'failed_static_method') as mock_failed_static_method:  # 4
                        with patch.object(tm, 'failed_function') as mock_failed_function:  # 5
                            with patch.object(tm.FirstClass, '__init__', return_value=None):  # 6
                                fc = tm.FirstClass()
                                mock_method_argument = MagicMock()  # 7
                                result = fc.success_method(method_argument=mock_method_argument)

FirstClass.success_method does not have errors itself, but it calls other functions and methods that will fail. Thus, we need to mock them all.

Generally, testing methods or functions call others those need database access, redis, etc. Those calls have to be mocked.

You can do not mock something, but then you will test not only target method or function. You will test target method or function and all not mocked dependencies!! If there are many not mocked dependencies, your test will very complex, very hard to read and difficulty to support.

In other hand we want only 1 failed test for 1 failed method. So, all dependencies have to be mocked.

How you can see in example below, writing a minimal test with mocking all dependencies is very expensive. In the case below we need to write seven mocks to create a minimal test, that just performs successfully, but does not make any asserts.

Let us do something. Let us write a new in reverse style.

# pytest__my_code__reverse.py
import my_code as tm
from unittest.mock import patch, MagicMock


class TestFirstClass:
    def test_success_method(self):
        """ Minimal test (without asserts) """
        c = tm.FirstClass.success_method
        mock_self = MagicMock()  # 1
        with patch.object(tm, 'id', create=True) as mock_id:  # 2
            with patch.object(tm, 'failed_function') as mock_failed_function:  # 3
                mock_method_argument = MagicMock()  # 4
                result = c(self=mock_self, method_argument=mock_method_argument)

Reverse way is better. One need to create four mocks, but it is expensive too.

The solution

Please. Look at the code of reverse test. If you write more unit-tests in reverse style, you will checkout, that all test are very similar to each other.

What happens if we mock all scope of testing method automatically in one statement?

# pytest__my_code__one_patch.py
import my_code as tm
from one_patch import OnePatch


class TestFirstClass:
    def test_success_method(self):
        with OnePatch(func=tm.FirstClass.success_method) as op:  # 1
            result = op.c(*op.args)

Miracle!!

All minimal tests needs only one mock!!

Full one_patch_test

Let us add testing logic.

# pytest__my_code__one_patch.py
import my_code as tm
from one_patch import OnePatch


class TestFirstClass:
    def test_success_method(self):
        """ 
        full test (100% coverage), 
        look pytest__one_patch.TestOnePatch.test_success_method for more information.
        Run this test in debugger, play around.
        """
        with OnePatch(func=tm.FirstClass.success_method) as op:  # 1 op contains all needed mocks
            assert op.c(*op.args) == tm.failed_function.return_value  # assert 1
            tm.failed_function.assert_called_once_with(tm.id.return_value)  # assert 2
            tm.id.assert_called_once_with(op.args.method_argument)  # assert 3
            op.args.self.failed_method.assert_called_once_with(1, 2)  # assert 4
            op.args[0].failed_class_method.assert_called_once_with(1, 2)  # assert 5
            op.args[0].failed_static_method.assert_called_once_with(1, 2)  # assert 6

Testing method contains five not empty lines and six simple statements, look

    def success_method(self, method_argument):
        id_ = id(method_argument)  # statement 1

        self.failed_method(1, 2)  # statement 2
        self.failed_class_method(1, 2)  # statement 3
        self.failed_static_method(1, 2)  # statement 4
        # return failed_function(id_)  
        result = failed_function(id_)  # statement 5
        return result  # statement 6

Great: 6 asserts for 6 statements!!

Just imagine, that you did not write testing method from scratch. Then only one line was added, e.g. my_other_func(). Previously one need to write four or seven mocks, plus mock for my_other_func, plus one or two asserts that checks: return value of my_other_func, and something like assert_called_once_with.

The price is so hi, that one do not write them at all.

class TestFirstClass:
    def test_success_method(self):
        with OnePatch(func=tm.FirstClass.success_method) as op:
            assert op.c(*op.args) == tm.failed_function.return_value
            tm.my_other_func.assert_called_once_with()

The entry threshold has been drastically lowered. Such tests are much more likely to be written.

Testing @propery

Use fget attribute of the property instead of the property itself.

# example
class ExampleProperty:
    @property
    def message(self):
        return 'hello'
# test for example
class TestExampleProperty:
    def test_message(self):
        with OnePatch(tm.ExampleProperty.message.fget) as op:  # ! Use fget 
            assert op.c(*op.args) == 'hello'

Testing logger message interpolation

import logging
logger = logging.getLogger('some.logger')
logger.setLevel(logging.DEBUG)  # if level lower than DEBUG, debug message will not be interpolated


def do_log_debug_fail():
    """
    Calls logger with wrong message template
    """
    # TypeError: not all arguments converted during string formatting
    logger.debug('debug %s', 'fail', 1)

We want to test debug message matches other arguments passed to its call.

    def test_do_log_debug_success(self):
        with OnePatch(tm.do_log_debug_success, exclude_set={'logging', 'logger'}) as op:
            with PatchLogger(tm.logger):
                assert op.c(*op.args) is None

Unpacking OnePatchDTO

OnePatchDTO supports unpacking. This can be useful to make your tests more compact.

    def test_op_dto_unpack(self):
        with OnePatch(tm.FirstClass.success_method) as (op, c, args, s):
            assert op.c == c
            assert op.args == args
            assert op.args.self == s

        with OnePatch(tm.FirstClass.success_class_method) as (op, c, args, cls):
            assert op.c == c
            assert op.args == args
            assert op.args.cls == cls

        with OnePatch(tm.FirstClass.success_static_method) as (op, c, args, s):
            assert op.c == c
            assert op.args == args
            assert s is None
        # check short form
        with OnePatch(tm.FirstClass.success_static_method) as (op, c, *_):
            assert op.c == c

        with OnePatch(tm.FirstClass.success_method) as (op, *_, s):
            assert op.args.self == s

Including (include_set) and excluding (exclude_set)

Sometime we want to patch some builtins, like id, open, type, etc. This is available using include_set.

    def test_success_static_method__include(self):
        with OnePatch(tm.FirstClass.success_static_method__include, include_set={'type'}) as op:
            r = op.c(*op.args)
            assert r == m(m(tm).type).return_value

id function is in include_set by default.

Note: exclude_set has more priority than include_set. So, if you put id in exclude_set, it will not be patched. Example:

    def test_success_static_method__exclude(self):
        with OnePatch(tm.FirstClass.success_static_method__exclude, exclude_set={'id'}) as op:
            r = op.c(*op.args)
            assert isinstance(r, int)  # id was not patched

Note: python3.10 cannot mock that already mocked. OnePatch will exclude mock objects from patching. It is not possible to patch them using include_set or any other way.

In exclude_set you can use object itself or its python path. The excluded object will be collected in ReversePathDTO in exclusions dictionary. Also, you can access it using both ways, the object itself or its python path.

# region exclude_set
with OnePatch(tm.InitCase.use_attrs_inited_in__init, exclude_set={tm.InitCase.__init__}) as op:
    op.exclusions[tm.InitCase.__init__].c(*op.exclusions[tm.InitCase.__init__].args)
    op.c(*op.args)

with OnePatch(tm.InitCase.use_attrs_inited_in__init, exclude_set={'InitCase.__init__'}) as op:
    op.exclusions[tm.InitCase.__init__].c(*op.exclusions[tm.InitCase.__init__].args)
    op.c(*op.args)

with OnePatch(tm.InitCase.use_attrs_inited_in__init, exclude_set={'InitCase.__init__'}) as op:
    op.exclusions[tm.InitCase.__init__].c(*op.exclusions['InitCase.__init__'].args)
    op.c(*op.args)
# endregion exclude_set

Exception classes will be excluded. You can use include_set to force patch this classes.

    def test_skip_exception_classes(self):
        with OnePatch(tm.raise_some_exception) as op:
            # SomeException will not be mocked be default
            with pytest.raises(tm.SomeException):
                op.c(*op.args)

        with OnePatch(tm.raise_some_exception, include_set={'SomeException'}) as op:
            # raise SomeException, if SomeException is a mock object, will produce TypeError
            with pytest.raises(TypeError):  
                op.c(*op.args)

Shortcuts

Op is the same as ReversePath.

def test_op_shortcut__success_method(self):
    with Op(tm.FirstClass.success_method) as op:
        op.c(*op.args)

Oc automatically perform r = op.c(*op.args). Use Oc instead of Op or OnePatch. This is more short and convenient way.

def test_oc_shortcut__success_method(self):
    with Oc(tm.FirstClass.success_method) as oc:
        # do not need `r = op.c(*op.args)`
        assert oc.r == m(tm.failed_function).return_value

Ocl like Oc, it automatically does r = op.c(*op.args). Also, it uses PatchLogger and exclude logging and logger identifiers in the testing module.

Thus, code below can be more short.

    def test_do_log_debug_success(self):
        with OnePatch(tm.do_log_debug_success, exclude_set={'logging', 'logger'}) as op:
            with PatchLogger(tm.logger):
                assert op.c(*op.args) is None

Like this.

    def test_do_log_debug_success__via_shortcut(self):
        with Ocl(tm.do_log_debug_success):
            pass

Note: Ocl works only if your testing module imports logging and creates a logger with identifier name logger.

# testing module
import logging  # Ocl requires
logger = logging.getLogger('my_logger')  # Ocl requires

Examples

Please, look pytest__one_patch, self-documented file contains the most usage examples, those checks OnePatch using OnePatch itself.

More useful examples:

  • TestOnePatch.test_success_method
  • TestOnePatch.test_success_class_method
  • TestOnePatch.test_use_attrs_inited_in__init

Bonus

All mocks created by OnePatch created with autospec. So your tests will defend your code against a human factor in the future, like a dirty refactoring.

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

one_patch-1.0.1.tar.gz (17.1 kB view details)

Uploaded Source

Built Distribution

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

one_patch-1.0.1-py3-none-any.whl (14.9 kB view details)

Uploaded Python 3

File details

Details for the file one_patch-1.0.1.tar.gz.

File metadata

  • Download URL: one_patch-1.0.1.tar.gz
  • Upload date:
  • Size: 17.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.20

File hashes

Hashes for one_patch-1.0.1.tar.gz
Algorithm Hash digest
SHA256 6a6c0bbcc3efd24cf497f98318d78871eb35f518c2b56c2159394a50901ee162
MD5 a4a07c9d5aadb8ca0b53398880d01418
BLAKE2b-256 dc84fe41ba32cafdfc29f7c71500f2de2bf83f93ec78c0ef4e78f586453ce692

See more details on using hashes here.

File details

Details for the file one_patch-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: one_patch-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 14.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.20

File hashes

Hashes for one_patch-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1cd03e2ed8f88a35c1cb225d0e08dba7b74976f727b374efae1f0db2ffa804cc
MD5 6f729faace3ce53445135c5f68848ed5
BLAKE2b-256 f2423cde5a2ed49112ddfd3629263f189454f41a249254f7f218446e63cd0cd0

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