PageRenderTime 223ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/lib-python/2.7/unittest/result.py

http://github.com/pypy/pypy
Python | 193 lines | 156 code | 17 blank | 20 comment | 9 complexity | c7ea1955d5803f5f564228a444df899c MD5 | raw file
  1. """Test result object"""
  2. import os
  3. import sys
  4. import traceback
  5. from StringIO import StringIO
  6. from . import util
  7. from functools import wraps
  8. __unittest = True
  9. def failfast(method):
  10. @wraps(method)
  11. def inner(self, *args, **kw):
  12. if getattr(self, 'failfast', False):
  13. self.stop()
  14. return method(self, *args, **kw)
  15. return inner
  16. STDOUT_LINE = '\nStdout:\n%s'
  17. STDERR_LINE = '\nStderr:\n%s'
  18. class TestResult(object):
  19. """Holder for test result information.
  20. Test results are automatically managed by the TestCase and TestSuite
  21. classes, and do not need to be explicitly manipulated by writers of tests.
  22. Each instance holds the total number of tests run, and collections of
  23. failures and errors that occurred among those test runs. The collections
  24. contain tuples of (testcase, exceptioninfo), where exceptioninfo is the
  25. formatted traceback of the error that occurred.
  26. """
  27. _previousTestClass = None
  28. _testRunEntered = False
  29. _moduleSetUpFailed = False
  30. def __init__(self, stream=None, descriptions=None, verbosity=None):
  31. self.failfast = False
  32. self.failures = []
  33. self.errors = []
  34. self.testsRun = 0
  35. self.skipped = []
  36. self.expectedFailures = []
  37. self.unexpectedSuccesses = []
  38. self.shouldStop = False
  39. self.buffer = False
  40. self._stdout_buffer = None
  41. self._stderr_buffer = None
  42. self._original_stdout = sys.stdout
  43. self._original_stderr = sys.stderr
  44. self._mirrorOutput = False
  45. def printErrors(self):
  46. "Called by TestRunner after test run"
  47. def startTest(self, test):
  48. "Called when the given test is about to be run"
  49. self.testsRun += 1
  50. self._mirrorOutput = False
  51. self._setupStdout()
  52. def _setupStdout(self):
  53. if self.buffer:
  54. if self._stderr_buffer is None:
  55. self._stderr_buffer = StringIO()
  56. self._stdout_buffer = StringIO()
  57. sys.stdout = self._stdout_buffer
  58. sys.stderr = self._stderr_buffer
  59. def startTestRun(self):
  60. """Called once before any tests are executed.
  61. See startTest for a method called before each test.
  62. """
  63. def stopTest(self, test):
  64. """Called when the given test has been run"""
  65. self._restoreStdout()
  66. self._mirrorOutput = False
  67. def _restoreStdout(self):
  68. if self.buffer:
  69. if self._mirrorOutput:
  70. output = sys.stdout.getvalue()
  71. error = sys.stderr.getvalue()
  72. if output:
  73. if not output.endswith('\n'):
  74. output += '\n'
  75. self._original_stdout.write(STDOUT_LINE % output)
  76. if error:
  77. if not error.endswith('\n'):
  78. error += '\n'
  79. self._original_stderr.write(STDERR_LINE % error)
  80. sys.stdout = self._original_stdout
  81. sys.stderr = self._original_stderr
  82. self._stdout_buffer.seek(0)
  83. self._stdout_buffer.truncate()
  84. self._stderr_buffer.seek(0)
  85. self._stderr_buffer.truncate()
  86. def stopTestRun(self):
  87. """Called once after all tests are executed.
  88. See stopTest for a method called after each test.
  89. """
  90. @failfast
  91. def addError(self, test, err):
  92. """Called when an error has occurred. 'err' is a tuple of values as
  93. returned by sys.exc_info().
  94. """
  95. self.errors.append((test, self._exc_info_to_string(err, test)))
  96. self._mirrorOutput = True
  97. @failfast
  98. def addFailure(self, test, err):
  99. """Called when an error has occurred. 'err' is a tuple of values as
  100. returned by sys.exc_info()."""
  101. self.failures.append((test, self._exc_info_to_string(err, test)))
  102. self._mirrorOutput = True
  103. def addSuccess(self, test):
  104. "Called when a test has completed successfully"
  105. pass
  106. def addSkip(self, test, reason):
  107. """Called when a test is skipped."""
  108. self.skipped.append((test, reason))
  109. def addExpectedFailure(self, test, err):
  110. """Called when an expected failure/error occured."""
  111. self.expectedFailures.append(
  112. (test, self._exc_info_to_string(err, test)))
  113. @failfast
  114. def addUnexpectedSuccess(self, test):
  115. """Called when a test was expected to fail, but succeed."""
  116. self.unexpectedSuccesses.append(test)
  117. def wasSuccessful(self):
  118. "Tells whether or not this result was a success"
  119. return len(self.failures) == len(self.errors) == 0
  120. def stop(self):
  121. "Indicates that the tests should be aborted"
  122. self.shouldStop = True
  123. def _exc_info_to_string(self, err, test):
  124. """Converts a sys.exc_info()-style tuple of values into a string."""
  125. exctype, value, tb = err
  126. # Skip test runner traceback levels
  127. while tb and self._is_relevant_tb_level(tb):
  128. tb = tb.tb_next
  129. if exctype is test.failureException:
  130. # Skip assert*() traceback levels
  131. length = self._count_relevant_tb_levels(tb)
  132. msgLines = traceback.format_exception(exctype, value, tb, length)
  133. else:
  134. msgLines = traceback.format_exception(exctype, value, tb)
  135. if self.buffer:
  136. output = sys.stdout.getvalue()
  137. error = sys.stderr.getvalue()
  138. if output:
  139. if not output.endswith('\n'):
  140. output += '\n'
  141. msgLines.append(STDOUT_LINE % output)
  142. if error:
  143. if not error.endswith('\n'):
  144. error += '\n'
  145. msgLines.append(STDERR_LINE % error)
  146. return ''.join(msgLines)
  147. def _is_relevant_tb_level(self, tb):
  148. return '__unittest' in tb.tb_frame.f_globals
  149. def _count_relevant_tb_levels(self, tb):
  150. length = 0
  151. while tb and not self._is_relevant_tb_level(tb):
  152. length += 1
  153. tb = tb.tb_next
  154. return length
  155. def __repr__(self):
  156. return ("<%s run=%i errors=%i failures=%i>" %
  157. (util.strclass(self.__class__), self.testsRun, len(self.errors),
  158. len(self.failures)))