Skip to content

Commit a0bbad6

Browse files
committed
Experiment to make normal fixtures work with "yield"
1 parent b5bd4d9 commit a0bbad6

File tree

2 files changed

+30
-58
lines changed

2 files changed

+30
-58
lines changed

_pytest/python.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2326,6 +2326,7 @@ def fail_fixturefunc(fixturefunc, msg):
23262326
pytrace=False)
23272327

23282328
def call_fixture_func(fixturefunc, request, kwargs, yieldctx):
2329+
yieldctx = is_generator(fixturefunc)
23292330
if yieldctx:
23302331
if not is_generator(fixturefunc):
23312332
fail_fixturefunc(fixturefunc,

testing/python/fixture.py

Lines changed: 29 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2597,11 +2597,13 @@ def test_bar(arg1):
25972597
''')
25982598

25992599

2600+
@pytest.mark.parametrize('flavor', ['fixture', 'yield_fixture'])
26002601
class TestContextManagerFixtureFuncs:
2601-
def test_simple(self, testdir):
2602+
2603+
def test_simple(self, testdir, flavor):
26022604
testdir.makepyfile("""
26032605
import pytest
2604-
@pytest.yield_fixture
2606+
@pytest.{flavor}
26052607
def arg1():
26062608
print ("setup")
26072609
yield 1
@@ -2611,7 +2613,7 @@ def test_1(arg1):
26112613
def test_2(arg1):
26122614
print ("test2 %s" % arg1)
26132615
assert 0
2614-
""")
2616+
""".format(flavor=flavor))
26152617
result = testdir.runpytest("-s")
26162618
result.stdout.fnmatch_lines("""
26172619
*setup*
@@ -2622,10 +2624,10 @@ def test_2(arg1):
26222624
*teardown*
26232625
""")
26242626

2625-
def test_scoped(self, testdir):
2627+
def test_scoped(self, testdir, flavor):
26262628
testdir.makepyfile("""
26272629
import pytest
2628-
@pytest.yield_fixture(scope="module")
2630+
@pytest.{flavor}(scope="module")
26292631
def arg1():
26302632
print ("setup")
26312633
yield 1
@@ -2634,7 +2636,7 @@ def test_1(arg1):
26342636
print ("test1 %s" % arg1)
26352637
def test_2(arg1):
26362638
print ("test2 %s" % arg1)
2637-
""")
2639+
""".format(flavor=flavor))
26382640
result = testdir.runpytest("-s")
26392641
result.stdout.fnmatch_lines("""
26402642
*setup*
@@ -2643,94 +2645,63 @@ def test_2(arg1):
26432645
*teardown*
26442646
""")
26452647

2646-
def test_setup_exception(self, testdir):
2648+
def test_setup_exception(self, testdir, flavor):
26472649
testdir.makepyfile("""
26482650
import pytest
2649-
@pytest.yield_fixture(scope="module")
2651+
@pytest.{flavor}(scope="module")
26502652
def arg1():
26512653
pytest.fail("setup")
26522654
yield 1
26532655
def test_1(arg1):
26542656
pass
2655-
""")
2657+
""".format(flavor=flavor))
26562658
result = testdir.runpytest("-s")
26572659
result.stdout.fnmatch_lines("""
26582660
*pytest.fail*setup*
26592661
*1 error*
26602662
""")
26612663

2662-
def test_teardown_exception(self, testdir):
2664+
def test_teardown_exception(self, testdir, flavor):
26632665
testdir.makepyfile("""
26642666
import pytest
2665-
@pytest.yield_fixture(scope="module")
2667+
@pytest.{flavor}(scope="module")
26662668
def arg1():
26672669
yield 1
26682670
pytest.fail("teardown")
26692671
def test_1(arg1):
26702672
pass
2671-
""")
2673+
""".format(flavor=flavor))
26722674
result = testdir.runpytest("-s")
26732675
result.stdout.fnmatch_lines("""
26742676
*pytest.fail*teardown*
26752677
*1 passed*1 error*
26762678
""")
26772679

2678-
def test_yields_more_than_one(self, testdir):
2680+
def test_yields_more_than_one(self, testdir, flavor):
26792681
testdir.makepyfile("""
26802682
import pytest
2681-
@pytest.yield_fixture(scope="module")
2683+
@pytest.{flavor}(scope="module")
26822684
def arg1():
26832685
yield 1
26842686
yield 2
26852687
def test_1(arg1):
26862688
pass
2687-
""")
2689+
""".format(flavor=flavor))
26882690
result = testdir.runpytest("-s")
26892691
result.stdout.fnmatch_lines("""
26902692
*fixture function*
26912693
*test_yields*:2*
26922694
""")
26932695

2694-
2695-
def test_no_yield(self, testdir):
2696-
testdir.makepyfile("""
2697-
import pytest
2698-
@pytest.yield_fixture(scope="module")
2699-
def arg1():
2700-
return 1
2701-
def test_1(arg1):
2702-
pass
2703-
""")
2704-
result = testdir.runpytest("-s")
2705-
result.stdout.fnmatch_lines("""
2706-
*yield_fixture*requires*yield*
2707-
*yield_fixture*
2708-
*def arg1*
2709-
""")
2710-
2711-
def test_yield_not_allowed_in_non_yield(self, testdir):
2712-
testdir.makepyfile("""
2713-
import pytest
2714-
@pytest.fixture(scope="module")
2715-
def arg1():
2716-
yield 1
2717-
def test_1(arg1):
2718-
pass
2719-
""")
2720-
result = testdir.runpytest("-s")
2721-
result.stdout.fnmatch_lines("""
2722-
*fixture*cannot use*yield*
2723-
*def arg1*
2724-
""")
2725-
2726-
def test_custom_name(self, testdir):
2727-
testdir.makepyfile("""
2728-
import pytest
2729-
@pytest.fixture(name='meow')
2730-
def arg1():
2731-
return 'mew'
2732-
def test_1(meow):
2733-
print(meow)
2734-
""")
2735-
result = testdir.runpytest("-s")
2736-
result.stdout.fnmatch_lines("*mew*")
2696+
# TODO: yield_fixture should work as well (this is bugged right now)
2697+
def test_custom_name(testdir):
2698+
testdir.makepyfile("""
2699+
import pytest
2700+
@pytest.fixture(name='meow')
2701+
def arg1():
2702+
return 'mew'
2703+
def test_1(meow):
2704+
print(meow)
2705+
""")
2706+
result = testdir.runpytest("-s")
2707+
result.stdout.fnmatch_lines("*mew*")

0 commit comments

Comments
 (0)