itrial.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Interfaces for Trial.
  5. Maintainer: Jonathan Lange
  6. """
  7. from __future__ import division, absolute_import
  8. import zope.interface as zi
  9. from zope.interface import Attribute
  10. class ITestCase(zi.Interface):
  11. """
  12. The interface that a test case must implement in order to be used in Trial.
  13. """
  14. failureException = zi.Attribute(
  15. "The exception class that is raised by failed assertions")
  16. def __call__(result):
  17. """
  18. Run the test. Should always do exactly the same thing as run().
  19. """
  20. def countTestCases():
  21. """
  22. Return the number of tests in this test case. Usually 1.
  23. """
  24. def id():
  25. """
  26. Return a unique identifier for the test, usually the fully-qualified
  27. Python name.
  28. """
  29. def run(result):
  30. """
  31. Run the test, storing the results in C{result}.
  32. @param result: A L{TestResult}.
  33. """
  34. def shortDescription():
  35. """
  36. Return a short description of the test.
  37. """
  38. class IReporter(zi.Interface):
  39. """
  40. I report results from a run of a test suite.
  41. """
  42. stream = zi.Attribute(
  43. "Deprecated in Twisted 8.0. "
  44. "The io-stream that this reporter will write to")
  45. tbformat = zi.Attribute("Either 'default', 'brief', or 'verbose'")
  46. args = zi.Attribute(
  47. "Additional string argument passed from the command line")
  48. shouldStop = zi.Attribute(
  49. """
  50. A boolean indicating that this reporter would like the test run to stop.
  51. """)
  52. separator = Attribute(
  53. "Deprecated in Twisted 8.0. "
  54. "A value which will occasionally be passed to the L{write} method.")
  55. testsRun = Attribute(
  56. """
  57. The number of tests that seem to have been run according to this
  58. reporter.
  59. """)
  60. def startTest(method):
  61. """
  62. Report the beginning of a run of a single test method.
  63. @param method: an object that is adaptable to ITestMethod
  64. """
  65. def stopTest(method):
  66. """
  67. Report the status of a single test method
  68. @param method: an object that is adaptable to ITestMethod
  69. """
  70. def startSuite(name):
  71. """
  72. Deprecated in Twisted 8.0.
  73. Suites which wish to appear in reporter output should call this
  74. before running their tests.
  75. """
  76. def endSuite(name):
  77. """
  78. Deprecated in Twisted 8.0.
  79. Called at the end of a suite, if and only if that suite has called
  80. C{startSuite}.
  81. """
  82. def cleanupErrors(errs):
  83. """
  84. Deprecated in Twisted 8.0.
  85. Called when the reactor has been left in a 'dirty' state
  86. @param errs: a list of L{twisted.python.failure.Failure}s
  87. """
  88. def upDownError(userMeth, warn=True, printStatus=True):
  89. """
  90. Deprecated in Twisted 8.0.
  91. Called when an error occurs in a setUp* or tearDown* method
  92. @param warn: indicates whether or not the reporter should emit a
  93. warning about the error
  94. @type warn: Boolean
  95. @param printStatus: indicates whether or not the reporter should
  96. print the name of the method and the status
  97. message appropriate for the type of error
  98. @type printStatus: Boolean
  99. """
  100. def addSuccess(test):
  101. """
  102. Record that test passed.
  103. """
  104. def addError(test, error):
  105. """
  106. Record that a test has raised an unexpected exception.
  107. @param test: The test that has raised an error.
  108. @param error: The error that the test raised. It will either be a
  109. three-tuple in the style of C{sys.exc_info()} or a
  110. L{Failure<twisted.python.failure.Failure>} object.
  111. """
  112. def addFailure(test, failure):
  113. """
  114. Record that a test has failed with the given failure.
  115. @param test: The test that has failed.
  116. @param failure: The failure that the test failed with. It will
  117. either be a three-tuple in the style of C{sys.exc_info()}
  118. or a L{Failure<twisted.python.failure.Failure>} object.
  119. """
  120. def addExpectedFailure(test, failure, todo=None):
  121. """
  122. Record that the given test failed, and was expected to do so.
  123. In Twisted 15.5 and prior, C{todo} was a mandatory parameter.
  124. @type test: L{unittest.TestCase}
  125. @param test: The test which this is about.
  126. @type error: L{failure.Failure}
  127. @param error: The error which this test failed with.
  128. @type todo: L{unittest.Todo}
  129. @param todo: The reason for the test's TODO status. If L{None}, a
  130. generic reason is used.
  131. """
  132. def addUnexpectedSuccess(test, todo=None):
  133. """
  134. Record that the given test failed, and was expected to do so.
  135. In Twisted 15.5 and prior, C{todo} was a mandatory parameter.
  136. @type test: L{unittest.TestCase}
  137. @param test: The test which this is about.
  138. @type todo: L{unittest.Todo}
  139. @param todo: The reason for the test's TODO status. If L{None}, a
  140. generic reason is used.
  141. """
  142. def addSkip(test, reason):
  143. """
  144. Record that a test has been skipped for the given reason.
  145. @param test: The test that has been skipped.
  146. @param reason: An object that the test case has specified as the reason
  147. for skipping the test.
  148. """
  149. def printSummary():
  150. """
  151. Deprecated in Twisted 8.0, use L{done} instead.
  152. Present a summary of the test results.
  153. """
  154. def printErrors():
  155. """
  156. Deprecated in Twisted 8.0, use L{done} instead.
  157. Present the errors that have occurred during the test run. This method
  158. will be called after all tests have been run.
  159. """
  160. def write(string):
  161. """
  162. Deprecated in Twisted 8.0, use L{done} instead.
  163. Display a string to the user, without appending a new line.
  164. """
  165. def writeln(string):
  166. """
  167. Deprecated in Twisted 8.0, use L{done} instead.
  168. Display a string to the user, appending a new line.
  169. """
  170. def wasSuccessful():
  171. """
  172. Return a boolean indicating whether all test results that were reported
  173. to this reporter were successful or not.
  174. """
  175. def done():
  176. """
  177. Called when the test run is complete.
  178. This gives the result object an opportunity to display a summary of
  179. information to the user. Once you have called C{done} on an
  180. L{IReporter} object, you should assume that the L{IReporter} object is
  181. no longer usable.
  182. """