Remove yieldctx variable being passed around

This commit is contained in:
Bruno Oliveira 2016-06-07 20:18:57 -03:00
parent d712428d33
commit 98acda426f
1 changed files with 15 additions and 19 deletions

View File

@ -116,12 +116,10 @@ def safe_getattr(object, name, default):
class FixtureFunctionMarker: class FixtureFunctionMarker:
def __init__(self, scope, params, def __init__(self, scope, params, autouse=False, ids=None, name=None):
autouse=False, yieldctx=False, ids=None, name=None):
self.scope = scope self.scope = scope
self.params = params self.params = params
self.autouse = autouse self.autouse = autouse
self.yieldctx = yieldctx
self.ids = ids self.ids = ids
self.name = name self.name = name
@ -166,6 +164,10 @@ def fixture(scope="function", params=None, autouse=False, ids=None, name=None):
to resolve this is to name the decorated function to resolve this is to name the decorated function
``fixture_<fixturename>`` and then use ``fixture_<fixturename>`` and then use
``@pytest.fixture(name='<fixturename>')``. ``@pytest.fixture(name='<fixturename>')``.
Fixtures can optionally provide their values to test functions using a ``yield`` statement,
instead of ``return``. In this case, the code block after the ``yield`` statement is executed
as teardown code regardless of the test outcome. A fixture function must yield exactly once.
""" """
if callable(scope) and params is None and autouse == False: if callable(scope) and params is None and autouse == False:
# direct decoration # direct decoration
@ -175,22 +177,19 @@ def fixture(scope="function", params=None, autouse=False, ids=None, name=None):
params = list(params) params = list(params)
return FixtureFunctionMarker(scope, params, autouse, ids=ids, name=name) return FixtureFunctionMarker(scope, params, autouse, ids=ids, name=name)
def yield_fixture(scope="function", params=None, autouse=False, ids=None, name=None):
""" (return a) decorator to mark a yield-fixture factory function
(EXPERIMENTAL).
This takes the same arguments as :py:func:`pytest.fixture` but def yield_fixture(scope="function", params=None, autouse=False, ids=None, name=None):
expects a fixture function to use a ``yield`` instead of a ``return`` """ (return a) decorator to mark a yield-fixture factory function.
statement to provide a fixture. See
http://pytest.org/en/latest/yieldfixture.html for more info. .. deprecated:: 1.10
Use :py:func:`pytest.fixture` directly instead.
""" """
if callable(scope) and params is None and not autouse: if callable(scope) and params is None and not autouse:
# direct decoration # direct decoration
return FixtureFunctionMarker( return FixtureFunctionMarker(
"function", params, autouse, name=name, yieldctx=True, ids=ids)(scope) "function", params, autouse, ids=ids, name=name)(scope)
else: else:
return FixtureFunctionMarker(scope, params, autouse, name=name, return FixtureFunctionMarker(scope, params, autouse, ids=ids, name=name)
yieldctx=True, ids=ids)
defaultfuncargprefixmarker = fixture() defaultfuncargprefixmarker = fixture()
@ -2287,7 +2286,6 @@ class FixtureManager:
assert not name.startswith(self._argprefix) assert not name.startswith(self._argprefix)
fixturedef = FixtureDef(self, nodeid, name, obj, fixturedef = FixtureDef(self, nodeid, name, obj,
marker.scope, marker.params, marker.scope, marker.params,
yieldctx=marker.yieldctx,
unittest=unittest, ids=marker.ids) unittest=unittest, ids=marker.ids)
faclist = self._arg2fixturedefs.setdefault(name, []) faclist = self._arg2fixturedefs.setdefault(name, [])
if fixturedef.has_location: if fixturedef.has_location:
@ -2325,7 +2323,7 @@ def fail_fixturefunc(fixturefunc, msg):
pytest.fail(msg + ":\n\n" + str(source.indent()) + "\n" + location, pytest.fail(msg + ":\n\n" + str(source.indent()) + "\n" + location,
pytrace=False) pytrace=False)
def call_fixture_func(fixturefunc, request, kwargs, yieldctx): def call_fixture_func(fixturefunc, request, kwargs):
yieldctx = is_generator(fixturefunc) yieldctx = is_generator(fixturefunc)
if yieldctx: if yieldctx:
if not is_generator(fixturefunc): if not is_generator(fixturefunc):
@ -2357,7 +2355,7 @@ def call_fixture_func(fixturefunc, request, kwargs, yieldctx):
class FixtureDef: class FixtureDef:
""" A container for a factory definition. """ """ A container for a factory definition. """
def __init__(self, fixturemanager, baseid, argname, func, scope, params, def __init__(self, fixturemanager, baseid, argname, func, scope, params,
yieldctx, unittest=False, ids=None): unittest=False, ids=None):
self._fixturemanager = fixturemanager self._fixturemanager = fixturemanager
self.baseid = baseid or '' self.baseid = baseid or ''
self.has_location = baseid is not None self.has_location = baseid is not None
@ -2368,7 +2366,6 @@ class FixtureDef:
self.params = params self.params = params
startindex = unittest and 1 or None startindex = unittest and 1 or None
self.argnames = getfuncargnames(func, startindex=startindex) self.argnames = getfuncargnames(func, startindex=startindex)
self.yieldctx = yieldctx
self.unittest = unittest self.unittest = unittest
self.ids = ids self.ids = ids
self._finalizer = [] self._finalizer = []
@ -2429,8 +2426,7 @@ class FixtureDef:
fixturefunc = fixturefunc.__get__(request.instance) fixturefunc = fixturefunc.__get__(request.instance)
try: try:
result = call_fixture_func(fixturefunc, request, kwargs, result = call_fixture_func(fixturefunc, request, kwargs)
self.yieldctx)
except Exception: except Exception:
self.cached_result = (None, my_cache_key, sys.exc_info()) self.cached_result = (None, my_cache_key, sys.exc_info())
raise raise