Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pytest workaround for ExceptionGroup display #3432

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"