I'm testing complex logic that require joining of central fact table with 10-20 smaller dimensional tables. I want to mock that 10-20 smaller tables.
How to patch methods return values in a for loop? See code below.
tables.py:
class BaseClass(object):
def load(path: str):
...
class SmallTable1(BaseClass):
def load():
super().load(self.path)
class SmallTable20(BaseClass):
def load():
super().load(self.path)
test_logic.py
# QUESTION - how to make it work
def test_not_work(datasets):
for i in range(1, 21):
table = 'SmallTable' + str(i)
with mock.patch('some_package.tables.{}.load'.format(table)) as load_mock:
load_mock.return_value = datasets[table]
do_joins() # here my mocks doesn't work
def test_works(datasets):
with mock.patch('some_package.tables.SmallTable1.load'.format(i)) as load_mock_1:
load_mock_1.return_value = datasets[SmallTable1]
with mock.patch('some_package.tables.SmallTable2.load'.format(i)) as load_mock_2:
load_mock_2.return_value = datasets[SmallTable2]
..... # repeat the same 8-18 times more
do_joins() # here my mocks do work, but with cumbersome code and huge right offset
P.S. alternatively I can try to mock the BaseClass.load, but then I don't know how to return the different data set for different table (class).
Under the assumption that do_join shall be called outside the loop, with all tables patched, you could write a fixture that uses contextlib.ExitStack to setup all mocks:
from contextlib import ExitStack
from unittest import mock
import pytest
from some_package import do_join
#pytest.fixture
def datasets():
...
#pytest.fixture
def mock_tables(datasets):
with ExitStack() as stack:
for i in range(1, 21):
table = 'SmallTable' + str(i)
load_mock = stack.enter_context(
mock.patch('some_package.tables.{}.load'.format(table)))
load_mock.return_value = datasets[table]
yield
def test_join(mock_tables):
do_join()
This means that all mocks are still active at yield time, and will only be removed after the test is finished.
If you have pytest-mock installed, you can use the mocker fixture instead:
#pytest.fixture
def mock_tables(datasets, mocker):
for i in range(1, 21):
table = 'SmallTable' + str(i)
load_mock = mocker.patch('some_package.tables.{}.load'.format(table))
load_mock.return_value = datasets[table]
yield
Related
I am trying to mock a bigtable call in my unit test by declaring fixtures like so:
#pytest.fixture()
def bigtableMock():
bigtableMock = Mock(spec=google.cloud.bigtable.table.Table)
yield bigtableMock
#pytest.fixture()
def bigtableInstanceMock(bigtableMock):
bigtableInstanceMock = Mock(spec=google.cloud.bigtable.instance.Instance)
bigtableInstanceMockAttrs = {'table': bigtableMock}
bigtableInstanceMock.configure_mock(**bigtableInstanceMockAttrs)
yield bigtableInstanceMock
#pytest.fixture()
def myDao(bigtableInstanceMock):
yield MyDao(bigtableInstanceMock)
I mock the read_rows function like so:
def mockReadRowsFuncWith1Dto(testDto):
mockTableRowData = {}
mockTableRowData['columnFamily'] = asDict(testDto)
rowDataMock = MagicMock()
rowDataMock.__iter__.return_value = [mockTableRowData]
rowDataMock.__len__ = 1
def mockReadRowsFunc(startKey, endKey, limit, end_inclusive):
return rowDataMock
return mockReadRowsFunc
When I call my test function:
def test_read_table(
myDao,
testDto,
bigtableMock
):
bigtableMock.read_rows = mockReadRowsFuncWith1Dto(testDto)
samp = bigtableMock.read_rows(
startKey="asdf",
endKey="sadf",
limit=1,
end_inclusive=True
)
print(f"\test data {samp}")
myDao.readTable(...)
Inside myDao.readTable I call read_rows like so:
tableRows: PartialRowData = self.table.read_rows(
start_key=startKey,
end_key=endKey,
limit=10,
end_inclusive=True
)
However, I do not get the magicMock return that I expect inside readTable, tableRows:<Mock name='mock.table().read_rows()' id='4378752480'>, whereas in the test function I can print out the Magic mock: test data <MagicMock id='4413191168'>. Regardless of the print statement or not, I can never invoke the correct mocked read_rows function. What am I doing wrong?
The problem in my case was that the fixture for bigtableMock was different between test_read_table and the fixture for myDao. I modified my test cases to include the table mocking inside the bigtableMock like so:
#pytest.fixture(read_row_data, mock_append_row)
def bigtableMock():
bigtableMock = Mock(spec=google.cloud.bigtable.table.Table)
bigtableMock.read_rows.return_value = [read_row_data]
bigtableMock.append_row.return_value = mock_append_row
yield bigtableMock
Most of my unit testing experience is with Java and now I'm turning to Python. I need to test whether a method (from object B) gets called inside another method (in object A).
In Java the test method would have to pass a mock or spy version of B to A's constructor to be used when the B method is invoked. Do I need to do the same in Python? Or is there a simpler way? (I raise the possibility of the latter, because it seems, from what little I know, that Python is relatively relaxed about enforcing isolation between different components.)
Below is how I do this the "Java way." There are two Python files under test (for objects A and B) and a test program. Notice that object A's constructor had to be modified to accommodate testing.
obj_a.py
from obj_b import *
class ObjA:
def __init__(self, *args):
if len(args) > 0:
self.objb = args[0] # for testing
return
self.objb = ObjB()
def methodCallsB(self, x, y):
return self.objb.add(x, y)
obj_b.py
class ObjB:
def add(self, x, y):
return x + y
test.py
import unittest
from unittest.mock import patch, Mock
from obj_a import *
from obj_b import *
class TTest(unittest.TestCase):
#patch("obj_b.ObjB")
def test_shouldCallBThroughA(self, mockB):
# configure mock
mockB.add = Mock(return_value=137)
obja = ObjA(mockB)
# invoke test method
res = obja.methodCallsB(4, 7)
print("result: " + str(res))
# assess results
self.assertEqual(137, res)
mockB.add.assert_called_once()
args = mockB.add.call_args[0] # Python 3.7
print("args: " + str(args))
self.assertEqual((4, 7), args)
if __name__ =='__main__':
unittest.main()
Again, is there a simpler way to test that ObjB::add is called from ObjA?
Apart from the possible problems with the design, mentioned in the comment by #Alex, there is a couple of errors in using the mock.
First, you are mocking the wrong object. As in object_a you do from obj_b import * (which is bad style by the way - only import the objects you need), you need to patch the object reference imported into obj_b, e.g. obj_a.ObjB (see where to patch).
Second, you have to mock the method call on the instance instead of the class, e.g. mock mockB.return_value.add instead of mockB.add.
Your tests actually only work because you are not testing your real function, only your mock. If you do the patching correctly, there is no need to add that test-specific code in __init__.
So, put together, something like this should work:
obj_a.py
class ObjA:
def __init__(self):
self.objb = ObjB()
...
test.py
class TTest(unittest.TestCase):
#patch("obj_a.ObjB")
def test_shouldCallBThroughA(self, mockB):
# for convenience, store the mocked method
mocked_add = mockB.return_value.add
mocked_add.return_value = 137
obja = ObjA()
res = obja.methodCallsB(4, 7)
self.assertEqual(137, res)
mocked_add.assert_called_once()
args = mocked_add.call_args[0]
self.assertEqual((4, 7), args)
Let's say I have the following python files:
# source.py
def get_one():
return 1
# file1.py
from source import get_one
def func1():
return get_one()
# file2.py
from source import get_one
def func2():
return get_one()
# script.py
from file1 import func1
from file2 import func2
def main(a, b):
count = 0
for _ in range(a):
count += func1()
for _ in range(b):
count += func2()
return count
I know I can mock out get_one() in main.py using the following setup:
def test_mock():
with (
patch("file1.get_one") as mock1,
patch("file2.get_one") as mock2,
):
main(2, 3)
assert mock1.call_count + mock2.call_count == 5
However, this gets increasingly verbose and hard to read if get_one() needs to be mocked out in many files. I would love to be able to mock out all of its locations in a single mock variable. Something like:
# this test fails, I'm just showing what this ideally would look like
def test_mock():
with patch("file1.get_one", "file2.get_one") as mock:
main(2, 3)
assert mock.call_count == 5
Is there anyway to do this or do I need to use multiple mocks?
Note, I know I can't mock where the function is defined, e.g. patch("source.get_one").
patch accepts new as the object to patch the target with:
def test_mock():
with (
patch("file1.get_one") as mock,
patch("file2.get_one", new=mock),
):
main(2, 3)
assert mock.call_count == 5, mock.call_count
You can write a helper context manager:
import contextlib
from unittest.mock import DEFAULT, patch
#contextlib.contextmanager
def patch_same(target, *targets, new=DEFAULT):
with patch(target, new=new) as mock:
if targets:
with patch_same(*targets, new=mock):
yield mock
else:
yield mock
Usage:
def test_mock():
with patch_same("file1.get_one", "file2.get_one") as mock:
main(2, 3)
assert mock.call_count == 5
A possible workaround: add a level of indirection in source.py so that get_one has a dependency that you can patch:
def get_one():
return _get_one()
def _get_one():
return 1
and now you can do this in your test:
from script import main
from unittest.mock import patch
def test_mock():
with patch("source._get_one") as mock1:
main(2, 3)
assert mock1.call_count == 5
I created a class to make my life easier while doing some integration tests involving workers and their contracts. The code looks like this:
class ContractID(str):
contract_counter = 0
contract_list = list()
def __new__(cls):
cls.contract_counter += 1
new_entry = super().__new__(cls, f'Some_internal_name-{cls.contract_counter:10d}')
cls.contract_list.append(new_entry)
return new_entry
#classmethod
def get_contract_no(cls, worker_number):
return cls.contract_list[worker_number-1] # -1 so WORKER1 has contract #1 and not #0 etc.
When I'm unit-testing the class, I'm using the following code:
from test_helpers import ContractID
#pytest.fixture
def get_contract_numbers():
test_string_1 = ContractID()
test_string_2 = ContractID()
test_string_3 = ContractID()
return test_string_1, test_string_2, test_string_3
def test_contract_id(get_contract_numbers):
assert get_contract_ids[0] == 'Some_internal_name-0000000001'
assert get_contract_ids[1] == 'Some_internal_name-0000000002'
assert get_contract_ids[2] == 'Some_internal_name-0000000003'
def test_contract_id_get_contract_no(get_contract_numbers):
assert ContractID.get_contract_no(1) == 'Some_internal_name-0000000001'
assert ContractID.get_contract_no(2) == 'Some_internal_name-0000000002'
assert ContractID.get_contract_no(3) == 'Some_internal_name-0000000003'
with pytest.raises(IndexError) as py_e:
ContractID.get_contract_no(4)
assert py_e.type == IndexError
However, when I try to run these tests, the second one (test_contract_id_get_contract_no) fails, because it does not raise the error as there are more than three values. Furthermore, when I try to run all my tests in my folder test/, it fails even the first test (test_contract_id), which is probably because I'm trying to use this function in other tests that run before this test.
After reading this book, my understanding of fixtures was that it provides objects as if they were never called before, which is obviously not the case here. Is there a way how to tell the tests to use the class as if it hasn't been used before anywhere else?
If I understand that correctly, you want to run the fixture as setup code, so that your class has exactly 3 instances. If the fixture is function-scoped (the default) it is indeed run before each test, which will each time create 3 new instances for your class. If you want to reset your class after the test, you have to do this yourself - there is no way pytest can guess what you want to do here.
So, a working solution would be something like this:
#pytest.fixture(autouse=True)
def get_contract_numbers():
test_string_1 = ContractID()
test_string_2 = ContractID()
test_string_3 = ContractID()
yield
ContractID.contract_counter = 0
ContractID.contract_list.clear()
def test_contract_id():
...
Note that I did not yield the test strings, as you don't need them in the shown tests - if you need them, you can yield them, of course. I also added autouse=True, which makes sense if you need this for all tests, so you don't have to reference the fixture in each test.
Another possibility would be to use a session-scoped fixture. In this case the setup would be done only once. If that is what you need, you can use this instead:
#pytest.fixture(autouse=True, scope="session")
def get_contract_numbers():
test_string_1 = ContractID()
test_string_2 = ContractID()
test_string_3 = ContractID()
yield
I can't seem to get my head around mocking in Python. I have a global function:
a.py:
def has_permission(args):
ret_val = ...get-true-or-false...
return ret_val
b.py:
class MySerializer(HyperlinkedModelSerializer):
def get_fields():
fields = super().get_fields()
for f in :
if has_permission(...):
ret_val[f.name] = fields[f]
return ret_val
c.py:
class CountrySerializer(MySerializer):
class Meta:
model = Country
Question: Now i want to test c.py, but i want to mock the has_permission function that is defined in a.py, but is called in the get_fields-method of the class MySerializer that is defined in b.py ... How do i do that?
I've tried things like:
#patch('b.MySerializer.has_permission')
and
#patch('b.MySerializer.get_fields.has_permission')
and
#patch('a.has_permission')
But everything i try either just doesn't work and has_permission is still executed, or python complains about that it can't find the attribute 'has_permission'
with the patching done in:
test.py
class TestSerializerFields(TestCase):
#patch(... the above examples....)
def test_my_country_serializer():
s = CountrySerializer()
self..assertTrue(issubclass(my_serializer_fields.MyCharField, type(s.get_fields()['field1'])))
You need to patch the global in the b module:
#patch('b.has_permission')
because that's where your code looks for it.
Also see the Where to patch section of the mock documentation.
You need to patch the method where it exists at the time your test runs. If you try and patch the method where it is defined after the test code has already imported it, then the patch will have no effect. At the point where the #patch(...) executes, the test code under test has already grabbed the global method into its own module.
Here is an example:
app/util/config.py:
# This is the global method we want to mock
def is_search_enabled():
return True
app/service/searcher.py:
# Here is where that global method will be imported
# when this file is first imported
from app.util.config import is_search_enabled
class Searcher:
def __init__(self, api_service):
self._api_service = api_service
def search(self):
if not is_search_enabled():
return None
return self._api_service.perform_request('/search')
test/service/test_searcher.py:
from unittest.mock import patch, Mock
# The next line will cause the imports of `searcher.py` to execute...
from app.service.searcher import Searcher
# At this point, searcher.py has imported is_search_enabled into its module.
# If you later try and patch the method at its definition
# (app.util.config.is_search_enabled), it will have no effect because
# searcher.py won't look there again.
class MockApiService:
pass
class TestSearcher:
# By the time this executes, `is_search_enabled` has already been
# imported into `app.service.searcher`. So that is where we must
# patch it.
#patch('app.service.searcher.is_search_enabled')
def test_no_search_when_disabled(self, mock_is_search_enabled):
mock_is_search_enabled.return_value = False
mock_api_service = MockApiService()
mock_api_service.perform_request = Mock()
searcher = Searcher(mock_api_service)
results = searcher.search()
assert results is None
mock_api_service.perform_request.assert_not_called()
# (For completeness' sake, make sure the code actually works when search is enabled...)
def test_search(self):
mock_api_service = MockApiService()
mock_api_service.perform_request = mock_perform_request = Mock()
searcher = Searcher(mock_api_service)
expected_results = [1, 2, 3]
mock_perform_request.return_value = expected_results
actual_results = searcher.search()
assert actual_results == expected_results
mock_api_service.perform_request.assert_called_once_with('/search')