test_suppression.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Tests for warning suppression features of Trial.
  5. """
  6. from __future__ import division, absolute_import
  7. import unittest as pyunit
  8. from twisted.python.reflect import namedAny
  9. from twisted.trial import unittest
  10. from twisted.trial.test import suppression
  11. class SuppressionMixin(object):
  12. """
  13. Tests for the warning suppression features of
  14. L{twisted.trial.unittest.SynchronousTestCase}.
  15. """
  16. def runTests(self, suite):
  17. suite.run(pyunit.TestResult())
  18. def _load(self, cls, methodName):
  19. """
  20. Return a new L{unittest.TestSuite} with a single test method in it.
  21. @param cls: A L{TestCase} subclass defining a test method.
  22. @param methodName: The name of the test method from C{cls}.
  23. """
  24. return pyunit.TestSuite([cls(methodName)])
  25. def _assertWarnings(self, warnings, which):
  26. """
  27. Assert that a certain number of warnings with certain messages were
  28. emitted in a certain order.
  29. @param warnings: A list of emitted warnings, as returned by
  30. C{flushWarnings}.
  31. @param which: A list of strings giving warning messages that should
  32. appear in C{warnings}.
  33. @raise self.failureException: If the warning messages given by C{which}
  34. do not match the messages in the warning information in C{warnings},
  35. or if they do not appear in the same order.
  36. """
  37. self.assertEqual(
  38. [warning['message'] for warning in warnings],
  39. which)
  40. def test_setUpSuppression(self):
  41. """
  42. Suppressions defined by the test method being run are applied to any
  43. warnings emitted while running the C{setUp} fixture.
  44. """
  45. self.runTests(
  46. self._load(self.TestSetUpSuppression, "testSuppressMethod"))
  47. warningsShown = self.flushWarnings([
  48. self.TestSetUpSuppression._emit])
  49. self._assertWarnings(
  50. warningsShown,
  51. [suppression.CLASS_WARNING_MSG, suppression.MODULE_WARNING_MSG,
  52. suppression.CLASS_WARNING_MSG, suppression.MODULE_WARNING_MSG])
  53. def test_tearDownSuppression(self):
  54. """
  55. Suppressions defined by the test method being run are applied to any
  56. warnings emitted while running the C{tearDown} fixture.
  57. """
  58. self.runTests(
  59. self._load(self.TestTearDownSuppression, "testSuppressMethod"))
  60. warningsShown = self.flushWarnings([
  61. self.TestTearDownSuppression._emit])
  62. self._assertWarnings(
  63. warningsShown,
  64. [suppression.CLASS_WARNING_MSG, suppression.MODULE_WARNING_MSG,
  65. suppression.CLASS_WARNING_MSG, suppression.MODULE_WARNING_MSG])
  66. def test_suppressMethod(self):
  67. """
  68. A suppression set on a test method prevents warnings emitted by that
  69. test method which the suppression matches from being emitted.
  70. """
  71. self.runTests(
  72. self._load(self.TestSuppression, "testSuppressMethod"))
  73. warningsShown = self.flushWarnings([
  74. self.TestSuppression._emit])
  75. self._assertWarnings(
  76. warningsShown,
  77. [suppression.CLASS_WARNING_MSG, suppression.MODULE_WARNING_MSG])
  78. def test_suppressClass(self):
  79. """
  80. A suppression set on a L{SynchronousTestCase} subclass prevents warnings
  81. emitted by any test methods defined on that class which match the
  82. suppression from being emitted.
  83. """
  84. self.runTests(
  85. self._load(self.TestSuppression, "testSuppressClass"))
  86. warningsShown = self.flushWarnings([
  87. self.TestSuppression._emit])
  88. self.assertEqual(
  89. warningsShown[0]['message'], suppression.METHOD_WARNING_MSG)
  90. self.assertEqual(
  91. warningsShown[1]['message'], suppression.MODULE_WARNING_MSG)
  92. self.assertEqual(len(warningsShown), 2)
  93. def test_suppressModule(self):
  94. """
  95. A suppression set on a module prevents warnings emitted by any test
  96. mewthods defined in that module which match the suppression from being
  97. emitted.
  98. """
  99. self.runTests(
  100. self._load(self.TestSuppression2, "testSuppressModule"))
  101. warningsShown = self.flushWarnings([
  102. self.TestSuppression._emit])
  103. self.assertEqual(
  104. warningsShown[0]['message'], suppression.METHOD_WARNING_MSG)
  105. self.assertEqual(
  106. warningsShown[1]['message'], suppression.CLASS_WARNING_MSG)
  107. self.assertEqual(len(warningsShown), 2)
  108. def test_overrideSuppressClass(self):
  109. """
  110. The suppression set on a test method completely overrides a suppression
  111. with wider scope; if it does not match a warning emitted by that test
  112. method, the warning is emitted, even if a wider suppression matches.
  113. """
  114. self.runTests(
  115. self._load(self.TestSuppression, "testOverrideSuppressClass"))
  116. warningsShown = self.flushWarnings([
  117. self.TestSuppression._emit])
  118. self.assertEqual(
  119. warningsShown[0]['message'], suppression.METHOD_WARNING_MSG)
  120. self.assertEqual(
  121. warningsShown[1]['message'], suppression.CLASS_WARNING_MSG)
  122. self.assertEqual(
  123. warningsShown[2]['message'], suppression.MODULE_WARNING_MSG)
  124. self.assertEqual(len(warningsShown), 3)
  125. class SynchronousSuppressionTests(SuppressionMixin, unittest.SynchronousTestCase):
  126. """
  127. @see: L{twisted.trial.test.test_tests}
  128. """
  129. TestSetUpSuppression = namedAny(
  130. 'twisted.trial.test.suppression.SynchronousTestSetUpSuppression')
  131. TestTearDownSuppression = namedAny(
  132. 'twisted.trial.test.suppression.SynchronousTestTearDownSuppression')
  133. TestSuppression = namedAny(
  134. 'twisted.trial.test.suppression.SynchronousTestSuppression')
  135. TestSuppression2 = namedAny(
  136. 'twisted.trial.test.suppression.SynchronousTestSuppression2')