2017-03-17 09:21:30 +08:00
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
|
2016-06-25 18:19:46 +08:00
|
|
|
import pytest
|
|
|
|
import sys
|
|
|
|
|
2016-07-04 04:33:21 +08:00
|
|
|
|
2016-06-25 18:20:56 +08:00
|
|
|
def pytest_addoption(parser):
|
|
|
|
group = parser.getgroup("debugconfig")
|
2018-05-23 22:48:46 +08:00
|
|
|
group.addoption(
|
|
|
|
"--setuponly",
|
|
|
|
"--setup-only",
|
|
|
|
action="store_true",
|
|
|
|
help="only setup fixtures, do not execute tests.",
|
|
|
|
)
|
|
|
|
group.addoption(
|
|
|
|
"--setupshow",
|
|
|
|
"--setup-show",
|
|
|
|
action="store_true",
|
|
|
|
help="show setup of fixtures while executing tests.",
|
|
|
|
)
|
2016-07-04 04:33:21 +08:00
|
|
|
|
2016-06-25 18:20:56 +08:00
|
|
|
|
2016-06-25 18:19:46 +08:00
|
|
|
@pytest.hookimpl(hookwrapper=True)
|
|
|
|
def pytest_fixture_setup(fixturedef, request):
|
|
|
|
yield
|
|
|
|
config = request.config
|
2016-07-04 04:30:51 +08:00
|
|
|
if config.option.setupshow:
|
2018-05-23 22:48:46 +08:00
|
|
|
if hasattr(request, "param"):
|
2016-06-25 18:19:46 +08:00
|
|
|
# Save the fixture parameter so ._show_fixture_action() can
|
|
|
|
# display it now and during the teardown (in .finish()).
|
|
|
|
if fixturedef.ids:
|
|
|
|
if callable(fixturedef.ids):
|
|
|
|
fixturedef.cached_param = fixturedef.ids(request.param)
|
|
|
|
else:
|
2018-05-23 22:48:46 +08:00
|
|
|
fixturedef.cached_param = fixturedef.ids[request.param_index]
|
2016-06-25 18:19:46 +08:00
|
|
|
else:
|
|
|
|
fixturedef.cached_param = request.param
|
2018-05-23 22:48:46 +08:00
|
|
|
_show_fixture_action(fixturedef, "SETUP")
|
2016-06-25 18:19:46 +08:00
|
|
|
|
2016-07-04 04:33:21 +08:00
|
|
|
|
2016-06-25 18:19:46 +08:00
|
|
|
def pytest_fixture_post_finalizer(fixturedef):
|
|
|
|
if hasattr(fixturedef, "cached_result"):
|
|
|
|
config = fixturedef._fixturemanager.config
|
2016-07-04 04:30:51 +08:00
|
|
|
if config.option.setupshow:
|
2018-05-23 22:48:46 +08:00
|
|
|
_show_fixture_action(fixturedef, "TEARDOWN")
|
2016-06-25 18:19:46 +08:00
|
|
|
if hasattr(fixturedef, "cached_param"):
|
|
|
|
del fixturedef.cached_param
|
|
|
|
|
2016-07-04 04:33:21 +08:00
|
|
|
|
2016-06-25 18:19:46 +08:00
|
|
|
def _show_fixture_action(fixturedef, msg):
|
|
|
|
config = fixturedef._fixturemanager.config
|
2018-05-23 22:48:46 +08:00
|
|
|
capman = config.pluginmanager.getplugin("capturemanager")
|
2016-06-25 18:19:46 +08:00
|
|
|
if capman:
|
2017-09-27 06:54:26 +08:00
|
|
|
out, err = capman.suspend_global_capture()
|
2016-06-25 18:19:46 +08:00
|
|
|
|
|
|
|
tw = config.get_terminal_writer()
|
|
|
|
tw.line()
|
2018-05-23 22:48:46 +08:00
|
|
|
tw.write(" " * 2 * fixturedef.scopenum)
|
|
|
|
tw.write(
|
|
|
|
"{step} {scope} {fixture}".format(
|
|
|
|
step=msg.ljust(8), # align the output to TEARDOWN
|
|
|
|
scope=fixturedef.scope[0].upper(),
|
|
|
|
fixture=fixturedef.argname,
|
|
|
|
)
|
|
|
|
)
|
2016-06-25 18:19:46 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
if msg == "SETUP":
|
|
|
|
deps = sorted(arg for arg in fixturedef.argnames if arg != "request")
|
2016-06-25 18:19:46 +08:00
|
|
|
if deps:
|
2018-05-23 22:48:46 +08:00
|
|
|
tw.write(" (fixtures used: {})".format(", ".join(deps)))
|
2016-06-25 18:19:46 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
if hasattr(fixturedef, "cached_param"):
|
|
|
|
tw.write("[{}]".format(fixturedef.cached_param))
|
2016-06-25 18:19:46 +08:00
|
|
|
|
|
|
|
if capman:
|
2017-09-27 06:54:26 +08:00
|
|
|
capman.resume_global_capture()
|
2016-06-25 18:19:46 +08:00
|
|
|
sys.stdout.write(out)
|
|
|
|
sys.stderr.write(err)
|
2016-07-04 04:30:51 +08:00
|
|
|
|
2016-07-04 04:33:21 +08:00
|
|
|
|
2016-07-04 04:30:51 +08:00
|
|
|
@pytest.hookimpl(tryfirst=True)
|
|
|
|
def pytest_cmdline_main(config):
|
|
|
|
if config.option.setuponly:
|
|
|
|
config.option.setupshow = True
|