def test_store_except_info_on_error() -> None: """Test that upon test failure, the exception info is stored on sys.last_traceback and friends.""" # Simulate item that might raise a specific exception, depending on `raise_error` class var class ItemMightRaise: nodeid = "item_that_raises" raise_error = True def runtest(self): if self.raise_error: raise IndexError("TEST") try: runner.pytest_runtest_call(ItemMightRaise()) # type: ignore[arg-type] except IndexError: pass # Check that exception info is stored on sys assert sys.last_type is IndexError assert isinstance(sys.last_value, IndexError) assert sys.last_value.args[0] == "TEST" assert sys.last_traceback # The next run should clear the exception info stored by the previous run ItemMightRaise.raise_error = False runner.pytest_runtest_call(ItemMightRaise()) # type: ignore[arg-type] assert not hasattr(sys, "last_type") assert not hasattr(sys, "last_value") assert not hasattr(sys, "last_traceback")
def test_store_except_info_on_error(): """ Test that upon test failure, the exception info is stored on sys.last_traceback and friends. """ # Simulate item that might raise a specific exception, depending on `raise_error` class var class ItemMightRaise(object): nodeid = 'item_that_raises' raise_error = True def runtest(self): if self.raise_error: raise IndexError('TEST') try: runner.pytest_runtest_call(ItemMightRaise()) except IndexError: pass # Check that exception info is stored on sys assert sys.last_type is IndexError assert sys.last_value.args[0] == 'TEST' assert sys.last_traceback # The next run should clear the exception info stored by the previous run ItemMightRaise.raise_error = False runner.pytest_runtest_call(ItemMightRaise()) assert sys.last_type is None assert sys.last_value is None assert sys.last_traceback is None
def test_store_except_info_on_error(): """ Test that upon test failure, the exception info is stored on sys.last_traceback and friends. """ # Simulate item that might raise a specific exception, depending on `raise_error` class var class ItemMightRaise(object): nodeid = "item_that_raises" raise_error = True def runtest(self): if self.raise_error: raise IndexError("TEST") try: runner.pytest_runtest_call(ItemMightRaise()) except IndexError: pass # Check that exception info is stored on sys assert sys.last_type is IndexError assert sys.last_value.args[0] == "TEST" assert sys.last_traceback # The next run should clear the exception info stored by the previous run ItemMightRaise.raise_error = False runner.pytest_runtest_call(ItemMightRaise()) assert sys.last_type is None assert sys.last_value is None assert sys.last_traceback is None
def pytest_runtest_call(item): # Beautiful names # # test_filename = item.module.__file__ # test_name = item.name # test_fullname = f"{test_filename}::{test_name}" # elapsed time start = time() runner.pytest_runtest_call(item) elapsed_time = time() - start # lines of code item_source_code = inspect.getsource(item._getobj()) lines_of_code = len(item_source_code.split('\n')) - 1 # check conditionals and loops root = ast.parse(item_source_code) ifs_used = len( [node for node in ast.walk(root) if isinstance(node, ast.If)]) for_used = len( [node for node in ast.walk(root) if isinstance(node, ast.For)]) while_used = len( [node for node in ast.walk(root) if isinstance(node, ast.While)]) # write measures MEASURES[item.name] = { "LOC": lines_of_code, "elapsed_time": elapsed_time, "ifs_used": ifs_used, "for_used": for_used, "while_used": while_used, }
def test_store_except_info_on_eror(): """ Test that upon test failure, the exception info is stored on sys.last_traceback and friends. """ # Simulate item that raises a specific exception class ItemThatRaises: def runtest(self): raise IndexError('TEST') try: runner.pytest_runtest_call(ItemThatRaises()) except IndexError: pass # Check that exception info is stored on sys assert sys.last_type is IndexError assert sys.last_value.args[0] == 'TEST' assert sys.last_traceback