Skip to content

Commit

Permalink
Hide pytest helper from tracebacks
Browse files Browse the repository at this point in the history
Inspired by the extremely verbose traceback show in https://stackoverflow.com/q/72792829
  • Loading branch information
Zac-HD committed Jul 3, 2022
1 parent 076231d commit 5a4ef42
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
9 changes: 7 additions & 2 deletions hypothesis-python/src/_hypothesis_pytestplugin.py
Expand Up @@ -180,6 +180,7 @@ def pytest_configure(config):

@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_call(item):
__tracebackhide__ = True
if not (hasattr(item, "obj") and "hypothesis" in sys.modules):
yield
return
Expand All @@ -192,10 +193,14 @@ def pytest_runtest_call(item):
if not is_hypothesis_test(item.obj):
# If @given was not applied, check whether other hypothesis
# decorators were applied, and raise an error if they were.
# We add this frame of indirection to enable __tracebackhide__.
def raise_hypothesis_usage_error(msg):
raise InvalidArgument(msg)

if getattr(item.obj, "is_hypothesis_strategy_function", False):
from hypothesis.errors import InvalidArgument

raise InvalidArgument(
raise_hypothesis_usage_error(
f"{item.nodeid} is a function that returns a Hypothesis strategy, "
"but pytest has collected it as a test function. This is useless "
"as the function body will never be executed. To define a test "
Expand All @@ -211,7 +216,7 @@ def pytest_runtest_call(item):
if hasattr(item.obj, attribute):
from hypothesis.errors import InvalidArgument

raise InvalidArgument(message % (name,))
raise_hypothesis_usage_error(message % (name,))
yield
else:
from hypothesis import HealthCheck, settings
Expand Down
4 changes: 3 additions & 1 deletion hypothesis-python/tests/pytest/test_checks.py
Expand Up @@ -33,4 +33,6 @@ def test_repro_without_given_fails():

def test_decorators_without_given_should_fail(testdir):
script = testdir.makepyfile(TEST_DECORATORS_ALONE)
testdir.runpytest(script).assert_outcomes(failed=4)
result = testdir.runpytest(script)
result.assert_outcomes(failed=4)
assert "pytest_runtest_call" not in "\n".join(result.outlines)

0 comments on commit 5a4ef42

Please sign in to comment.