Skip to content

Commit 3de3d0d

Browse files
author
Gupta Arpit
committed
Added final review requested changes
1 parent 5d5739f commit 3de3d0d

File tree

4 files changed

+38
-36
lines changed

4 files changed

+38
-36
lines changed

changelog/12960.breaking.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Test functions containing a yield now cause an explicit error. They have not been run since Pytest 4.0, and were previously marked as an expected failure and deprecation warning.
1+
Test functions containing a yield now cause an explicit error. They have not been run since pytest 4.0, and were previously marked as an expected failure and deprecation warning.

doc/en/deprecations.rst

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,36 @@ an appropriate period of deprecation has passed.
374374

375375
Some breaking changes which could not be deprecated are also listed.
376376

377+
.. _yield tests deprecated:
378+
379+
``yield`` tests
380+
~~~~~~~~~~~~~~~
381+
382+
.. versionremoved:: 8.4
383+
384+
pytest no longer supports ``yield``-style tests, where a test function actually ``yield`` functions and values
385+
that are then turned into proper test methods. Example:
386+
387+
.. code-block:: python
388+
389+
def check(x, y):
390+
assert x**x == y
391+
392+
393+
def test_squared():
394+
yield check, 2, 4
395+
yield check, 3, 9
396+
397+
This would result in two actual test functions being generated.
398+
399+
This form of test function doesn't support fixtures properly, and users should switch to ``pytest.mark.parametrize``:
400+
401+
.. code-block:: python
402+
403+
@pytest.mark.parametrize("x, y", [(2, 4), (3, 9)])
404+
def test_squared(x, y):
405+
assert x**x == y
406+
377407
.. _nose-deprecation:
378408

379409
Support for tests written for nose
@@ -1270,36 +1300,6 @@ with the ``name`` parameter:
12701300
return cell()
12711301
12721302
1273-
.. _yield tests deprecated:
1274-
1275-
``yield`` tests
1276-
~~~~~~~~~~~~~~~
1277-
1278-
.. versionremoved:: 4.0
1279-
1280-
pytest supported ``yield``-style tests, where a test function actually ``yield`` functions and values
1281-
that are then turned into proper test methods. Example:
1282-
1283-
.. code-block:: python
1284-
1285-
def check(x, y):
1286-
assert x**x == y
1287-
1288-
1289-
def test_squared():
1290-
yield check, 2, 4
1291-
yield check, 3, 9
1292-
1293-
This would result into two actual test functions being generated.
1294-
1295-
This form of test function doesn't support fixtures properly, and users should switch to ``pytest.mark.parametrize``:
1296-
1297-
.. code-block:: python
1298-
1299-
@pytest.mark.parametrize("x, y", [(2, 4), (3, 9)])
1300-
def test_squared(x, y):
1301-
assert x**x == y
1302-
13031303
.. _internal classes accessed through node deprecated:
13041304

13051305
Internal classes accessed through ``Node``

src/_pytest/python.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,9 @@ def pytest_pycollect_makeitem(
230230
)
231231
elif getattr(obj, "__test__", True):
232232
if inspect.isgeneratorfunction(obj):
233-
raise RuntimeError(
234-
"'yield' keyword is allowed in fixtures, but not in tests ({name})"
233+
fail(
234+
f"'yield' keyword is allowed in fixtures, but not in tests ({name})",
235+
pytrace=False,
235236
)
236237
return list(collector._genfunctions(name, obj))
237238
return None

testing/test_collection.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1881,7 +1881,7 @@ def test_respect_system_exceptions(
18811881

18821882

18831883
def test_yield_disallowed_in_tests(pytester: Pytester):
1884-
"""Ensure generator test functions with 'yield' raise a RuntimeError."""
1884+
"""Ensure generator test functions with 'yield' fail collection (#12960)."""
18851885
pytester.makepyfile(
18861886
"""
18871887
def test_with_yield():
@@ -1891,6 +1891,7 @@ def test_with_yield():
18911891
result = pytester.runpytest()
18921892
assert result.ret == 2
18931893
result.stdout.fnmatch_lines(
1894-
["*RuntimeError: 'yield' keyword is allowed in fixtures, but not in tests (*)*"]
1894+
["*'yield' keyword is allowed in fixtures, but not in tests (test_with_yield)*"]
18951895
)
1896-
result.stdout.fnmatch_lines(["*collected 0 items*"])
1896+
# Assert that no tests were collected
1897+
result.stdout.fnmatch_lines(["*collected 0 items*"])

0 commit comments

Comments
 (0)