3
from .support import LoggingResult
6
class Test_FunctionTestCase(unittest.TestCase):
8
# "Return the number of tests represented by the this test object. For
9
# TestCase instances, this will always be 1"
10
def test_countTestCases(self):
11
test = unittest.FunctionTestCase(lambda: None)
13
self.assertEqual(test.countTestCases(), 1)
15
# "When a setUp() method is defined, the test runner will run that method
16
# prior to each test. Likewise, if a tearDown() method is defined, the
17
# test runner will invoke that method after each test. In the example,
18
# setUp() was used to create a fresh sequence for each test."
20
# Make sure the proper call order is maintained, even if setUp() raises
22
def test_run_call_order__error_in_setUp(self):
24
result = LoggingResult(events)
27
events.append('setUp')
28
raise RuntimeError('raised by setUp')
34
events.append('tearDown')
36
expected = ['startTest', 'setUp', 'addError', 'stopTest']
37
unittest.FunctionTestCase(test, setUp, tearDown).run(result)
38
self.assertEqual(events, expected)
40
# "When a setUp() method is defined, the test runner will run that method
41
# prior to each test. Likewise, if a tearDown() method is defined, the
42
# test runner will invoke that method after each test. In the example,
43
# setUp() was used to create a fresh sequence for each test."
45
# Make sure the proper call order is maintained, even if the test raises
46
# an error (as opposed to a failure).
47
def test_run_call_order__error_in_test(self):
49
result = LoggingResult(events)
52
events.append('setUp')
56
raise RuntimeError('raised by test')
59
events.append('tearDown')
61
expected = ['startTest', 'setUp', 'test', 'addError', 'tearDown',
63
unittest.FunctionTestCase(test, setUp, tearDown).run(result)
64
self.assertEqual(events, expected)
66
# "When a setUp() method is defined, the test runner will run that method
67
# prior to each test. Likewise, if a tearDown() method is defined, the
68
# test runner will invoke that method after each test. In the example,
69
# setUp() was used to create a fresh sequence for each test."
71
# Make sure the proper call order is maintained, even if the test signals
72
# a failure (as opposed to an error).
73
def test_run_call_order__failure_in_test(self):
75
result = LoggingResult(events)
78
events.append('setUp')
82
self.fail('raised by test')
85
events.append('tearDown')
87
expected = ['startTest', 'setUp', 'test', 'addFailure', 'tearDown',
89
unittest.FunctionTestCase(test, setUp, tearDown).run(result)
90
self.assertEqual(events, expected)
92
# "When a setUp() method is defined, the test runner will run that method
93
# prior to each test. Likewise, if a tearDown() method is defined, the
94
# test runner will invoke that method after each test. In the example,
95
# setUp() was used to create a fresh sequence for each test."
97
# Make sure the proper call order is maintained, even if tearDown() raises
99
def test_run_call_order__error_in_tearDown(self):
101
result = LoggingResult(events)
104
events.append('setUp')
107
events.append('test')
110
events.append('tearDown')
111
raise RuntimeError('raised by tearDown')
113
expected = ['startTest', 'setUp', 'test', 'tearDown', 'addError',
115
unittest.FunctionTestCase(test, setUp, tearDown).run(result)
116
self.assertEqual(events, expected)
118
# "Return a string identifying the specific test case."
120
# Because of the vague nature of the docs, I'm not going to lock this
121
# test down too much. Really all that can be asserted is that the id()
122
# will be a string (either 8-byte or unicode -- again, because the docs
125
test = unittest.FunctionTestCase(lambda: None)
127
self.assertIsInstance(test.id(), basestring)
129
# "Returns a one-line description of the test, or None if no description
130
# has been provided. The default implementation of this method returns
131
# the first line of the test method's docstring, if available, or None."
132
def test_shortDescription__no_docstring(self):
133
test = unittest.FunctionTestCase(lambda: None)
135
self.assertEqual(test.shortDescription(), None)
137
# "Returns a one-line description of the test, or None if no description
138
# has been provided. The default implementation of this method returns
139
# the first line of the test method's docstring, if available, or None."
140
def test_shortDescription__singleline_docstring(self):
141
desc = "this tests foo"
142
test = unittest.FunctionTestCase(lambda: None, description=desc)
144
self.assertEqual(test.shortDescription(), "this tests foo")
147
if __name__ == '__main__':