I need to check the sequence of calling of some methods of a class.
I had this need while I was developing using TDD (test driven development), so when I was writing the test for method_1()
I would like to be sure that it calls some methods in an precise order.
I suppose that my production class A
is stored in the following file class_a.py:
class A:
__lock = None
def __init__(self, lock):
self.__lock = lock
def method_1(self):
self.__lock.acquire()
self.__atomic_method_1()
self.__lock.release()
def __atomic_method_1(self):
pass
The __lock
attribute is an instance of class threading.Lock
and is use to reach a thread safe execution of __atomic_method_1()
.
I need to write a unit test that checks the sequence of calling of the methods of the class A
when it is invoked the method_1()
.
The check must verify that method_1()
calls:
- as first method:
self.__lock.acquire()
- after that it calls
self.__atomic_method_1()
- the last method called is
self.__lock.release()
My need comes from wanting to make sure that the __atomic_method_1()
method runs in a multithreaded context without being interrupted.
A useful hint but not enough
This is a very useful link, but it doesn't solve my problem. The solution provided by the link is perfect to verify the calling order of a sequence of functions invoked by an other function. The link shows an example where the function under test and the functions called are all contained in a file called module_under_test
.
In my case, however, I have to verify the calling sequence of methods of a class and this difference prevents to use the solution provided by the link.
A trace that starts from the suggestion
However I have tried to develop the unit test referring to the link and in this way I have prepared a trace of the test file that I can show below:
import unittest
from unittest import mock
from class_a import A
import threading
class TestCallOrder(unittest.TestCase):
def test_call_order(self):
# create an instance of the SUT (note the parameter threading.Lock())
sut = A(threading.Lock())
# prepare the MOCK object
source_mock = ...
with patch(...)
# prepare the expected values
expected = [...]
# run the code-under-test (method_1()).
sut.method_1()
# Check the order calling
self.assertEqual(expected, source_mock.mock_calls)
if __name__ == '__main__':
unittest.main()
But I'm not able to complete the method test_call_order()
.
Thanks