xunit.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. """This plugin provides test results in the standard XUnit XML format.
  2. It's designed for the `Jenkins`_ (previously Hudson) continuous build
  3. system, but will probably work for anything else that understands an
  4. XUnit-formatted XML representation of test results.
  5. Add this shell command to your builder ::
  6. nosetests --with-xunit
  7. And by default a file named nosetests.xml will be written to the
  8. working directory.
  9. In a Jenkins builder, tick the box named "Publish JUnit test result report"
  10. under the Post-build Actions and enter this value for Test report XMLs::
  11. **/nosetests.xml
  12. If you need to change the name or location of the file, you can set the
  13. ``--xunit-file`` option.
  14. If you need to change the name of the test suite, you can set the
  15. ``--xunit-testsuite-name`` option.
  16. Here is an abbreviated version of what an XML test report might look like::
  17. <?xml version="1.0" encoding="UTF-8"?>
  18. <testsuite name="nosetests" tests="1" errors="1" failures="0" skip="0">
  19. <testcase classname="path_to_test_suite.TestSomething"
  20. name="test_it" time="0">
  21. <error type="exceptions.TypeError" message="oops, wrong type">
  22. Traceback (most recent call last):
  23. ...
  24. TypeError: oops, wrong type
  25. </error>
  26. </testcase>
  27. </testsuite>
  28. .. _Jenkins: http://jenkins-ci.org/
  29. """
  30. import codecs
  31. import doctest
  32. import os
  33. import sys
  34. import traceback
  35. import re
  36. import inspect
  37. from StringIO import StringIO
  38. from time import time
  39. from xml.sax import saxutils
  40. from nose.plugins.base import Plugin
  41. from nose.exc import SkipTest
  42. from nose.pyversion import force_unicode, format_exception
  43. # Invalid XML characters, control characters 0-31 sans \t, \n and \r
  44. CONTROL_CHARACTERS = re.compile(r"[\000-\010\013\014\016-\037]")
  45. TEST_ID = re.compile(r'^(.*?)(\(.*\))$')
  46. def xml_safe(value):
  47. """Replaces invalid XML characters with '?'."""
  48. return CONTROL_CHARACTERS.sub('?', value)
  49. def escape_cdata(cdata):
  50. """Escape a string for an XML CDATA section."""
  51. return xml_safe(cdata).replace(']]>', ']]>]]&gt;<![CDATA[')
  52. def id_split(idval):
  53. m = TEST_ID.match(idval)
  54. if m:
  55. name, fargs = m.groups()
  56. head, tail = name.rsplit(".", 1)
  57. return [head, tail+fargs]
  58. else:
  59. return idval.rsplit(".", 1)
  60. def nice_classname(obj):
  61. """Returns a nice name for class object or class instance.
  62. >>> nice_classname(Exception()) # doctest: +ELLIPSIS
  63. '...Exception'
  64. >>> nice_classname(Exception) # doctest: +ELLIPSIS
  65. '...Exception'
  66. """
  67. if inspect.isclass(obj):
  68. cls_name = obj.__name__
  69. else:
  70. cls_name = obj.__class__.__name__
  71. mod = inspect.getmodule(obj)
  72. if mod:
  73. name = mod.__name__
  74. # jython
  75. if name.startswith('org.python.core.'):
  76. name = name[len('org.python.core.'):]
  77. return "%s.%s" % (name, cls_name)
  78. else:
  79. return cls_name
  80. def exc_message(exc_info):
  81. """Return the exception's message."""
  82. exc = exc_info[1]
  83. if exc is None:
  84. # str exception
  85. result = exc_info[0]
  86. else:
  87. try:
  88. result = str(exc)
  89. except UnicodeEncodeError:
  90. try:
  91. result = unicode(exc)
  92. except UnicodeError:
  93. # Fallback to args as neither str nor
  94. # unicode(Exception(u'\xe6')) work in Python < 2.6
  95. result = exc.args[0]
  96. result = force_unicode(result, 'UTF-8')
  97. return xml_safe(result)
  98. class Tee(object):
  99. def __init__(self, encoding, *args):
  100. self._encoding = encoding
  101. self._streams = args
  102. def write(self, data):
  103. data = force_unicode(data, self._encoding)
  104. for s in self._streams:
  105. s.write(data)
  106. def writelines(self, lines):
  107. for line in lines:
  108. self.write(line)
  109. def flush(self):
  110. for s in self._streams:
  111. s.flush()
  112. def isatty(self):
  113. return False
  114. class Xunit(Plugin):
  115. """This plugin provides test results in the standard XUnit XML format."""
  116. name = 'xunit'
  117. score = 1500
  118. encoding = 'UTF-8'
  119. error_report_file = None
  120. def __init__(self):
  121. super(Xunit, self).__init__()
  122. self._capture_stack = []
  123. self._currentStdout = None
  124. self._currentStderr = None
  125. def _timeTaken(self):
  126. if hasattr(self, '_timer'):
  127. taken = time() - self._timer
  128. else:
  129. # test died before it ran (probably error in setup())
  130. # or success/failure added before test started probably
  131. # due to custom TestResult munging
  132. taken = 0.0
  133. return taken
  134. def _quoteattr(self, attr):
  135. """Escape an XML attribute. Value can be unicode."""
  136. attr = xml_safe(attr)
  137. return saxutils.quoteattr(attr)
  138. def options(self, parser, env):
  139. """Sets additional command line options."""
  140. Plugin.options(self, parser, env)
  141. parser.add_option(
  142. '--xunit-file', action='store',
  143. dest='xunit_file', metavar="FILE",
  144. default=env.get('NOSE_XUNIT_FILE', 'nosetests.xml'),
  145. help=("Path to xml file to store the xunit report in. "
  146. "Default is nosetests.xml in the working directory "
  147. "[NOSE_XUNIT_FILE]"))
  148. parser.add_option(
  149. '--xunit-testsuite-name', action='store',
  150. dest='xunit_testsuite_name', metavar="PACKAGE",
  151. default=env.get('NOSE_XUNIT_TESTSUITE_NAME', 'nosetests'),
  152. help=("Name of the testsuite in the xunit xml, generated by plugin. "
  153. "Default test suite name is nosetests."))
  154. def configure(self, options, config):
  155. """Configures the xunit plugin."""
  156. Plugin.configure(self, options, config)
  157. self.config = config
  158. if self.enabled:
  159. self.stats = {'errors': 0,
  160. 'failures': 0,
  161. 'passes': 0,
  162. 'skipped': 0
  163. }
  164. self.errorlist = []
  165. self.error_report_file_name = os.path.realpath(options.xunit_file)
  166. self.xunit_testsuite_name = options.xunit_testsuite_name
  167. def report(self, stream):
  168. """Writes an Xunit-formatted XML file
  169. The file includes a report of test errors and failures.
  170. """
  171. self.error_report_file = codecs.open(self.error_report_file_name, 'w',
  172. self.encoding, 'replace')
  173. self.stats['encoding'] = self.encoding
  174. self.stats['testsuite_name'] = self.xunit_testsuite_name
  175. self.stats['total'] = (self.stats['errors'] + self.stats['failures']
  176. + self.stats['passes'] + self.stats['skipped'])
  177. self.error_report_file.write(
  178. u'<?xml version="1.0" encoding="%(encoding)s"?>'
  179. u'<testsuite name="%(testsuite_name)s" tests="%(total)d" '
  180. u'errors="%(errors)d" failures="%(failures)d" '
  181. u'skip="%(skipped)d">' % self.stats)
  182. self.error_report_file.write(u''.join([force_unicode(e, self.encoding)
  183. for e in self.errorlist]))
  184. self.error_report_file.write(u'</testsuite>')
  185. self.error_report_file.close()
  186. if self.config.verbosity > 1:
  187. stream.writeln("-" * 70)
  188. stream.writeln("XML: %s" % self.error_report_file.name)
  189. def _startCapture(self):
  190. self._capture_stack.append((sys.stdout, sys.stderr))
  191. self._currentStdout = StringIO()
  192. self._currentStderr = StringIO()
  193. sys.stdout = Tee(self.encoding, self._currentStdout, sys.stdout)
  194. sys.stderr = Tee(self.encoding, self._currentStderr, sys.stderr)
  195. def startContext(self, context):
  196. self._startCapture()
  197. def stopContext(self, context):
  198. self._endCapture()
  199. def beforeTest(self, test):
  200. """Initializes a timer before starting a test."""
  201. self._timer = time()
  202. self._startCapture()
  203. def _endCapture(self):
  204. if self._capture_stack:
  205. sys.stdout, sys.stderr = self._capture_stack.pop()
  206. def afterTest(self, test):
  207. self._endCapture()
  208. self._currentStdout = None
  209. self._currentStderr = None
  210. def finalize(self, test):
  211. while self._capture_stack:
  212. self._endCapture()
  213. def _getCapturedStdout(self):
  214. if self._currentStdout:
  215. value = self._currentStdout.getvalue()
  216. if value:
  217. return '<system-out><![CDATA[%s]]></system-out>' % escape_cdata(
  218. value)
  219. return ''
  220. def _getCapturedStderr(self):
  221. if self._currentStderr:
  222. value = self._currentStderr.getvalue()
  223. if value:
  224. return '<system-err><![CDATA[%s]]></system-err>' % escape_cdata(
  225. value)
  226. return ''
  227. def addError(self, test, err, capt=None):
  228. """Add error output to Xunit report.
  229. """
  230. taken = self._timeTaken()
  231. if issubclass(err[0], SkipTest):
  232. type = 'skipped'
  233. self.stats['skipped'] += 1
  234. else:
  235. type = 'error'
  236. self.stats['errors'] += 1
  237. tb = format_exception(err, self.encoding)
  238. id = test.id()
  239. self.errorlist.append(
  240. u'<testcase classname=%(cls)s name=%(name)s time="%(taken).3f">'
  241. u'<%(type)s type=%(errtype)s message=%(message)s><![CDATA[%(tb)s]]>'
  242. u'</%(type)s>%(systemout)s%(systemerr)s</testcase>' %
  243. {'cls': self._quoteattr(id_split(id)[0]),
  244. 'name': self._quoteattr(id_split(id)[-1]),
  245. 'taken': taken,
  246. 'type': type,
  247. 'errtype': self._quoteattr(nice_classname(err[0])),
  248. 'message': self._quoteattr(exc_message(err)),
  249. 'tb': escape_cdata(tb),
  250. 'systemout': self._getCapturedStdout(),
  251. 'systemerr': self._getCapturedStderr(),
  252. })
  253. def addFailure(self, test, err, capt=None, tb_info=None):
  254. """Add failure output to Xunit report.
  255. """
  256. taken = self._timeTaken()
  257. tb = format_exception(err, self.encoding)
  258. self.stats['failures'] += 1
  259. id = test.id()
  260. self.errorlist.append(
  261. u'<testcase classname=%(cls)s name=%(name)s time="%(taken).3f">'
  262. u'<failure type=%(errtype)s message=%(message)s><![CDATA[%(tb)s]]>'
  263. u'</failure>%(systemout)s%(systemerr)s</testcase>' %
  264. {'cls': self._quoteattr(id_split(id)[0]),
  265. 'name': self._quoteattr(id_split(id)[-1]),
  266. 'taken': taken,
  267. 'errtype': self._quoteattr(nice_classname(err[0])),
  268. 'message': self._quoteattr(exc_message(err)),
  269. 'tb': escape_cdata(tb),
  270. 'systemout': self._getCapturedStdout(),
  271. 'systemerr': self._getCapturedStderr(),
  272. })
  273. def addSuccess(self, test, capt=None):
  274. """Add success output to Xunit report.
  275. """
  276. taken = self._timeTaken()
  277. self.stats['passes'] += 1
  278. id = test.id()
  279. self.errorlist.append(
  280. '<testcase classname=%(cls)s name=%(name)s '
  281. 'time="%(taken).3f">%(systemout)s%(systemerr)s</testcase>' %
  282. {'cls': self._quoteattr(id_split(id)[0]),
  283. 'name': self._quoteattr(id_split(id)[-1]),
  284. 'taken': taken,
  285. 'systemout': self._getCapturedStdout(),
  286. 'systemerr': self._getCapturedStderr(),
  287. })