What you can do is put your mocks in a new Mock() object and check the layout of the calls of the new layout. Maybe an example is easier to understand:
>>> from unittest.mock import * >>> m0, m1 = Mock(), Mock() >>> m = Mock() >>> m.m0, m.m1 = m0, m1 >>> m0() <Mock name='mock.m0()' id='140660445334224'> >>> m1() <Mock name='mock.m1()' id='140660445334608'> >>> m.mock_calls [call.m0(), call.m1()]
Ok, now we are in a good position: we can just check the calls to m to check the correct order:
>>> m.assert_has_calls([call.m0(), call.m1()]) >>> m.assert_has_calls([call.m1(), call.m0()]) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/lib/python2.7/dist-packages/mock.py", line 863, in assert_has_calls 'Actual: %r' % (calls, self.mock_calls) AssertionError: Calls not found. Expected: [call.m1(), call.m0()] Actual: [call.m0(), call.m1()]
As we want, the first pass and the reverse order will not pass.
When you use patch , just take the layouts returned by the patches and put them in the new Mock() container. Make your check on a container that records orders for calls from children.
Michele d'amico
source share