tl;dr: I know a code block I am testing with unittest is being executed because of a print statement, but a function directly after the print statement is failing an assert_called check. When running the code, the function is being called and exectued as expected. Unclear why test is failing.
Code in function being tested:
from path.to import deliver_error
def handle_error(ARGS):
if HANDLE_ERROR_BOOL:
print('calling deliver_error')
deliver_error(ARGS)
else:
# do something else
Test code:
class TestSampleRegistration(unittest.TestCase):
def setUp():
# mock deliver_error
mock_deliver_error_patch = patch("path.to.deliver_error", autospec=True)
self.mock_deliver_error = mock_deliver_error_patch.start()
self.addCleanup(mock_deliver_error_patch.stop)
def test_handle_error(self):
main.HANDLE_ERROR_BOOL = True
handle_error(ARGS)
self.mock_deliver_error.assert_called() # point of failure
Assertion failure:
AssertionError: Expected 'deliver_error' to have been called.
Captured stdout:
Captured stdout for test_main.MyTestClass.test_handle_error
calling deliver_error
I KNOW:
- HANDLE_ERROR_BOOL is correctly set (print statement executed and showing up in stdout)
- deliver_error should be getting called since there is nothing between the print statement and the deliver_error call, and the print statement is being executed
- when running the actual code the function is being executed as expected
So why is the assert_called test failing? Help very much appreciated, thank you!