Add --junittime=call option

This commit is contained in:
Andrey Paramonov 2018-12-11 19:29:31 +03:00
parent bb363c8ff2
commit cfbd387a5d
2 changed files with 36 additions and 3 deletions

View File

@ -314,6 +314,15 @@ def pytest_addoption(parser):
default=None, default=None,
help="prepend prefix to classnames in junit-xml output", help="prepend prefix to classnames in junit-xml output",
) )
group.addoption(
"--junittime",
"--junit-time",
action="store",
metavar="str",
default="total",
# choices=["total", "call"],
help='duration time to report: "total" (default), "call"',
)
parser.addini( parser.addini(
"junit_suite_name", "Test suite name for JUnit report", default="pytest" "junit_suite_name", "Test suite name for JUnit report", default="pytest"
) )
@ -334,6 +343,7 @@ def pytest_configure(config):
config.option.junitprefix, config.option.junitprefix,
config.getini("junit_suite_name"), config.getini("junit_suite_name"),
config.getini("junit_logging"), config.getini("junit_logging"),
config.option.junittime,
) )
config.pluginmanager.register(config._xml) config.pluginmanager.register(config._xml)
@ -361,12 +371,14 @@ def mangle_test_address(address):
class LogXML(object): class LogXML(object):
def __init__(self, logfile, prefix, suite_name="pytest", logging="no"): def __init__(self, logfile, prefix, suite_name="pytest", logging="no",
report_duration=None):
logfile = os.path.expanduser(os.path.expandvars(logfile)) logfile = os.path.expanduser(os.path.expandvars(logfile))
self.logfile = os.path.normpath(os.path.abspath(logfile)) self.logfile = os.path.normpath(os.path.abspath(logfile))
self.prefix = prefix self.prefix = prefix
self.suite_name = suite_name self.suite_name = suite_name
self.logging = logging self.logging = logging
self.report_duration = report_duration
self.stats = dict.fromkeys(["error", "passed", "failure", "skipped"], 0) self.stats = dict.fromkeys(["error", "passed", "failure", "skipped"], 0)
self.node_reporters = {} # nodeid -> _NodeReporter self.node_reporters = {} # nodeid -> _NodeReporter
self.node_reporters_ordered = [] self.node_reporters_ordered = []
@ -500,8 +512,10 @@ class LogXML(object):
"""accumulates total duration for nodeid from given report and updates """accumulates total duration for nodeid from given report and updates
the Junit.testcase with the new total if already created. the Junit.testcase with the new total if already created.
""" """
reporter = self.node_reporter(report) if not self.report_duration or self.report_duration == "total" or \
reporter.duration += getattr(report, "duration", 0.0) report.when == self.report_duration:
reporter = self.node_reporter(report)
reporter.duration += getattr(report, "duration", 0.0)
def pytest_collectreport(self, report): def pytest_collectreport(self, report):
if not report.passed: if not report.passed:

View File

@ -153,6 +153,24 @@ class TestPython(object):
val = tnode["time"] val = tnode["time"]
assert round(float(val), 2) >= 0.03 assert round(float(val), 2) >= 0.03
def test_call_time(self, testdir):
testdir.makepyfile(
"""
import time, pytest
def setup_module():
time.sleep(0.01)
def teardown_module():
time.sleep(0.01)
def test_sleep():
time.sleep(0.01)
"""
)
result, dom = runandparse(testdir, "--junit-time=call")
node = dom.find_first_by_tag("testsuite")
tnode = node.find_first_by_tag("testcase")
val = tnode["time"]
assert 0.01 <= round(float(val), 2) < 0.02
def test_setup_error(self, testdir): def test_setup_error(self, testdir):
testdir.makepyfile( testdir.makepyfile(
""" """
@ -727,6 +745,7 @@ def test_dont_configure_on_slaves(tmpdir):
junitprefix = None junitprefix = None
# XXX: shouldnt need tmpdir ? # XXX: shouldnt need tmpdir ?
xmlpath = str(tmpdir.join("junix.xml")) xmlpath = str(tmpdir.join("junix.xml"))
junittime = None
register = gotten.append register = gotten.append
fake_config = FakeConfig() fake_config = FakeConfig()