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