From 290f04d11e8552573e1d7f1f739c9389cbdf8a12 Mon Sep 17 00:00:00 2001 From: Zac Hatfield-Dodds Date: Sun, 3 Jul 2022 01:32:35 -0700 Subject: [PATCH] Hide pytest helper from tracebacks Inspired by the extremely verbose traceback show in https://stackoverflow.com/q/72792829 --- hypothesis-python/src/_hypothesis_pytestplugin.py | 9 +++++++-- hypothesis-python/tests/pytest/test_checks.py | 4 +++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/hypothesis-python/src/_hypothesis_pytestplugin.py b/hypothesis-python/src/_hypothesis_pytestplugin.py index bc91b60302..0155c9f232 100644 --- a/hypothesis-python/src/_hypothesis_pytestplugin.py +++ b/hypothesis-python/src/_hypothesis_pytestplugin.py @@ -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 @@ -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 " @@ -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 diff --git a/hypothesis-python/tests/pytest/test_checks.py b/hypothesis-python/tests/pytest/test_checks.py index 48b8e629e3..911b142b38 100644 --- a/hypothesis-python/tests/pytest/test_checks.py +++ b/hypothesis-python/tests/pytest/test_checks.py @@ -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)