File tree Expand file tree Collapse file tree 3 files changed +32
-5
lines changed Expand file tree Collapse file tree 3 files changed +32
-5
lines changed Original file line number Diff line number Diff line change
1
+ When a yielding fixture fails to yield a value, report a test setup error instead of crashing.
Original file line number Diff line number Diff line change @@ -786,13 +786,18 @@ def fail_fixturefunc(fixturefunc, msg):
786
786
def call_fixture_func (fixturefunc , request , kwargs ):
787
787
yieldctx = is_generator (fixturefunc )
788
788
if yieldctx :
789
- it = fixturefunc (** kwargs )
790
- res = next (it )
791
- finalizer = functools .partial (_teardown_yield_fixture , fixturefunc , it )
789
+ generator = fixturefunc (** kwargs )
790
+ try :
791
+ fixture_result = next (generator )
792
+ except StopIteration :
793
+ raise ValueError (
794
+ "{} did not yield a value" .format (request .fixturename )
795
+ ) from None
796
+ finalizer = functools .partial (_teardown_yield_fixture , fixturefunc , generator )
792
797
request .addfinalizer (finalizer )
793
798
else :
794
- res = fixturefunc (** kwargs )
795
- return res
799
+ fixture_result = fixturefunc (** kwargs )
800
+ return fixture_result
796
801
797
802
798
803
def _teardown_yield_fixture (fixturefunc , it ):
Original file line number Diff line number Diff line change 3
3
4
4
import pytest
5
5
from _pytest import fixtures
6
+ from _pytest .config import ExitCode
6
7
from _pytest .fixtures import FixtureRequest
7
8
from _pytest .pathlib import Path
8
9
from _pytest .pytester import get_public_names
@@ -4290,3 +4291,23 @@ def test_suffix(fix_combined):
4290
4291
)
4291
4292
result = testdir .runpytest ("-vv" , str (p1 ))
4292
4293
assert result .ret == 0
4294
+
4295
+
4296
+ def test_yield_fixture_with_no_value (testdir ):
4297
+ testdir .makepyfile (
4298
+ """
4299
+ import pytest
4300
+ @pytest.fixture(name='custom')
4301
+ def empty_yield():
4302
+ if False:
4303
+ yield
4304
+
4305
+ def test_fixt(custom):
4306
+ pass
4307
+ """
4308
+ )
4309
+ expected = "E ValueError: custom did not yield a value"
4310
+ result = testdir .runpytest ()
4311
+ result .assert_outcomes (error = 1 )
4312
+ result .stdout .fnmatch_lines ([expected ])
4313
+ assert result .ret == ExitCode .TESTS_FAILED
You can’t perform that action at this time.
0 commit comments