From cfbd387a5d7a27a3e7dfb754d7cd4482e931822b Mon Sep 17 00:00:00 2001 From: Andrey Paramonov Date: Tue, 11 Dec 2018 19:29:31 +0300 Subject: [PATCH] Add --junittime=call option --- src/_pytest/junitxml.py | 20 +++++++++++++++++--- testing/test_junitxml.py | 19 +++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/_pytest/junitxml.py b/src/_pytest/junitxml.py index 09847c942..c5cc2b0a9 100644 --- a/src/_pytest/junitxml.py +++ b/src/_pytest/junitxml.py @@ -314,6 +314,15 @@ def pytest_addoption(parser): default=None, 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( "junit_suite_name", "Test suite name for JUnit report", default="pytest" ) @@ -334,6 +343,7 @@ def pytest_configure(config): config.option.junitprefix, config.getini("junit_suite_name"), config.getini("junit_logging"), + config.option.junittime, ) config.pluginmanager.register(config._xml) @@ -361,12 +371,14 @@ def mangle_test_address(address): 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)) self.logfile = os.path.normpath(os.path.abspath(logfile)) self.prefix = prefix self.suite_name = suite_name self.logging = logging + self.report_duration = report_duration self.stats = dict.fromkeys(["error", "passed", "failure", "skipped"], 0) self.node_reporters = {} # nodeid -> _NodeReporter self.node_reporters_ordered = [] @@ -500,8 +512,10 @@ class LogXML(object): """accumulates total duration for nodeid from given report and updates the Junit.testcase with the new total if already created. """ - reporter = self.node_reporter(report) - reporter.duration += getattr(report, "duration", 0.0) + if not self.report_duration or self.report_duration == "total" or \ + report.when == self.report_duration: + reporter = self.node_reporter(report) + reporter.duration += getattr(report, "duration", 0.0) def pytest_collectreport(self, report): if not report.passed: diff --git a/testing/test_junitxml.py b/testing/test_junitxml.py index c9dc39f82..82ed7901c 100644 --- a/testing/test_junitxml.py +++ b/testing/test_junitxml.py @@ -153,6 +153,24 @@ class TestPython(object): val = tnode["time"] 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): testdir.makepyfile( """ @@ -727,6 +745,7 @@ def test_dont_configure_on_slaves(tmpdir): junitprefix = None # XXX: shouldnt need tmpdir ? xmlpath = str(tmpdir.join("junix.xml")) + junittime = None register = gotten.append fake_config = FakeConfig()