Skip to content

Commit 92bcc36

Browse files
committed
Refactor logging of fixtures
1 parent 7d19f83 commit 92bcc36

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

_pytest/python.py

+22-12
Original file line numberDiff line numberDiff line change
@@ -2460,12 +2460,8 @@ def finish(self):
24602460
# the cached fixture value
24612461
if hasattr(self, "cached_result"):
24622462
if self._fixturemanager.config.option.setuponly:
2463-
tw = self._fixturemanager.config.get_terminal_writer()
2464-
tw.line()
2465-
tw.write(' ' * 2 * self.scopenum)
2466-
tw.write('TEARDOWN {} {}'.format(self.scope[0].upper(), self.argname))
2463+
self._log_fixture_stack('TEARDOWN')
24672464
if hasattr(self, "cached_param"):
2468-
tw.write('[{}]'.format(self.cached_param))
24692465
del self.cached_param
24702466
del self.cached_result
24712467

@@ -2512,19 +2508,33 @@ def execute(self, request):
25122508

25132509
try:
25142510
result = call_fixture_func(fixturefunc, request, kwargs)
2515-
tw = request.config.get_terminal_writer()
2516-
tw.line()
2517-
tw.write(' ' * 2 * self.scopenum)
2518-
tw.write('SETUP {} {}'.format(self.scope[0].upper(), fixturefunc.__name__))
2519-
if hasattr(request, 'param'):
2520-
tw.write('[{}]'.format(request.param))
2521-
self.cached_param = request.param
2511+
# We want to access the params of ids if they exist also in during
2512+
# the finish() method.
2513+
if self._fixturemanager.config.option.setuponly:
2514+
if hasattr(request, 'param'):
2515+
if self.ids:
2516+
ind = self.params.index(request.param)
2517+
self.cached_param = self.ids[ind]
2518+
else:
2519+
self.cached_param = request.param
2520+
self._log_fixture_stack('SETUP')
25222521
except Exception:
25232522
self.cached_result = (None, my_cache_key, sys.exc_info())
25242523
raise
25252524
self.cached_result = (result, my_cache_key, None)
25262525
return result
25272526

2527+
def _log_fixture_stack(self, what):
2528+
tw = self._fixturemanager.config.get_terminal_writer()
2529+
tw.line()
2530+
tw.write(' ' * 2 * self.scopenum)
2531+
tw.write('{step} {scope} {fixture}'.format(
2532+
step=what.ljust(8), # align the output to TEARDOWN
2533+
scope=self.scope[0].upper(),
2534+
fixture=self.argname))
2535+
if hasattr(self, 'cached_param'):
2536+
tw.write('[{}]'.format(self.cached_param))
2537+
25282538
def __repr__(self):
25292539
return ("<FixtureDef name=%r scope=%r baseid=%r >" %
25302540
(self.argname, self.scope, self.baseid))

0 commit comments

Comments
 (0)