hbp_nrp_commons.MockUtil module

This package provides easy mocking utility for the unit tests

class MockUtil[source]

Bases: object

Defines utility functions to create mocks for any given target module. Following is an example usage of this class:

/* mybase.py */
class MyBase(object):
    def __init__(self):
        self.someAttribute = 'value'

    def my_func(self):
        return 2+2


/* myderived.py */
import os
from some.other.module import AnotherModule
from some.other.module import DontCareMockingModule
from mybase import MyBase  # NOTE: it won't work if MyBase is mocked like AnotherModule

class MyDerived(MyBase):
    def __init__(self):
        self.derivedAttribute = 'value'

    def my_derived_func(self):
        return self.my_func()


/* test_myderived.py */
import unittest
from unittest.mock import MagicMock, Mock, patch, ANY
from hbp_nrp_commons.MockUtil import MockUtil

from myderived import MyDerived  # module to be tested

_base_path_ = 'myderived.'  # import path for MyDerived module (myderived.MyDerived)

@patch(_base_path_ + 'DontCareMockingModule', new=MagicMock())
class TestMyDerived(unittest.TestCase):
    def setUp(self):
        # mock imported modules
        self.m_os = MockUtil.fakeit(self, _base_path_ + 'os')
        self.m_anotherModule = MockUtil.fakeit(self, _base_path_ + 'AnotherModule')

        # mock the bases
        self.m_baseClass = MockUtil.fake_base(self, MyDerived)
        # NOTICE ACTUAL MODULE PASSED

        # now create the object to test
        self.my_derived = MyDerived()  # if needed instantiate in each test individually

    def tearDown(self):
        pass

    def test_derived_func(self):
        # set returns for mocks
        self.m_baseClass.my_func.return_value = 'myval'
        # test for execution
        self.assertEqual(self.my_derived.my_derived_func(), 'myval')
classmethod any()[source]

Helper function to assert equal to anything. Similar to mock.ANY.

classmethod fake_base(test_object, target_class)[source]
Parameters
  • test_object – unittest.TestCase class for which target would be mocked, Generally, this would be the ‘self’ of the class running the test cases.

  • target_class – class whose bases are to be mocked

Returns

mock class that replaced the bases

classmethod fakeit(test_object, target_module)[source]

Creates a mock of target_module for test_object class by invoking test_object.patch() Automatically adds the newly created patch to be cleaned up on test exit.

Parameters
  • test_object – unittest.TestCase class for which target would be mocked, Generally, this would be the ‘self’ of the class running the test cases.

  • target_module – module to be patched by mock.patch()

Returns

MagicMock() object obtained from patch.start()

classmethod imitate(*others)[source]

Creates a MockUtil object that imitates a list of other classes. Attributes of the given classes are replaced with MagicMocks.

Parameters

others – list of classes to be imitated

Returns

MockUtil object containing fake (MagicMock) attributes of all the given classes