test_functiontestcase.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import unittest
  2. from unittest.test.support import LoggingResult
  3. class Test_FunctionTestCase(unittest.TestCase):
  4. # "Return the number of tests represented by the this test object. For
  5. # TestCase instances, this will always be 1"
  6. def test_countTestCases(self):
  7. test = unittest.FunctionTestCase(lambda: None)
  8. self.assertEqual(test.countTestCases(), 1)
  9. # "When a setUp() method is defined, the test runner will run that method
  10. # prior to each test. Likewise, if a tearDown() method is defined, the
  11. # test runner will invoke that method after each test. In the example,
  12. # setUp() was used to create a fresh sequence for each test."
  13. #
  14. # Make sure the proper call order is maintained, even if setUp() raises
  15. # an exception.
  16. def test_run_call_order__error_in_setUp(self):
  17. events = []
  18. result = LoggingResult(events)
  19. def setUp():
  20. events.append('setUp')
  21. raise RuntimeError('raised by setUp')
  22. def test():
  23. events.append('test')
  24. def tearDown():
  25. events.append('tearDown')
  26. expected = ['startTest', 'setUp', 'addError', 'stopTest']
  27. unittest.FunctionTestCase(test, setUp, tearDown).run(result)
  28. self.assertEqual(events, expected)
  29. # "When a setUp() method is defined, the test runner will run that method
  30. # prior to each test. Likewise, if a tearDown() method is defined, the
  31. # test runner will invoke that method after each test. In the example,
  32. # setUp() was used to create a fresh sequence for each test."
  33. #
  34. # Make sure the proper call order is maintained, even if the test raises
  35. # an error (as opposed to a failure).
  36. def test_run_call_order__error_in_test(self):
  37. events = []
  38. result = LoggingResult(events)
  39. def setUp():
  40. events.append('setUp')
  41. def test():
  42. events.append('test')
  43. raise RuntimeError('raised by test')
  44. def tearDown():
  45. events.append('tearDown')
  46. expected = ['startTest', 'setUp', 'test', 'tearDown',
  47. 'addError', 'stopTest']
  48. unittest.FunctionTestCase(test, setUp, tearDown).run(result)
  49. self.assertEqual(events, expected)
  50. # "When a setUp() method is defined, the test runner will run that method
  51. # prior to each test. Likewise, if a tearDown() method is defined, the
  52. # test runner will invoke that method after each test. In the example,
  53. # setUp() was used to create a fresh sequence for each test."
  54. #
  55. # Make sure the proper call order is maintained, even if the test signals
  56. # a failure (as opposed to an error).
  57. def test_run_call_order__failure_in_test(self):
  58. events = []
  59. result = LoggingResult(events)
  60. def setUp():
  61. events.append('setUp')
  62. def test():
  63. events.append('test')
  64. self.fail('raised by test')
  65. def tearDown():
  66. events.append('tearDown')
  67. expected = ['startTest', 'setUp', 'test', 'tearDown',
  68. 'addFailure', 'stopTest']
  69. unittest.FunctionTestCase(test, setUp, tearDown).run(result)
  70. self.assertEqual(events, expected)
  71. # "When a setUp() method is defined, the test runner will run that method
  72. # prior to each test. Likewise, if a tearDown() method is defined, the
  73. # test runner will invoke that method after each test. In the example,
  74. # setUp() was used to create a fresh sequence for each test."
  75. #
  76. # Make sure the proper call order is maintained, even if tearDown() raises
  77. # an exception.
  78. def test_run_call_order__error_in_tearDown(self):
  79. events = []
  80. result = LoggingResult(events)
  81. def setUp():
  82. events.append('setUp')
  83. def test():
  84. events.append('test')
  85. def tearDown():
  86. events.append('tearDown')
  87. raise RuntimeError('raised by tearDown')
  88. expected = ['startTest', 'setUp', 'test', 'tearDown', 'addError',
  89. 'stopTest']
  90. unittest.FunctionTestCase(test, setUp, tearDown).run(result)
  91. self.assertEqual(events, expected)
  92. # "Return a string identifying the specific test case."
  93. #
  94. # Because of the vague nature of the docs, I'm not going to lock this
  95. # test down too much. Really all that can be asserted is that the id()
  96. # will be a string (either 8-byte or unicode -- again, because the docs
  97. # just say "string")
  98. def test_id(self):
  99. test = unittest.FunctionTestCase(lambda: None)
  100. self.assertIsInstance(test.id(), str)
  101. # "Returns a one-line description of the test, or None if no description
  102. # has been provided. The default implementation of this method returns
  103. # the first line of the test method's docstring, if available, or None."
  104. def test_shortDescription__no_docstring(self):
  105. test = unittest.FunctionTestCase(lambda: None)
  106. self.assertEqual(test.shortDescription(), None)
  107. # "Returns a one-line description of the test, or None if no description
  108. # has been provided. The default implementation of this method returns
  109. # the first line of the test method's docstring, if available, or None."
  110. def test_shortDescription__singleline_docstring(self):
  111. desc = "this tests foo"
  112. test = unittest.FunctionTestCase(lambda: None, description=desc)
  113. self.assertEqual(test.shortDescription(), "this tests foo")
  114. if __name__ == "__main__":
  115. unittest.main()