def test_filter_traceback_path_no_longer_valid(self, testdir) -> None: """Test that filter_traceback() works with the fact that _pytest._code.code.Code.path attribute might return an str object. In this case, one of the files in the traceback no longer exists. This fixes #1133. """ from _pytest._code import filter_traceback testdir.syspathinsert() testdir.makepyfile( filter_traceback_entry_as_str=""" def foo(): raise ValueError """ ) try: import filter_traceback_entry_as_str filter_traceback_entry_as_str.foo() except ValueError: _, _, tb = sys.exc_info() assert tb is not None testdir.tmpdir.join("filter_traceback_entry_as_str.py").remove() traceback = _pytest._code.Traceback(tb) assert isinstance(traceback[-1].path, str) assert filter_traceback(traceback[-1])
def filter_traceback_for_conftest_import_failure(entry) -> bool: """filters tracebacks entries which point to pytest internals or importlib. Make a special case for importlib because we use it to import test modules and conftest files in _pytest.pathlib.import_path. """ return filter_traceback(entry) and "importlib" not in str(entry.path).split(os.sep)
def test_filter_traceback_generated_code(self) -> None: """Test that filter_traceback() works with the fact that _pytest._code.code.Code.path attribute might return an str object. In this case, one of the entries on the traceback was produced by dynamically generated code. See: https://bitbucket.org/pytest-dev/py/issues/71 This fixes #995. """ from _pytest._code import filter_traceback try: ns = {} # type: Dict[str, Any] exec("def foo(): raise ValueError", ns) ns["foo"]() except ValueError: _, _, tb = sys.exc_info() assert tb is not None traceback = _pytest._code.Traceback(tb) assert isinstance(traceback[-1].path, str) assert not filter_traceback(traceback[-1])