fix issue67 - junitxml now contains correct durations. thanks ronny.

This commit is contained in:
holger krekel 2011-09-24 15:15:51 +02:00
commit 45afb1b7d1
3 changed files with 20 additions and 17 deletions

View File

@ -1,6 +1,8 @@
Changes between 2.1.1 and [next version] Changes between 2.1.1 and [next version]
---------------------------------------- ----------------------------------------
- fix issue67 / junitxml now contains correct test durations, thanks ronny
Changes between 2.1.1 and 2.1.2 Changes between 2.1.1 and 2.1.2
---------------------------------------- ----------------------------------------

View File

@ -71,13 +71,12 @@ class LogXML(object):
self.test_logs = [] self.test_logs = []
self.passed = self.skipped = 0 self.passed = self.skipped = 0
self.failed = self.errors = 0 self.failed = self.errors = 0
self._durations = {}
def _opentestcase(self, report): def _opentestcase(self, report):
names = report.nodeid.split("::") names = report.nodeid.split("::")
names[0] = names[0].replace("/", '.') names[0] = names[0].replace("/", '.')
names = tuple(names) names = tuple(names)
d = {'time': self._durations.pop(report.nodeid, "0")} d = {'time': getattr(report, 'duration', 0)}
names = [x.replace(".py", "") for x in names if x != "()"] names = [x.replace(".py", "") for x in names if x != "()"]
classnames = names[:-1] classnames = names[:-1]
if self.prefix: if self.prefix:
@ -176,13 +175,6 @@ class LogXML(object):
elif report.skipped: elif report.skipped:
self.append_skipped(report) self.append_skipped(report)
def pytest_runtest_call(self, item, __multicall__):
start = time.time()
try:
return __multicall__.execute()
finally:
self._durations[item.nodeid] = time.time() - start
def pytest_collectreport(self, report): def pytest_collectreport(self, report):
if not report.passed: if not report.passed:
if report.failed: if report.failed:

View File

@ -1,6 +1,6 @@
""" basic collect and runtest protocol implementations """ """ basic collect and runtest protocol implementations """
import py, sys import py, sys, time
from py._code.code import TerminalRepr from py._code.code import TerminalRepr
def pytest_namespace(): def pytest_namespace():
@ -95,12 +95,16 @@ class CallInfo:
#: context of invocation: one of "setup", "call", #: context of invocation: one of "setup", "call",
#: "teardown", "memocollect" #: "teardown", "memocollect"
self.when = when self.when = when
self.start = time.time()
try: try:
self.result = func() try:
except KeyboardInterrupt: self.result = func()
raise except KeyboardInterrupt:
except: raise
self.excinfo = py.code.ExceptionInfo() except:
self.excinfo = py.code.ExceptionInfo()
finally:
self.stop = time.time()
def __repr__(self): def __repr__(self):
if self.excinfo: if self.excinfo:
@ -139,6 +143,7 @@ class BaseReport(object):
def pytest_runtest_makereport(item, call): def pytest_runtest_makereport(item, call):
when = call.when when = call.when
duration = call.stop-call.start
keywords = dict([(x,1) for x in item.keywords]) keywords = dict([(x,1) for x in item.keywords])
excinfo = call.excinfo excinfo = call.excinfo
if not call.excinfo: if not call.excinfo:
@ -160,14 +165,15 @@ def pytest_runtest_makereport(item, call):
else: # exception in setup or teardown else: # exception in setup or teardown
longrepr = item._repr_failure_py(excinfo) longrepr = item._repr_failure_py(excinfo)
return TestReport(item.nodeid, item.location, return TestReport(item.nodeid, item.location,
keywords, outcome, longrepr, when) keywords, outcome, longrepr, when,
duration=duration)
class TestReport(BaseReport): class TestReport(BaseReport):
""" Basic test report object (also used for setup and teardown calls if """ Basic test report object (also used for setup and teardown calls if
they fail). they fail).
""" """
def __init__(self, nodeid, location, def __init__(self, nodeid, location,
keywords, outcome, longrepr, when, sections=()): keywords, outcome, longrepr, when, sections=(), duration=0):
#: normalized collection node id #: normalized collection node id
self.nodeid = nodeid self.nodeid = nodeid
@ -193,6 +199,9 @@ class TestReport(BaseReport):
#: marshallable #: marshallable
self.sections = list(sections) self.sections = list(sections)
#: time it took to run just the test
self.duration = duration
def __repr__(self): def __repr__(self):
return "<TestReport %r when=%r outcome=%r>" % ( return "<TestReport %r when=%r outcome=%r>" % (
self.nodeid, self.when, self.outcome) self.nodeid, self.when, self.outcome)