From 93628fc0eb304407689a31aad81f994a1a9fa87f Mon Sep 17 00:00:00 2001 From: Janne Vanhala Date: Fri, 1 May 2015 14:55:52 +0300 Subject: [PATCH] Include setup and teardown in junitxml test durations --HG-- branch : include-setup-teardown-duration-in-junitxml --- _pytest/junitxml.py | 8 ++++---- testing/test_junitxml.py | 6 +++++- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/_pytest/junitxml.py b/_pytest/junitxml.py index 6d25d8407..3553a19ec 100644 --- a/_pytest/junitxml.py +++ b/_pytest/junitxml.py @@ -96,7 +96,7 @@ class LogXML(object): self.tests.append(Junit.testcase( classname=".".join(classnames), name=bin_xml_escape(names[-1]), - time=getattr(report, 'duration', 0) + time=0 )) def _write_captured_output(self, report): @@ -168,18 +168,18 @@ class LogXML(object): self._write_captured_output(report) def pytest_runtest_logreport(self, report): + if report.when == "setup": + self._opentestcase(report) + self.tests[-1].attr.time += getattr(report, 'duration', 0) if report.passed: if report.when == "call": # ignore setup/teardown - self._opentestcase(report) self.append_pass(report) elif report.failed: - self._opentestcase(report) if report.when != "call": self.append_error(report) else: self.append_failure(report) elif report.skipped: - self._opentestcase(report) self.append_skipped(report) def pytest_collectreport(self, report): diff --git a/testing/test_junitxml.py b/testing/test_junitxml.py index 9da836bbe..4c54a14b6 100644 --- a/testing/test_junitxml.py +++ b/testing/test_junitxml.py @@ -44,6 +44,10 @@ class TestPython: def test_timing_function(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) """) @@ -51,7 +55,7 @@ class TestPython: node = dom.getElementsByTagName("testsuite")[0] tnode = node.getElementsByTagName("testcase")[0] val = tnode.getAttributeNode("time").value - assert float(val) >= 0.001 + assert float(val) >= 0.03 def test_setup_error(self, testdir): testdir.makepyfile("""