I am unit testing the following functions:
import uuid
def process_name(id, name, weight):
print('process_item:', id, name, weight)
def process_list(names):
for (name, weight) in names:
id = uuid.uuid4()
process_name(id, name, weight)
My unit test looks as follows:
import unittest
from mock import patch, call
import SomePackage.SomeModule
class MyTestCase(unittest.TestCase):
def test_something(self):
items = [('Joe', 190), ('Dan', 200)]
with patch('SomePackage.SomeModule.process_name') as mock_process_name:
SomePackage.SomeModule.process_list(items)
I cannot match the whole mock_calls
thing, because the first parameter submitted to it is a guid, and as such it will be different every time I call the function:
print(mock_process_name.mock_calls)
[call(UUID('some randomish guid'), 'Joe', 190),
call(UUID('some other guid'), 'Dan', 200)]
I want to extract the parameters, and only match the non-volatile ones:
print(mock_process_name.mock_calls[0][1][1:])
print(mock_process_name.mock_calls[1][1][1:])
('Joe', 190)
('Dan', 200)
I know that I can also mock the thing which returns guids, and provide a fixed list of values in its side_effect. However, I feel quite lazy needing to mock too many things like uuid.uuid4()
, datetime.now()
and such. This is why I am looking for an alternative to mocking each and every volatile function.
Is there a more readable alternative to mock_calls[0][1][1:]
?
Aucun commentaire:
Enregistrer un commentaire