Skip to content

Commit ad76688

Browse files
authored
Merge pull request #4434 from tybug/pytest-compat
fix pytest test failures
2 parents 609acd8 + 8cc068a commit ad76688

File tree

9 files changed

+43
-17
lines changed

9 files changed

+43
-17
lines changed

hypothesis-python/RELEASE.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
RELEASE_TYPE: patch
2+
3+
Improve the error message when applying |@given| to a :pypi:`pytest` fixture with pytest 8.4.0.

hypothesis-python/src/hypothesis/core.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1781,7 +1781,20 @@ def run_test_as_given(test):
17811781
if inspect.isclass(test):
17821782
# Provide a meaningful error to users, instead of exceptions from
17831783
# internals that assume we're dealing with a function.
1784-
raise InvalidArgument("@given cannot be applied to a class.")
1784+
raise InvalidArgument("@given cannot be applied to a class")
1785+
1786+
if (
1787+
"_pytest" in sys.modules
1788+
and (
1789+
tuple(map(int, sys.modules["_pytest"].__version__.split(".")[:2]))
1790+
>= (8, 4)
1791+
)
1792+
and isinstance(
1793+
test, sys.modules["_pytest"].fixtures.FixtureFunctionDefinition
1794+
)
1795+
): # pragma: no cover # covered by pytest/test_fixtures, but not by cover/
1796+
raise InvalidArgument("@given cannot be applied to a pytest fixture")
1797+
17851798
given_arguments = tuple(_given_arguments)
17861799
given_kwargs = dict(_given_kwargs)
17871800

hypothesis-python/tests/pytest/test_capture.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ def test_output_emitting_unicode(testdir, monkeypatch):
7070
assert result.ret == 0
7171

7272

73-
def get_line_num(token, result, skip_n=0):
73+
def get_line_num(token, lines, skip_n=0):
7474
skipped = 0
75-
for i, line in enumerate(result.stdout.lines):
75+
for i, line in enumerate(lines):
7676
if token in line:
7777
if skip_n == skipped:
7878
return i
@@ -96,12 +96,14 @@ def test_healthcheck_traceback_is_hidden(x):
9696
def test_healthcheck_traceback_is_hidden(testdir, monkeypatch):
9797
monkeypatch.delenv("CI", raising=False)
9898
script = testdir.makepyfile(TRACEBACKHIDE_HEALTHCHECK)
99-
result = testdir.runpytest(script, "--verbose")
99+
lines = testdir.runpytest(script, "--verbose").stdout.lines
100100
def_token = "__ test_healthcheck_traceback_is_hidden __"
101101
timeout_token = ": FailedHealthCheck"
102-
def_line = get_line_num(def_token, result)
103-
timeout_line = get_line_num(timeout_token, result)
104-
assert timeout_line - def_line == 9
102+
def_line = get_line_num(def_token, lines)
103+
timeout_line = get_line_num(timeout_token, lines)
104+
# 10 on pytest 8.4.0 combined with py3{9, 10} or 3.13 free-threading (but
105+
# not with 3.13 normal??)
106+
assert timeout_line - def_line in {9, 10}
105107

106108

107109
COMPOSITE_IS_NOT_A_TEST = """

hypothesis-python/tests/pytest/test_fixtures.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,21 @@ def test(x):
187187
pass
188188
"""
189189

190+
pytest_version = tuple(map(int, pytest.__version__.split(".")[:2]))
191+
192+
193+
def assert_outcomes(result, *, errors=0, failed=0):
194+
kwargs = {"errors" if pytest_version[0] > 5 else "error": errors}
195+
result.assert_outcomes(failed=failed, **kwargs)
196+
190197

191198
def test_given_fails_if_already_decorated_with_fixture(testdir):
192199
script = testdir.makepyfile(TESTSCRIPT_FIXTURE_THEN_GIVEN)
193-
testdir.runpytest(script).assert_outcomes(failed=1)
200+
result = testdir.runpytest(script)
201+
if pytest_version[:2] >= (8, 4):
202+
assert_outcomes(result, errors=1)
203+
else:
204+
assert_outcomes(result, failed=1)
194205

195206

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

207218
def test_fixture_errors_if_already_decorated_with_given(testdir):
208219
script = testdir.makepyfile(TESTSCRIPT_GIVEN_THEN_FIXTURE)
209-
if int(pytest.__version__.split(".")[0]) > 5:
210-
testdir.runpytest(script).assert_outcomes(errors=1)
211-
else:
212-
testdir.runpytest(script).assert_outcomes(error=1)
220+
assert_outcomes(testdir.runpytest(script), errors=1)

requirements/coverage.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ ptyprocess==0.7.0
6060
# via pexpect
6161
pyarrow==19.0.1
6262
# via -r requirements/coverage.in
63-
pytest==8.3.5
63+
pytest==8.4.0
6464
# via
6565
# -r requirements/test.in
6666
# pytest-cov

requirements/crosshair.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ ptyprocess==0.7.0
5353
# via pexpect
5454
pygls==1.3.1
5555
# via crosshair-tool
56-
pytest==8.3.5
56+
pytest==8.4.0
5757
# via
5858
# -r requirements/test.in
5959
# pytest-xdist

requirements/fuzzing.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ pyarrow==19.0.1
110110
# via -r requirements/coverage.in
111111
pygments==2.19.1
112112
# via rich
113-
pytest==8.3.5
113+
pytest==8.4.0
114114
# via
115115
# -r requirements/test.in
116116
# hypofuzz

requirements/test.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pluggy==1.5.0
2424
# via pytest
2525
ptyprocess==0.7.0
2626
# via pexpect
27-
pytest==8.3.5
27+
pytest==8.4.0
2828
# via
2929
# -r requirements/test.in
3030
# pytest-xdist

requirements/tools.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ pyproject-hooks==1.2.0
220220
# pip-tools
221221
pyright==1.1.399
222222
# via -r requirements/tools.in
223-
pytest==8.3.5
223+
pytest==8.4.0
224224
# via
225225
# -r requirements/test.in
226226
# pytest-xdist

0 commit comments

Comments
 (0)