Skip to content

Commit 0df91f9

Browse files
committed
Add spy exception catching
1 parent 25de852 commit 0df91f9

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

tests/test_pytest_mock.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,24 @@ def bar(self, arg):
243243
assert spy.return_value == 20
244244

245245

246+
def test_instance_method_spy_exception(mocker):
247+
excepted_message = "foo"
248+
class Foo(object):
249+
def bar(self, arg):
250+
raise Exception(excepted_message)
251+
252+
foo = Foo()
253+
other = Foo()
254+
spy = mocker.spy(foo, "bar")
255+
256+
with pytest.raises(Exception) as exc_info:
257+
foo.bar(10)
258+
assert str(exc_info.value) == excepted_message
259+
260+
foo.bar.assert_called_once_with(arg=10)
261+
assert spy.return_value == exc_info.value
262+
263+
246264
@skip_pypy
247265
def test_instance_method_by_class_spy(mocker):
248266
class Foo(object):

0 commit comments

Comments
 (0)