import unittest
from mock import Mock, MagicMock
import time
wait = False
def pushback_callback(self, res):
print("Received callback")
wait = True
def pushback_fcn(callback):
print("pushback_fcn")
def execute():
pushback_fcn(pushback_callback)
while wait == False:
time.sleep(5)
print("done")
#********* Test code ************#
pushback_fcn = MagicMock()
execute()
As test code never call the callback, wait value is False always hence the test code wait forever. Is there any way we can get the function pointer form MagicMock?
I created a similar code to test mocking a function that receives a callback. Install and run
pytestto test it out.The way the callback is mocked is by using
@patchandside_effect(documentation about how patch and side_effect work)The script includes 2 tests, one without the mock and one with it (changing the value sent through the callback from
donetomocked).