python - How to mock calls to function that receives mutable object as parameter? -



python - How to mock calls to function that receives mutable object as parameter? -

consider example:

def func_b(a): print def func_a(): = [-1] in xrange(0, 2): a[0] = func_b(a)

and test function tries test func_a , mocks func_b:

import mock mock import phone call def test_a(): datatransform.test import func_a mock.patch('datatransform.test.func_b', autospec=true) func_b_mock: func_a() func_b_mock.assert_has_calls([call(0), call(1)])

after func_a has executed seek test if func_a made right calls func_b, since in loop mutating list in end get:

assertionerror: calls not found. expected: [call(0), call(1)] actual: [call([1]), call([1])]

the next works (the importing mock unittest python 3 thing, , module func_a , func_b are):

import mock mock import phone call import re-create class modifiedmagicmock(mock.magicmock): def _mock_call(_mock_self, *args, **kwargs): homecoming super(modifiedmagicmock, _mock_self)._mock_call(*copy.deepcopy(args), **copy.deepcopy(kwargs))

this inherits magicmock, , redefines phone call behaviour deepcopy arguments , keyword arguments.

def test_a(): module import func_a mock.patch('module.func_b', new_callable=modifiedmagicmock) func_b_mock: func_a() func_b_mock.assert_has_calls([call([0]), call([1])])

you can pass new class patch using new_callable parameter, cannot co-exist autospec. note function calls func_b list, call(0), call(1) has changed call([0]), call([1]). when run calling test_a, nil (passes).

now cannot utilize both new_callable , autospec because new_callable generic mill in our case magicmock override. autospeccing cool mock's feature, don't want lose it.

what need replace magicmock modifiedmagicmock our test: want avoid alter magicmock behavior tests... dangerous. have tool , patch, used new argument replace destination.

in case utilize decorators avoid much indentation , create more readable:

@mock.patch('module.func_b', autospec=true) @mock.patch("mock.magicmock", new=modifiedmagicmock) def test_a(func_b_mock): module import func_a func_a() func_b_mock.assert_has_calls([call([0]), call([1])])

or:

@mock.patch("mock.magicmock", new=modifiedmagicmock) def test_a(): mock.patch('module.func_b') func_b_mock: module import func_a func_a() func_b_mock.assert_has_calls([call([0]), call([1])])

python mocking

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -