test_testcase.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Direct unit tests for L{twisted.trial.unittest.SynchronousTestCase} and
  5. L{twisted.trial.unittest.TestCase}.
  6. """
  7. from __future__ import division, absolute_import
  8. from twisted.trial.unittest import SynchronousTestCase, TestCase
  9. class TestCaseMixin(object):
  10. """
  11. L{TestCase} tests.
  12. """
  13. def setUp(self):
  14. """
  15. Create a couple instances of C{MyTestCase}, each for the same test
  16. method, to be used in the test methods of this class.
  17. """
  18. self.first = self.MyTestCase('test_1')
  19. self.second = self.MyTestCase('test_1')
  20. def test_equality(self):
  21. """
  22. In order for one test method to be runnable twice, two TestCase
  23. instances with the same test method name must not compare as equal.
  24. """
  25. self.assertTrue(self.first == self.first)
  26. self.assertTrue(self.first != self.second)
  27. self.assertFalse(self.first == self.second)
  28. def test_hashability(self):
  29. """
  30. In order for one test method to be runnable twice, two TestCase
  31. instances with the same test method name should not have the same
  32. hash value.
  33. """
  34. container = {}
  35. container[self.first] = None
  36. container[self.second] = None
  37. self.assertEqual(len(container), 2)
  38. class SynchronousTestCaseTests(TestCaseMixin, SynchronousTestCase):
  39. class MyTestCase(SynchronousTestCase):
  40. """
  41. Some test methods which can be used to test behaviors of
  42. L{SynchronousTestCase}.
  43. """
  44. def test_1(self):
  45. pass
  46. # Yes, subclass SynchronousTestCase again. There are no interesting behaviors
  47. # of self being tested below, only of self.MyTestCase.
  48. class AsynchronousTestCaseTests(TestCaseMixin, SynchronousTestCase):
  49. class MyTestCase(TestCase):
  50. """
  51. Some test methods which can be used to test behaviors of
  52. L{TestCase}.
  53. """
  54. def test_1(self):
  55. pass