Coming from a static programming language background, I am wondering how to best do mocking in Python. I am accustomed to dependency injection. Within tests, mocks are created and passed to the System Under Test (SUT). However, looking at Mock and other mock frameworks for Python, it appears that the types/functions/etc. in the module are replaced on a test-by-test basis.
Particularly, with Mock, atop each unit test you say @patch('some.type.in.the.module.under.test')
for each type/function/etc. you want to mock. For the lifetime of the test those things are mocked, then they are reverted. Unfortunately, across tests, the fixture is pretty close to the same and you end up repeating your @patch
es over and over again.
I want a way to share a collection of patches across unit tests. I also want carry through tweaks to the fixture in a composable way. I am okay using a context manager instead of a decorator.
I recently ran into a similar situation but more extreme. One of my top level modules had to mock out several repositories, providers, and logic libraries. This resulted in a number of unit tests that needed to @patch
7 components. I wanted to avoid a lot of duplicate test code so here was my solution which worked pretty well:
@mock.patch('module.blah1.method1') # index: 6
@mock.patch('module.blah1.method2') # index: 5
@mock.patch('module.blah2.method1') # index: 4
@mock.patch('module.blah2.method2') # index: 3
@mock.patch('module.blah2.method3') # index: 2
@mock.patch('module.blah3.method1') # index: 1
@mock.patch('module.blah4.method1') # index: 0
class TestsForMyCode(unittest.TestCase):
def test_first_test(self, *mocks):
# Arrange
# setup mocks for only the ones that need specific mocked behaviours
# idx 2 patches module.blah2.method3
mocks[2].return_value = 'some value'
# Act
target = sut()
result = target.do_something()
# Assert
assert result is False
def test_second_test(self, *mocks):
# Arrange
# setup mocks for only the ones that need specific mocked behaviours
# idx 0 patches module.blah4.method1
mocks[0].return_value = 'another value'
# idx 4 patches module.blah2.method1
mocks[4].return_value = 'another value'
# Act
target = sut()
result = target.do_something_else()
# Assert
assert result is True
The @mock
s on the class are applied to each test when run and pass all the patches into the *mocks parameter. The important thing to remember is the ordering - I place the index comments in my code to keep it straight in my head.
Hope this helps.
You can patch the test class which flows down to each method in that class. And then you could inherit from a super class and work with the the setUp and tearDown methods.
import unittest
@patch('some.type.in.the.module.under.test')
class MySuperTestCase(unittest.TestCase):
pass
class MyActualTestCase(MySuperTestCase):
def test_method(self, mock_function)
mock_function.return_value = False
This is less general than you might think though. Because you need to patch the object in the exact location where it is being used. You don't patch 'sys.stdout', you patch 'my_dir.my_module.sys.stdout'. So it would really only be of use when testing a particular module. But to test that particular model you could certainly only need one single patch decorator.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With