]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - third_party/Python/module/unittest2/unittest2/test/test_functiontestcase.py
Vendor import of lldb trunk r256945:
[FreeBSD/FreeBSD.git] / third_party / Python / module / unittest2 / unittest2 / test / test_functiontestcase.py
1 import unittest2
2 import six
3
4 from unittest2.test.support import LoggingResult
5
6
7 class Test_FunctionTestCase(unittest2.TestCase):
8
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)
13
14         self.assertEqual(test.countTestCases(), 1)
15
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."
20     #
21     # Make sure the proper call order is maintained, even if setUp() raises
22     # an exception.
23     def test_run_call_order__error_in_setUp(self):
24         events = []
25         result = LoggingResult(events)
26
27         def setUp():
28             events.append('setUp')
29             raise RuntimeError('raised by setUp')
30
31         def test():
32             events.append('test')
33
34         def tearDown():
35             events.append('tearDown')
36
37         expected = ['startTest', 'setUp', 'addError', 'stopTest']
38         unittest2.FunctionTestCase(test, setUp, tearDown).run(result)
39         self.assertEqual(events, expected)
40
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."
45     #
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):
49         events = []
50         result = LoggingResult(events)
51
52         def setUp():
53             events.append('setUp')
54
55         def test():
56             events.append('test')
57             raise RuntimeError('raised by test')
58
59         def tearDown():
60             events.append('tearDown')
61
62         expected = ['startTest', 'setUp', 'test', 'addError', 'tearDown',
63                     'stopTest']
64         unittest2.FunctionTestCase(test, setUp, tearDown).run(result)
65         self.assertEqual(events, expected)
66
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."
71     #
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):
75         events = []
76         result = LoggingResult(events)
77
78         def setUp():
79             events.append('setUp')
80
81         def test():
82             events.append('test')
83             self.fail('raised by test')
84
85         def tearDown():
86             events.append('tearDown')
87
88         expected = ['startTest', 'setUp', 'test', 'addFailure', 'tearDown',
89                     'stopTest']
90         unittest2.FunctionTestCase(test, setUp, tearDown).run(result)
91         self.assertEqual(events, expected)
92
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."
97     #
98     # Make sure the proper call order is maintained, even if tearDown() raises
99     # an exception.
100     def test_run_call_order__error_in_tearDown(self):
101         events = []
102         result = LoggingResult(events)
103
104         def setUp():
105             events.append('setUp')
106
107         def test():
108             events.append('test')
109
110         def tearDown():
111             events.append('tearDown')
112             raise RuntimeError('raised by tearDown')
113
114         expected = ['startTest', 'setUp', 'test', 'tearDown', 'addError',
115                     'stopTest']
116         unittest2.FunctionTestCase(test, setUp, tearDown).run(result)
117         self.assertEqual(events, expected)
118
119     # "Return a string identifying the specific test case."
120     #
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
124     # just say "string")
125     def test_id(self):
126         test = unittest2.FunctionTestCase(lambda: None)
127
128         self.assertIsInstance(test.id(), six.string_types)
129
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)
135
136         self.assertEqual(test.shortDescription(), None)
137
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)
144
145         self.assertEqual(test.shortDescription(), "this tests foo")
146
147
148
149 if __name__ == '__main__':
150     unittest2.main()