I have a piece of unit tests code where at some place unittest.mock.Mock
is used and I'd like to go through it step by step using debugger. I am using pdbpp
, but it does not matter in this situation, standard pdb
and ipdb
fail on same issue. When debugger encounters Mock()
call, whole process crashes with traceback below.
Example to replicate (simply run pytest bug_example.py
and then use s
(as step) for going through it).
My question is - why is it happening and if I am able to use both debugger and mock.
import unittest.mock
def function_that_uses_mock():
mock = unittest.mock.Mock()
# anything else
foo = 42
return foo
def test_something():
breakpoint() # can be "import ipdb; ipdb.set_trace()" or anything else
assert function_that_uses_mock() == 42
# anything else
> assert function_that_uses_mock() == 42
bug_example.py:13:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
bug_example.py:4: in function_that_uses_mock
mock = unittest.mock.Mock()
/usr/lib/python3.10/unittest/mock.py:417: in __new__
return instance
/usr/lib/python3.10/bdb.py:94: in trace_dispatch
return self.dispatch_return(frame, arg)
/usr/lib/python3.10/bdb.py:153: in dispatch_return
self.user_return(frame, arg)
/usr/lib/python3.10/pdb.py:294: in user_return
self.interaction(frame, None)
/home/user/.local/lib/python3.10/site-packages/pdb.py:224: in interaction
self.print_stack_entry(self.stack[self.curindex])
/home/user/.local/lib/python3.10/site-packages/pdb.py:893: in print_stack_entry
print(self.format_stack_entry(frame_lineno, prompt_prefix), file=self.stdout)
/home/user/.local/lib/python3.10/site-packages/pdb.py:399: in format_stack_entry
entry = super(Pdb, self).format_stack_entry(frame_lineno, lprefix)
/usr/lib/python3.10/bdb.py:572: in format_stack_entry
s += reprlib.repr(rv)
/usr/lib/python3.10/reprlib.py:52: in repr
return self.repr1(x, self.maxlevel)
/usr/lib/python3.10/reprlib.py:62: in repr1
return self.repr_instance(x, level)
/usr/lib/python3.10/reprlib.py:143: in repr_instance
return '<%s instance at %#x>' % (x.__class__.__name__, id(x))
/usr/lib/python3.10/unittest/mock.py:632: in __getattr__
elif self._mock_methods is not None:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <[AttributeError('_mock_methods') raised in repr()] Mock object at 0x7fe6115b1ba0>, name = '_mock_methods'
def __getattr__(self, name):
if name in {'_mock_methods', '_mock_unsafe'}:
> raise AttributeError(name)
E AttributeError: _mock_methods
/usr/lib/python3.10/unittest/mock.py:631: AttributeError