Merge pull request #3708 from nicoddemus/small-refactors
Small refactorings
This commit is contained in:
commit
f8749eeb5c
|
@ -789,8 +789,16 @@ def call_fixture_func(fixturefunc, request, kwargs):
|
||||||
if yieldctx:
|
if yieldctx:
|
||||||
it = fixturefunc(**kwargs)
|
it = fixturefunc(**kwargs)
|
||||||
res = next(it)
|
res = next(it)
|
||||||
|
finalizer = functools.partial(_teardown_yield_fixture, fixturefunc, it)
|
||||||
|
request.addfinalizer(finalizer)
|
||||||
|
else:
|
||||||
|
res = fixturefunc(**kwargs)
|
||||||
|
return res
|
||||||
|
|
||||||
def teardown():
|
|
||||||
|
def _teardown_yield_fixture(fixturefunc, it):
|
||||||
|
"""Executes the teardown of a fixture function by advancing the iterator after the
|
||||||
|
yield and ensure the iteration ends (if not it means there is more than one yield in the function"""
|
||||||
try:
|
try:
|
||||||
next(it)
|
next(it)
|
||||||
except StopIteration:
|
except StopIteration:
|
||||||
|
@ -800,11 +808,6 @@ def call_fixture_func(fixturefunc, request, kwargs):
|
||||||
fixturefunc, "yield_fixture function has more than one 'yield'"
|
fixturefunc, "yield_fixture function has more than one 'yield'"
|
||||||
)
|
)
|
||||||
|
|
||||||
request.addfinalizer(teardown)
|
|
||||||
else:
|
|
||||||
res = fixturefunc(**kwargs)
|
|
||||||
return res
|
|
||||||
|
|
||||||
|
|
||||||
class FixtureDef(object):
|
class FixtureDef(object):
|
||||||
""" A container for a factory definition. """
|
""" A container for a factory definition. """
|
||||||
|
@ -896,15 +899,10 @@ class FixtureDef(object):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def pytest_fixture_setup(fixturedef, request):
|
def resolve_fixture_function(fixturedef, request):
|
||||||
""" Execution of fixture setup. """
|
"""Gets the actual callable that can be called to obtain the fixture value, dealing with unittest-specific
|
||||||
kwargs = {}
|
instances and bound methods.
|
||||||
for argname in fixturedef.argnames:
|
"""
|
||||||
fixdef = request._get_active_fixturedef(argname)
|
|
||||||
result, arg_cache_key, exc = fixdef.cached_result
|
|
||||||
request._check_scope(argname, request.scope, fixdef.scope)
|
|
||||||
kwargs[argname] = result
|
|
||||||
|
|
||||||
fixturefunc = fixturedef.func
|
fixturefunc = fixturedef.func
|
||||||
if fixturedef.unittest:
|
if fixturedef.unittest:
|
||||||
if request.instance is not None:
|
if request.instance is not None:
|
||||||
|
@ -918,6 +916,19 @@ def pytest_fixture_setup(fixturedef, request):
|
||||||
fixturefunc = getimfunc(fixturedef.func)
|
fixturefunc = getimfunc(fixturedef.func)
|
||||||
if fixturefunc != fixturedef.func:
|
if fixturefunc != fixturedef.func:
|
||||||
fixturefunc = fixturefunc.__get__(request.instance)
|
fixturefunc = fixturefunc.__get__(request.instance)
|
||||||
|
return fixturefunc
|
||||||
|
|
||||||
|
|
||||||
|
def pytest_fixture_setup(fixturedef, request):
|
||||||
|
""" Execution of fixture setup. """
|
||||||
|
kwargs = {}
|
||||||
|
for argname in fixturedef.argnames:
|
||||||
|
fixdef = request._get_active_fixturedef(argname)
|
||||||
|
result, arg_cache_key, exc = fixdef.cached_result
|
||||||
|
request._check_scope(argname, request.scope, fixdef.scope)
|
||||||
|
kwargs[argname] = result
|
||||||
|
|
||||||
|
fixturefunc = resolve_fixture_function(fixturedef, request)
|
||||||
my_cache_key = request.param_index
|
my_cache_key = request.param_index
|
||||||
try:
|
try:
|
||||||
result = call_fixture_func(fixturefunc, request, kwargs)
|
result = call_fixture_func(fixturefunc, request, kwargs)
|
||||||
|
@ -1016,13 +1027,7 @@ def yield_fixture(scope="function", params=None, autouse=False, ids=None, name=N
|
||||||
.. deprecated:: 3.0
|
.. deprecated:: 3.0
|
||||||
Use :py:func:`pytest.fixture` directly instead.
|
Use :py:func:`pytest.fixture` directly instead.
|
||||||
"""
|
"""
|
||||||
if callable(scope) and params is None and not autouse:
|
return fixture(scope=scope, params=params, autouse=autouse, ids=ids, name=name)
|
||||||
# direct decoration
|
|
||||||
return FixtureFunctionMarker("function", params, autouse, ids=ids, name=name)(
|
|
||||||
scope
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
return FixtureFunctionMarker(scope, params, autouse, ids=ids, name=name)
|
|
||||||
|
|
||||||
|
|
||||||
defaultfuncargprefixmarker = fixture()
|
defaultfuncargprefixmarker = fixture()
|
||||||
|
|
Loading…
Reference in New Issue