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

Function test_testcase_custom_exception_info

testing/test_unittest.py:377–418  ·  view source on GitHub ↗
(pytester: Pytester, type: str)

Source from the content-addressed store, hash-verified

375
376@pytest.mark.parametrize("type", ["Error", "Failure"])
377def test_testcase_custom_exception_info(pytester: Pytester, type: str) -> None:
378 pytester.makepyfile(
379 """
380 from typing import Generic, TypeVar
381 from unittest import TestCase
382 import pytest, _pytest._code
383
384 class MyTestCase(TestCase):
385 def run(self, result):
386 excinfo = pytest.raises(ZeroDivisionError, lambda: 0/0)
387 # We fake an incompatible exception info.
388 class FakeExceptionInfo(Generic[TypeVar("E")]):
389 def __init__(self, *args, **kwargs):
390 mp.undo()
391 raise TypeError()
392 @classmethod
393 def from_current(cls):
394 return cls()
395 @classmethod
396 def from_exc_info(cls, *args, **kwargs):
397 return cls()
398 mp = pytest.MonkeyPatch()
399 mp.setattr(_pytest._code, 'ExceptionInfo', FakeExceptionInfo)
400 try:
401 excinfo = excinfo._excinfo
402 result.add%(type)s(self, excinfo)
403 finally:
404 mp.undo()
405
406 def test_hello(self):
407 pass
408 """
409 % locals()
410 )
411 result = pytester.runpytest()
412 result.stdout.fnmatch_lines(
413 [
414 "NOTE: Incompatible Exception Representation*",
415 "*ZeroDivisionError*",
416 "*1 failed*",
417 ]
418 )
419
420
421def test_testcase_totally_incompatible_exception_info(pytester: Pytester) -> None:

Callers

nothing calls this directly

Calls 3

fnmatch_linesMethod · 0.80
makepyfileMethod · 0.45
runpytestMethod · 0.45

Tested by

no test coverage detected