4 from unittest2.test.support import LoggingResult
7 class Test_FunctionTestCase(unittest2.TestCase):
9 # "Return the number of tests represented by the this test object. For
10 # unittest2.TestCase instances, this will always be 1"
11 def test_countTestCases(self):
12 test = unittest2.FunctionTestCase(lambda: None)
14 self.assertEqual(test.countTestCases(), 1)
16 # "When a setUp() method is defined, the test runner will run that method
17 # prior to each test. Likewise, if a tearDown() method is defined, the
18 # test runner will invoke that method after each test. In the example,
19 # setUp() was used to create a fresh sequence for each test."
21 # Make sure the proper call order is maintained, even if setUp() raises
23 def test_run_call_order__error_in_setUp(self):
25 result = LoggingResult(events)
28 events.append('setUp')
29 raise RuntimeError('raised by setUp')
35 events.append('tearDown')
37 expected = ['startTest', 'setUp', 'addError', 'stopTest']
38 unittest2.FunctionTestCase(test, setUp, tearDown).run(result)
39 self.assertEqual(events, expected)
41 # "When a setUp() method is defined, the test runner will run that method
42 # prior to each test. Likewise, if a tearDown() method is defined, the
43 # test runner will invoke that method after each test. In the example,
44 # setUp() was used to create a fresh sequence for each test."
46 # Make sure the proper call order is maintained, even if the test raises
47 # an error (as opposed to a failure).
48 def test_run_call_order__error_in_test(self):
50 result = LoggingResult(events)
53 events.append('setUp')
57 raise RuntimeError('raised by test')
60 events.append('tearDown')
62 expected = ['startTest', 'setUp', 'test', 'addError', 'tearDown',
64 unittest2.FunctionTestCase(test, setUp, tearDown).run(result)
65 self.assertEqual(events, expected)
67 # "When a setUp() method is defined, the test runner will run that method
68 # prior to each test. Likewise, if a tearDown() method is defined, the
69 # test runner will invoke that method after each test. In the example,
70 # setUp() was used to create a fresh sequence for each test."
72 # Make sure the proper call order is maintained, even if the test signals
73 # a failure (as opposed to an error).
74 def test_run_call_order__failure_in_test(self):
76 result = LoggingResult(events)
79 events.append('setUp')
83 self.fail('raised by test')
86 events.append('tearDown')
88 expected = ['startTest', 'setUp', 'test', 'addFailure', 'tearDown',
90 unittest2.FunctionTestCase(test, setUp, tearDown).run(result)
91 self.assertEqual(events, expected)
93 # "When a setUp() method is defined, the test runner will run that method
94 # prior to each test. Likewise, if a tearDown() method is defined, the
95 # test runner will invoke that method after each test. In the example,
96 # setUp() was used to create a fresh sequence for each test."
98 # Make sure the proper call order is maintained, even if tearDown() raises
100 def test_run_call_order__error_in_tearDown(self):
102 result = LoggingResult(events)
105 events.append('setUp')
108 events.append('test')
111 events.append('tearDown')
112 raise RuntimeError('raised by tearDown')
114 expected = ['startTest', 'setUp', 'test', 'tearDown', 'addError',
116 unittest2.FunctionTestCase(test, setUp, tearDown).run(result)
117 self.assertEqual(events, expected)
119 # "Return a string identifying the specific test case."
121 # Because of the vague nature of the docs, I'm not going to lock this
122 # test down too much. Really all that can be asserted is that the id()
123 # will be a string (either 8-byte or unicode -- again, because the docs
126 test = unittest2.FunctionTestCase(lambda: None)
128 self.assertIsInstance(test.id(), six.string_types)
130 # "Returns a one-line description of the test, or None if no description
131 # has been provided. The default implementation of this method returns
132 # the first line of the test method's docstring, if available, or None."
133 def test_shortDescription__no_docstring(self):
134 test = unittest2.FunctionTestCase(lambda: None)
136 self.assertEqual(test.shortDescription(), None)
138 # "Returns a one-line description of the test, or None if no description
139 # has been provided. The default implementation of this method returns
140 # the first line of the test method's docstring, if available, or None."
141 def test_shortDescription__singleline_docstring(self):
142 desc = "this tests foo"
143 test = unittest2.FunctionTestCase(lambda: None, description=desc)
145 self.assertEqual(test.shortDescription(), "this tests foo")
149 if __name__ == '__main__':