Skip to content

Commit

Permalink
Experiment to make normal fixtures work with "yield"
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoddemus committed Jun 3, 2016
1 parent b5bd4d9 commit a0bbad6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 58 deletions.
1 change: 1 addition & 0 deletions _pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -2326,6 +2326,7 @@ def fail_fixturefunc(fixturefunc, msg):
pytrace=False)

def call_fixture_func(fixturefunc, request, kwargs, yieldctx):
yieldctx = is_generator(fixturefunc)
if yieldctx:
if not is_generator(fixturefunc):
fail_fixturefunc(fixturefunc,
Expand Down
87 changes: 29 additions & 58 deletions testing/python/fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -2597,11 +2597,13 @@ def test_bar(arg1):
''')


@pytest.mark.parametrize('flavor', ['fixture', 'yield_fixture'])
class TestContextManagerFixtureFuncs:
def test_simple(self, testdir):

def test_simple(self, testdir, flavor):
testdir.makepyfile("""
import pytest
@pytest.yield_fixture
@pytest.{flavor}
def arg1():
print ("setup")
yield 1
Expand All @@ -2611,7 +2613,7 @@ def test_1(arg1):
def test_2(arg1):
print ("test2 %s" % arg1)
assert 0
""")
""".format(flavor=flavor))
result = testdir.runpytest("-s")
result.stdout.fnmatch_lines("""
*setup*
Expand All @@ -2622,10 +2624,10 @@ def test_2(arg1):
*teardown*
""")

def test_scoped(self, testdir):
def test_scoped(self, testdir, flavor):
testdir.makepyfile("""
import pytest
@pytest.yield_fixture(scope="module")
@pytest.{flavor}(scope="module")
def arg1():
print ("setup")
yield 1
Expand All @@ -2634,7 +2636,7 @@ def test_1(arg1):
print ("test1 %s" % arg1)
def test_2(arg1):
print ("test2 %s" % arg1)
""")
""".format(flavor=flavor))
result = testdir.runpytest("-s")
result.stdout.fnmatch_lines("""
*setup*
Expand All @@ -2643,94 +2645,63 @@ def test_2(arg1):
*teardown*
""")

def test_setup_exception(self, testdir):
def test_setup_exception(self, testdir, flavor):
testdir.makepyfile("""
import pytest
@pytest.yield_fixture(scope="module")
@pytest.{flavor}(scope="module")
def arg1():
pytest.fail("setup")
yield 1
def test_1(arg1):
pass
""")
""".format(flavor=flavor))
result = testdir.runpytest("-s")
result.stdout.fnmatch_lines("""
*pytest.fail*setup*
*1 error*
""")

def test_teardown_exception(self, testdir):
def test_teardown_exception(self, testdir, flavor):
testdir.makepyfile("""
import pytest
@pytest.yield_fixture(scope="module")
@pytest.{flavor}(scope="module")
def arg1():
yield 1
pytest.fail("teardown")
def test_1(arg1):
pass
""")
""".format(flavor=flavor))
result = testdir.runpytest("-s")
result.stdout.fnmatch_lines("""
*pytest.fail*teardown*
*1 passed*1 error*
""")

def test_yields_more_than_one(self, testdir):
def test_yields_more_than_one(self, testdir, flavor):
testdir.makepyfile("""
import pytest
@pytest.yield_fixture(scope="module")
@pytest.{flavor}(scope="module")
def arg1():
yield 1
yield 2
def test_1(arg1):
pass
""")
""".format(flavor=flavor))
result = testdir.runpytest("-s")
result.stdout.fnmatch_lines("""
*fixture function*
*test_yields*:2*
""")


def test_no_yield(self, testdir):
testdir.makepyfile("""
import pytest
@pytest.yield_fixture(scope="module")
def arg1():
return 1
def test_1(arg1):
pass
""")
result = testdir.runpytest("-s")
result.stdout.fnmatch_lines("""
*yield_fixture*requires*yield*
*yield_fixture*
*def arg1*
""")

def test_yield_not_allowed_in_non_yield(self, testdir):
testdir.makepyfile("""
import pytest
@pytest.fixture(scope="module")
def arg1():
yield 1
def test_1(arg1):
pass
""")
result = testdir.runpytest("-s")
result.stdout.fnmatch_lines("""
*fixture*cannot use*yield*
*def arg1*
""")

def test_custom_name(self, testdir):
testdir.makepyfile("""
import pytest
@pytest.fixture(name='meow')
def arg1():
return 'mew'
def test_1(meow):
print(meow)
""")
result = testdir.runpytest("-s")
result.stdout.fnmatch_lines("*mew*")
# TODO: yield_fixture should work as well (this is bugged right now)
def test_custom_name(testdir):
testdir.makepyfile("""
import pytest
@pytest.fixture(name='meow')
def arg1():
return 'mew'
def test_1(meow):
print(meow)
""")
result = testdir.runpytest("-s")
result.stdout.fnmatch_lines("*mew*")

0 comments on commit a0bbad6

Please sign in to comment.