Skip to content

Commit

Permalink
Merge pull request #3432 from Zac-HD/fix-exceptiongroup-display-worka…
Browse files Browse the repository at this point in the history
…round

Fix pytest workaround for `ExceptionGroup` display
  • Loading branch information
Zac-HD authored Aug 10, 2022
2 parents 81fb173 + df1896e commit ae171e0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
5 changes: 5 additions & 0 deletions hypothesis-python/RELEASE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
RELEASE_TYPE: patch

This patch fixes our workaround for `a pytest bug where the inner exceptions in
an ExceptionGroup are not displayed <https://github.com/pytest-dev/pytest/issues/9159>`__
(:issue:`3430`).
12 changes: 5 additions & 7 deletions hypothesis-python/src/_hypothesis_pytestplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,6 @@ def pytest_configure(config):
pass
core.global_force_seed = seed

core.pytest_shows_exceptiongroups = (
sys.version_info[:2] >= (3, 11)
## See https://github.com/pytest-dev/pytest/issues/9159
# or pytest_version >= (7, 2) # TODO: fill in correct version here
or config.getoption("tbstyle", "auto") == "native"
)

@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_call(item):
__tracebackhide__ = True
Expand All @@ -195,6 +188,11 @@ def pytest_runtest_call(item):
from hypothesis import core
from hypothesis.internal.detection import is_hypothesis_test

## See https://github.com/pytest-dev/pytest/issues/9159
# TODO: add `pytest_version >= (7, 2) or` once the issue above is fixed.
core.pytest_shows_exceptiongroups = (
item.config.getoption("tbstyle", "auto") == "native"
)
core.running_under_pytest = True

if not is_hypothesis_test(item.obj):
Expand Down
23 changes: 23 additions & 0 deletions hypothesis-python/tests/pytest/test_reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at https://mozilla.org/MPL/2.0/.

import pytest

pytest_plugins = "pytester"


Expand All @@ -33,3 +35,24 @@ def test_runs_reporting_hook(testdir):
assert "Captured stdout call" not in out
assert "Falsifying example" in out
assert result.ret != 0


TEST_EXCEPTIONGROUP = """
from hypothesis import given, strategies as st
@given(x=st.booleans())
def test_fuzz_sorted(x):
raise ValueError if x else TypeError
"""


@pytest.mark.parametrize("tb", ["auto", "long", "short", "native"])
def test_no_missing_reports(testdir, tb):
script = testdir.makepyfile(TEST_EXCEPTIONGROUP)
result = testdir.runpytest(script, f"--tb={tb}")
out = "\n".join(result.stdout.lines)
# If the False case is missing, that means we're not printing exception info.
# See https://github.com/HypothesisWorks/hypothesis/issues/3430 With --tb=native,
# we should show the full ExceptionGroup with *both* errors.
assert "x=False" in out
assert "x=True" in out or tb != "native"

0 comments on commit ae171e0

Please sign in to comment.