Skip to content

fix pytest test failures #4434

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

Merged
merged 3 commits into from
Jun 10, 2025
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
3 changes: 3 additions & 0 deletions hypothesis-python/RELEASE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
RELEASE_TYPE: patch

Improve the error message when applying |@given| to a :pypi:`pytest` fixture with pytest 8.4.0.
15 changes: 14 additions & 1 deletion hypothesis-python/src/hypothesis/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1781,7 +1781,20 @@ def run_test_as_given(test):
if inspect.isclass(test):
# Provide a meaningful error to users, instead of exceptions from
# internals that assume we're dealing with a function.
raise InvalidArgument("@given cannot be applied to a class.")
raise InvalidArgument("@given cannot be applied to a class")

if (
"_pytest" in sys.modules
and (
tuple(map(int, sys.modules["_pytest"].__version__.split(".")[:2]))
>= (8, 4)
)
and isinstance(
test, sys.modules["_pytest"].fixtures.FixtureFunctionDefinition
)
): # pragma: no cover # covered by pytest/test_fixtures, but not by cover/
raise InvalidArgument("@given cannot be applied to a pytest fixture")

given_arguments = tuple(_given_arguments)
given_kwargs = dict(_given_kwargs)

Expand Down
14 changes: 8 additions & 6 deletions hypothesis-python/tests/pytest/test_capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ def test_output_emitting_unicode(testdir, monkeypatch):
assert result.ret == 0


def get_line_num(token, result, skip_n=0):
def get_line_num(token, lines, skip_n=0):
skipped = 0
for i, line in enumerate(result.stdout.lines):
for i, line in enumerate(lines):
if token in line:
if skip_n == skipped:
return i
Expand All @@ -96,12 +96,14 @@ def test_healthcheck_traceback_is_hidden(x):
def test_healthcheck_traceback_is_hidden(testdir, monkeypatch):
monkeypatch.delenv("CI", raising=False)
script = testdir.makepyfile(TRACEBACKHIDE_HEALTHCHECK)
result = testdir.runpytest(script, "--verbose")
lines = testdir.runpytest(script, "--verbose").stdout.lines
def_token = "__ test_healthcheck_traceback_is_hidden __"
timeout_token = ": FailedHealthCheck"
def_line = get_line_num(def_token, result)
timeout_line = get_line_num(timeout_token, result)
assert timeout_line - def_line == 9
def_line = get_line_num(def_token, lines)
timeout_line = get_line_num(timeout_token, lines)
# 10 on pytest 8.4.0 combined with py3{9, 10} or 3.13 free-threading (but
# not with 3.13 normal??)
assert timeout_line - def_line in {9, 10}


COMPOSITE_IS_NOT_A_TEST = """
Expand Down
18 changes: 13 additions & 5 deletions hypothesis-python/tests/pytest/test_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,21 @@ def test(x):
pass
"""

pytest_version = tuple(map(int, pytest.__version__.split(".")[:2]))


def assert_outcomes(result, *, errors=0, failed=0):
kwargs = {"errors" if pytest_version[0] > 5 else "error": errors}
result.assert_outcomes(failed=failed, **kwargs)


def test_given_fails_if_already_decorated_with_fixture(testdir):
script = testdir.makepyfile(TESTSCRIPT_FIXTURE_THEN_GIVEN)
testdir.runpytest(script).assert_outcomes(failed=1)
result = testdir.runpytest(script)
if pytest_version[:2] >= (8, 4):
assert_outcomes(result, errors=1)
else:
assert_outcomes(result, failed=1)


TESTSCRIPT_GIVEN_THEN_FIXTURE = """
Expand All @@ -206,7 +217,4 @@ def test(x):

def test_fixture_errors_if_already_decorated_with_given(testdir):
script = testdir.makepyfile(TESTSCRIPT_GIVEN_THEN_FIXTURE)
if int(pytest.__version__.split(".")[0]) > 5:
testdir.runpytest(script).assert_outcomes(errors=1)
else:
testdir.runpytest(script).assert_outcomes(error=1)
assert_outcomes(testdir.runpytest(script), errors=1)
2 changes: 1 addition & 1 deletion requirements/coverage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ ptyprocess==0.7.0
# via pexpect
pyarrow==19.0.1
# via -r requirements/coverage.in
pytest==8.3.5
pytest==8.4.0
# via
# -r requirements/test.in
# pytest-cov
Expand Down
2 changes: 1 addition & 1 deletion requirements/crosshair.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ ptyprocess==0.7.0
# via pexpect
pygls==1.3.1
# via crosshair-tool
pytest==8.3.5
pytest==8.4.0
# via
# -r requirements/test.in
# pytest-xdist
Expand Down
2 changes: 1 addition & 1 deletion requirements/fuzzing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ pyarrow==19.0.1
# via -r requirements/coverage.in
pygments==2.19.1
# via rich
pytest==8.3.5
pytest==8.4.0
# via
# -r requirements/test.in
# hypofuzz
Expand Down
2 changes: 1 addition & 1 deletion requirements/test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pluggy==1.5.0
# via pytest
ptyprocess==0.7.0
# via pexpect
pytest==8.3.5
pytest==8.4.0
# via
# -r requirements/test.in
# pytest-xdist
Expand Down
2 changes: 1 addition & 1 deletion requirements/tools.txt
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ pyproject-hooks==1.2.0
# pip-tools
pyright==1.1.399
# via -r requirements/tools.in
pytest==8.3.5
pytest==8.4.0
# via
# -r requirements/test.in
# pytest-xdist
Expand Down