distreporter.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # -*- test-case-name: twisted.trial._dist.test.test_distreporter -*-
  2. #
  3. # Copyright (c) Twisted Matrix Laboratories.
  4. # See LICENSE for details.
  5. """
  6. The reporter is not made to support concurrent test running, so we will
  7. hold test results in here and only send them to the reporter once the
  8. test is over.
  9. @since: 12.3
  10. """
  11. from types import TracebackType
  12. from typing import Optional, Tuple, Union
  13. from zope.interface import implementer
  14. from twisted.python.components import proxyForInterface
  15. from twisted.python.failure import Failure
  16. from ..itrial import IReporter, ITestCase
  17. ReporterFailure = Union[Failure, Tuple[type, Exception, TracebackType]]
  18. @implementer(IReporter)
  19. class DistReporter(proxyForInterface(IReporter)): # type: ignore[misc]
  20. """
  21. See module docstring.
  22. """
  23. def __init__(self, original):
  24. super().__init__(original)
  25. self.running = {}
  26. def startTest(self, test):
  27. """
  28. Queue test starting.
  29. """
  30. self.running[test.id()] = []
  31. self.running[test.id()].append((self.original.startTest, test))
  32. def addFailure(self, test: ITestCase, fail: ReporterFailure) -> None:
  33. """
  34. Queue adding a failure.
  35. """
  36. self.running[test.id()].append((self.original.addFailure, test, fail))
  37. def addError(self, test: ITestCase, error: ReporterFailure) -> None:
  38. """
  39. Queue error adding.
  40. """
  41. self.running[test.id()].append((self.original.addError, test, error))
  42. def addSkip(self, test, reason):
  43. """
  44. Queue adding a skip.
  45. """
  46. self.running[test.id()].append((self.original.addSkip, test, reason))
  47. def addUnexpectedSuccess(self, test, todo=None):
  48. """
  49. Queue adding an unexpected success.
  50. """
  51. self.running[test.id()].append((self.original.addUnexpectedSuccess, test, todo))
  52. def addExpectedFailure(
  53. self, test: ITestCase, error: ReporterFailure, todo: Optional[str] = None
  54. ) -> None:
  55. """
  56. Queue adding an expected failure.
  57. """
  58. self.running[test.id()].append(
  59. (self.original.addExpectedFailure, test, error, todo)
  60. )
  61. def addSuccess(self, test):
  62. """
  63. Queue adding a success.
  64. """
  65. self.running[test.id()].append((self.original.addSuccess, test))
  66. def stopTest(self, test):
  67. """
  68. Queue stopping the test, then unroll the queue.
  69. """
  70. self.running[test.id()].append((self.original.stopTest, test))
  71. for step in self.running[test.id()]:
  72. step[0](*step[1:])
  73. del self.running[test.id()]