From 371939fb86b0b0c0e82384195bc45d9ac2b9cc63 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Sat, 15 Feb 2020 15:34:21 +0200 Subject: [PATCH 1/2] Avoid getattr when dispatching pytest_runtest_* hooks Using getattr doesn't work with typing, and also breaks grep. It took me a while to find where these hooks are called. --- src/_pytest/runner.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/_pytest/runner.py b/src/_pytest/runner.py index e10e4d8bd..e53c72e2c 100644 --- a/src/_pytest/runner.py +++ b/src/_pytest/runner.py @@ -25,6 +25,7 @@ from _pytest.outcomes import TEST_OUTCOME if TYPE_CHECKING: from typing import Type + from typing_extensions import Literal # # pytest plugin hooks @@ -181,7 +182,9 @@ def pytest_report_teststatus(report): # Implementation -def call_and_report(item, when, log=True, **kwds): +def call_and_report( + item, when: "Literal['setup', 'call', 'teardown']", log=True, **kwds +): call = call_runtest_hook(item, when, **kwds) hook = item.ihook report = hook.pytest_runtest_makereport(item=item, call=call) @@ -200,9 +203,15 @@ def check_interactive_exception(call, report): ) -def call_runtest_hook(item, when, **kwds): - hookname = "pytest_runtest_" + when - ihook = getattr(item.ihook, hookname) +def call_runtest_hook(item, when: "Literal['setup', 'call', 'teardown']", **kwds): + if when == "setup": + ihook = item.ihook.pytest_runtest_setup + elif when == "call": + ihook = item.ihook.pytest_runtest_call + elif when == "teardown": + ihook = item.ihook.pytest_runtest_teardown + else: + assert False, "Unhandled runtest hook case: {}".format(when) reraise = (Exit,) # type: Tuple[Type[BaseException], ...] if not item.config.getoption("usepdb", False): reraise += (KeyboardInterrupt,) From 7155b2277ce6dc077c3e3131a9dddea2c0c4922c Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Sat, 15 Feb 2020 17:07:32 +0200 Subject: [PATCH 2/2] Ignore "assert False" statements in coverage --- .coveragerc | 1 + 1 file changed, 1 insertion(+) diff --git a/.coveragerc b/.coveragerc index fc2321e87..b06629a8a 100644 --- a/.coveragerc +++ b/.coveragerc @@ -24,5 +24,6 @@ exclude_lines = \#\s*pragma: no cover ^\s*raise NotImplementedError\b ^\s*return NotImplemented\b + ^\s*assert False(,|$) ^\s*if TYPE_CHECKING: