MCPcopy Create free account
hub / github.com/pytest-dev/pytest / test_store_except_info_on_error

Function test_store_except_info_on_error

testing/test_runner.py:880–907  ·  view source on GitHub ↗

Test that upon test failure, the exception info is stored on sys.last_traceback and friends.

()

Source from the content-addressed store, hash-verified

878
879
880def test_store_except_info_on_error() -> None:
881 """Test that upon test failure, the exception info is stored on
882 sys.last_traceback and friends."""
883 # Simulate item that might raise a specific exception, depending on `raise_error` class var
884 class ItemMightRaise:
885 nodeid = "item_that_raises"
886 raise_error = True
887
888 def runtest(self):
889 if self.raise_error:
890 raise IndexError("TEST")
891
892 try:
893 runner.pytest_runtest_call(ItemMightRaise()) # type: ignore[arg-type]
894 except IndexError:
895 pass
896 # Check that exception info is stored on sys
897 assert sys.last_type is IndexError
898 assert isinstance(sys.last_value, IndexError)
899 assert sys.last_value.args[0] == "TEST"
900 assert sys.last_traceback
901
902 # The next run should clear the exception info stored by the previous run
903 ItemMightRaise.raise_error = False
904 runner.pytest_runtest_call(ItemMightRaise()) # type: ignore[arg-type]
905 assert not hasattr(sys, "last_type")
906 assert not hasattr(sys, "last_value")
907 assert not hasattr(sys, "last_traceback")
908
909
910def test_current_test_env_var(pytester: Pytester, monkeypatch: MonkeyPatch) -> None:

Callers

nothing calls this directly

Calls 2

ItemMightRaiseClass · 0.85
pytest_runtest_callMethod · 0.45

Tested by

no test coverage detected