Coverage for /usr/local/lib/python3.7/site-packages/_pytest/setuponly.py : 14%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1import pytest
4def pytest_addoption(parser):
5 group = parser.getgroup("debugconfig")
6 group.addoption(
7 "--setuponly",
8 "--setup-only",
9 action="store_true",
10 help="only setup fixtures, do not execute tests.",
11 )
12 group.addoption(
13 "--setupshow",
14 "--setup-show",
15 action="store_true",
16 help="show setup of fixtures while executing tests.",
17 )
20@pytest.hookimpl(hookwrapper=True)
21def pytest_fixture_setup(fixturedef, request):
22 yield
23 if request.config.option.setupshow:
24 if hasattr(request, "param"):
25 # Save the fixture parameter so ._show_fixture_action() can
26 # display it now and during the teardown (in .finish()).
27 if fixturedef.ids:
28 if callable(fixturedef.ids):
29 fixturedef.cached_param = fixturedef.ids(request.param)
30 else:
31 fixturedef.cached_param = fixturedef.ids[request.param_index]
32 else:
33 fixturedef.cached_param = request.param
34 _show_fixture_action(fixturedef, "SETUP")
37def pytest_fixture_post_finalizer(fixturedef):
38 if hasattr(fixturedef, "cached_result"):
39 config = fixturedef._fixturemanager.config
40 if config.option.setupshow:
41 _show_fixture_action(fixturedef, "TEARDOWN")
42 if hasattr(fixturedef, "cached_param"):
43 del fixturedef.cached_param
46def _show_fixture_action(fixturedef, msg):
47 config = fixturedef._fixturemanager.config
48 capman = config.pluginmanager.getplugin("capturemanager")
49 if capman:
50 capman.suspend_global_capture()
52 tw = config.get_terminal_writer()
53 tw.line()
54 tw.write(" " * 2 * fixturedef.scopenum)
55 tw.write(
56 "{step} {scope} {fixture}".format(
57 step=msg.ljust(8), # align the output to TEARDOWN
58 scope=fixturedef.scope[0].upper(),
59 fixture=fixturedef.argname,
60 )
61 )
63 if msg == "SETUP":
64 deps = sorted(arg for arg in fixturedef.argnames if arg != "request")
65 if deps:
66 tw.write(" (fixtures used: {})".format(", ".join(deps)))
68 if hasattr(fixturedef, "cached_param"):
69 tw.write("[{}]".format(fixturedef.cached_param))
71 if capman:
72 capman.resume_global_capture()
75@pytest.hookimpl(tryfirst=True)
76def pytest_cmdline_main(config):
77 if config.option.setuponly:
78 config.option.setupshow = True