From bdc29968b8586cd6c8851d1178801d9af3ed1806 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Tue, 7 Jun 2016 21:36:32 -0300 Subject: [PATCH] Remove dead code and simplify code in call_fixture_func --- _pytest/python.py | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/_pytest/python.py b/_pytest/python.py index 59d779ae1..59ba758b2 100644 --- a/_pytest/python.py +++ b/_pytest/python.py @@ -2326,29 +2326,20 @@ def fail_fixturefunc(fixturefunc, msg): def call_fixture_func(fixturefunc, request, kwargs): yieldctx = is_generator(fixturefunc) if yieldctx: - if not is_generator(fixturefunc): - fail_fixturefunc(fixturefunc, - msg="yield_fixture requires yield statement in function") - iter = fixturefunc(**kwargs) - next = getattr(iter, "__next__", None) - if next is None: - next = getattr(iter, "next") - res = next() + it = fixturefunc(**kwargs) + res = next(it) + def teardown(): try: - next() + next(it) except StopIteration: pass else: fail_fixturefunc(fixturefunc, "yield_fixture function has more than one 'yield'") + request.addfinalizer(teardown) else: - if is_generator(fixturefunc): - fail_fixturefunc(fixturefunc, - msg="pytest.fixture functions cannot use ``yield``. " - "Instead write and return an inner function/generator " - "and let the consumer call and iterate over it.") res = fixturefunc(**kwargs) return res