Skip to content

Commit d3d10a5

Browse files
committed
Testing exception on patch as context manager
Adding a new test to confirm that patch raises an exception when used as a context manager
1 parent 6587f79 commit d3d10a5

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

tests/test_pytest_mock.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,7 @@ def test_get_random_number(mocker):
723723
assert "RuntimeError" not in result.stderr.str()
724724

725725

726-
def test_abort_context_manager(mocker):
726+
def test_abort_patch_object_context_manager(mocker):
727727
class A(object):
728728
def doIt(self):
729729
return False
@@ -740,3 +740,16 @@ def doIt(self):
740740
)
741741

742742
assert str(excinfo.value) == expected_error_msg
743+
744+
745+
def test_abort_patch_context_manager(mocker):
746+
with pytest.raises(ValueError) as excinfo:
747+
with mocker.patch("some_package"):
748+
pass
749+
750+
expected_error_msg = (
751+
"Using mocker in a with context is not supported. "
752+
"https://github.com/pytest-dev/pytest-mock#note-about-usage-as-context-manager"
753+
)
754+
755+
assert str(excinfo.value) == expected_error_msg

0 commit comments

Comments
 (0)