Fix fixture parameter display when ids is a function

This commit is contained in:
Vasily Kuznetsov 2016-06-23 10:54:22 +02:00
parent 1a5e530b98
commit c6af737d4e
2 changed files with 28 additions and 7 deletions

View File

@ -2480,7 +2480,7 @@ class FixtureDef:
if hasattr(self, "cached_result"):
config = self._fixturemanager.config
if config.option.setuponly or config.option.setupplan:
self._log_fixture_stack('TEARDOWN')
self._show_fixture_action('TEARDOWN')
if hasattr(self, "cached_param"):
del self.cached_param
del self.cached_result
@ -2533,22 +2533,24 @@ class FixtureDef:
else:
result = call_fixture_func(fixturefunc, request, kwargs)
if config.option.setuponly or config.option.setupplan:
# We want to access the params of ids if they exist also in during
# the finish() method.
if hasattr(request, 'param'):
# Save the fixture parameter so ._show_fixture_action() can
# display it now and during the teardown (in .finish()).
if self.ids:
ind = self.params.index(request.param)
self.cached_param = self.ids[ind]
if callable(self.ids):
self.cached_param = self.ids(request.param)
else:
self.cached_param = self.ids[request.param_index]
else:
self.cached_param = request.param
self._log_fixture_stack('SETUP')
self._show_fixture_action('SETUP')
except Exception:
self.cached_result = (None, my_cache_key, sys.exc_info())
raise
self.cached_result = (result, my_cache_key, None)
return result
def _log_fixture_stack(self, what):
def _show_fixture_action(self, what):
config = self._fixturemanager.config
capman = config.pluginmanager.getplugin('capturemanager')
if capman:

View File

@ -159,6 +159,25 @@ def test_show_fixtures_with_parameter_ids(testdir, mode):
])
def test_show_fixtures_with_parameter_ids_function(testdir, mode):
p = testdir.makepyfile('''
import pytest
@pytest.fixture(params=['foo', 'bar'], ids=lambda p: p.upper())
def foobar():
pass
def test_foobar(foobar):
pass
''')
result = testdir.runpytest(mode, p)
assert result.ret == 0
result.stdout.fnmatch_lines([
'*SETUP F foobar?FOO?',
'*SETUP F foobar?BAR?',
])
def test_dynamic_fixture_request(testdir):
p = testdir.makepyfile('''
import pytest