I wrote a decorator that halts my program with breakpoint() if a critical error happens.
def the_breakpoint_dec(func):
@functools.wraps(func):
async def my_wrapper(func):
retval=await func(*args,**kwargs)
return retval if retval else breakpoint()
return my_wrapper
I'm doing unit testing and want to make sure the breakpoint hits, without actually triggering it, so I've been trying to patch breakpoint() with the @mock.patch decorator, but it's not working. Some of the tested paths to breakpoint of the 20 or so i've tried are
@mock.patch('pdb.breakpoint')
@mock.patch('builtins.breakpoint')
@mock.patch('builtins.pdb.breakpoint')
@mock.patch('my_module.breakpoint')
@mock.patch('my_module.builtins.breakpoint')
@mock.patch('sys.breakpointhook')
@mock.patch('my_module.sys.breakpointhook')
@mock.patch('my_decorator_module.sys.breakpointhook')
@mock.patch('my_decorator_module.sys.breakpoint')
I then make the test function def and pass in the mock:
#testmodule.py
pytestmark=pytest.mark.asyncio
@mock.patch('just_another_path_to_breakpoint()_that_doesnt_work')
async def test_func_to_test(self, mock_breakpoint, some_other_mock):
mock_breakpoint.return_value=None
await decorated_func()
mock_breakpoint.assert_called() # never hit because pdb prompt opens in interpreter
module under test
#moduleundertest.py
class bar:
@the_breakpoint_dec
async def decorated_func(self):
for i in range(3)
try:
return await something_likely_to_fail()
except:
pass
Initially the breakpoint() I wanted to patch was being called from the mentioned decorator. After several hours of failures I commented out the decorator and moved breakpoint() directly into the method under test. The the patching still failed. What is the proper way to patch this builtin, and if some special thing must be done to patch it when it's in a decorator, what thing is that?