Merge pull request #3708 from nicoddemus/small-refactors

Small refactorings
This commit is contained in:
Ronny Pfannschmidt 2018-07-23 06:53:08 +02:00 committed by GitHub
commit f8749eeb5c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 33 additions and 28 deletions

View File

@ -789,23 +789,26 @@ 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)
def teardown(): request.addfinalizer(finalizer)
try:
next(it)
except StopIteration:
pass
else:
fail_fixturefunc(
fixturefunc, "yield_fixture function has more than one 'yield'"
)
request.addfinalizer(teardown)
else: else:
res = fixturefunc(**kwargs) res = fixturefunc(**kwargs)
return res return res
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:
next(it)
except StopIteration:
pass
else:
fail_fixturefunc(
fixturefunc, "yield_fixture function has more than one 'yield'"
)
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()