xref: /openbsd-src/gnu/llvm/lldb/third_party/Python/module/unittest2/unittest2/test/test_suite.py (revision 061da546b983eb767bad15e67af1174fb0bcf31c)
1from unittest2.test.support import EqualityMixin, LoggingResult
2
3import sys
4import unittest2
5
6
7class Test(object):
8
9    class Foo(unittest2.TestCase):
10
11        def test_1(self): pass
12
13        def test_2(self): pass
14
15        def test_3(self): pass
16
17        def runTest(self): pass
18
19
20def _mk_TestSuite(*names):
21    return unittest2.TestSuite(Test.Foo(n) for n in names)
22
23
24class Test_TestSuite(unittest2.TestCase, EqualityMixin):
25
26    # Set up attributes needed by inherited tests
27    ################################################################
28
29    # Used by EqualityMixin.test_eq
30    eq_pairs = [(unittest2.TestSuite(), unittest2.TestSuite()),
31                (unittest2.TestSuite(), unittest2.TestSuite([])),
32                (_mk_TestSuite('test_1'), _mk_TestSuite('test_1'))]
33
34    # Used by EqualityMixin.test_ne
35    ne_pairs = [(unittest2.TestSuite(), _mk_TestSuite('test_1')),
36                (unittest2.TestSuite([]), _mk_TestSuite('test_1')),
37                (_mk_TestSuite('test_1', 'test_2'), _mk_TestSuite('test_1', 'test_3')),
38                (_mk_TestSuite('test_1'), _mk_TestSuite('test_2'))]
39
40    ################################################################
41    # /Set up attributes needed by inherited tests
42
43    # Tests for TestSuite.__init__
44    ################################################################
45
46    # "class TestSuite([tests])"
47    #
48    # The tests iterable should be optional
49    def test_init__tests_optional(self):
50        suite = unittest2.TestSuite()
51
52        self.assertEqual(suite.countTestCases(), 0)
53
54    # "class TestSuite([tests])"
55    # ...
56    # "If tests is given, it must be an iterable of individual test cases
57    # or other test suites that will be used to build the suite initially"
58    #
59    # TestSuite should deal with empty tests iterables by allowing the
60    # creation of an empty suite
61    def test_init__empty_tests(self):
62        suite = unittest2.TestSuite([])
63
64        self.assertEqual(suite.countTestCases(), 0)
65
66    # "class TestSuite([tests])"
67    # ...
68    # "If tests is given, it must be an iterable of individual test cases
69    # or other test suites that will be used to build the suite initially"
70    #
71    # TestSuite should allow any iterable to provide tests
72    def test_init__tests_from_any_iterable(self):
73        def tests():
74            yield unittest2.FunctionTestCase(lambda: None)
75            yield unittest2.FunctionTestCase(lambda: None)
76
77        suite_1 = unittest2.TestSuite(tests())
78        self.assertEqual(suite_1.countTestCases(), 2)
79
80        suite_2 = unittest2.TestSuite(suite_1)
81        self.assertEqual(suite_2.countTestCases(), 2)
82
83        suite_3 = unittest2.TestSuite(set(suite_1))
84        self.assertEqual(suite_3.countTestCases(), 2)
85
86    # "class TestSuite([tests])"
87    # ...
88    # "If tests is given, it must be an iterable of individual test cases
89    # or other test suites that will be used to build the suite initially"
90    #
91    # Does TestSuite() also allow other TestSuite() instances to be present
92    # in the tests iterable?
93    def test_init__TestSuite_instances_in_tests(self):
94        def tests():
95            ftc = unittest2.FunctionTestCase(lambda: None)
96            yield unittest2.TestSuite([ftc])
97            yield unittest2.FunctionTestCase(lambda: None)
98
99        suite = unittest2.TestSuite(tests())
100        self.assertEqual(suite.countTestCases(), 2)
101
102    ################################################################
103    # /Tests for TestSuite.__init__
104
105    # Container types should support the iter protocol
106    def test_iter(self):
107        test1 = unittest2.FunctionTestCase(lambda: None)
108        test2 = unittest2.FunctionTestCase(lambda: None)
109        suite = unittest2.TestSuite((test1, test2))
110
111        self.assertEqual(list(suite), [test1, test2])
112
113    # "Return the number of tests represented by the this test object.
114    # ...this method is also implemented by the TestSuite class, which can
115    # return larger [greater than 1] values"
116    #
117    # Presumably an empty TestSuite returns 0?
118    def test_countTestCases_zero_simple(self):
119        suite = unittest2.TestSuite()
120
121        self.assertEqual(suite.countTestCases(), 0)
122
123    # "Return the number of tests represented by the this test object.
124    # ...this method is also implemented by the TestSuite class, which can
125    # return larger [greater than 1] values"
126    #
127    # Presumably an empty TestSuite (even if it contains other empty
128    # TestSuite instances) returns 0?
129    def test_countTestCases_zero_nested(self):
130        class Test1(unittest2.TestCase):
131
132            def test(self):
133                pass
134
135        suite = unittest2.TestSuite([unittest2.TestSuite()])
136
137        self.assertEqual(suite.countTestCases(), 0)
138
139    # "Return the number of tests represented by the this test object.
140    # ...this method is also implemented by the TestSuite class, which can
141    # return larger [greater than 1] values"
142    def test_countTestCases_simple(self):
143        test1 = unittest2.FunctionTestCase(lambda: None)
144        test2 = unittest2.FunctionTestCase(lambda: None)
145        suite = unittest2.TestSuite((test1, test2))
146
147        self.assertEqual(suite.countTestCases(), 2)
148
149    # "Return the number of tests represented by the this test object.
150    # ...this method is also implemented by the TestSuite class, which can
151    # return larger [greater than 1] values"
152    #
153    # Make sure this holds for nested TestSuite instances, too
154    def test_countTestCases_nested(self):
155        class Test1(unittest2.TestCase):
156
157            def test1(self): pass
158
159            def test2(self): pass
160
161        test2 = unittest2.FunctionTestCase(lambda: None)
162        test3 = unittest2.FunctionTestCase(lambda: None)
163        child = unittest2.TestSuite((Test1('test2'), test2))
164        parent = unittest2.TestSuite((test3, child, Test1('test1')))
165
166        self.assertEqual(parent.countTestCases(), 4)
167
168    # "Run the tests associated with this suite, collecting the result into
169    # the test result object passed as result."
170    #
171    # And if there are no tests? What then?
172    def test_run__empty_suite(self):
173        events = []
174        result = LoggingResult(events)
175
176        suite = unittest2.TestSuite()
177
178        suite.run(result)
179
180        self.assertEqual(events, [])
181
182    # "Note that unlike TestCase.run(), TestSuite.run() requires the
183    # "result object to be passed in."
184    def test_run__requires_result(self):
185        suite = unittest2.TestSuite()
186
187        try:
188            suite.run()
189        except TypeError:
190            pass
191        else:
192            self.fail("Failed to raise TypeError")
193
194    # "Run the tests associated with this suite, collecting the result into
195    # the test result object passed as result."
196    def test_run(self):
197        events = []
198        result = LoggingResult(events)
199
200        class LoggingCase(unittest2.TestCase):
201
202            def run(self, result):
203                events.append('run %s' % self._testMethodName)
204
205            def test1(self): pass
206
207            def test2(self): pass
208
209        tests = [LoggingCase('test1'), LoggingCase('test2')]
210
211        unittest2.TestSuite(tests).run(result)
212
213        self.assertEqual(events, ['run test1', 'run test2'])
214
215    # "Add a TestCase ... to the suite"
216    def test_addTest__TestCase(self):
217        class Foo(unittest2.TestCase):
218
219            def test(self): pass
220
221        test = Foo('test')
222        suite = unittest2.TestSuite()
223
224        suite.addTest(test)
225
226        self.assertEqual(suite.countTestCases(), 1)
227        self.assertEqual(list(suite), [test])
228
229    # "Add a ... TestSuite to the suite"
230    def test_addTest__TestSuite(self):
231        class Foo(unittest2.TestCase):
232
233            def test(self): pass
234
235        suite_2 = unittest2.TestSuite([Foo('test')])
236
237        suite = unittest2.TestSuite()
238        suite.addTest(suite_2)
239
240        self.assertEqual(suite.countTestCases(), 1)
241        self.assertEqual(list(suite), [suite_2])
242
243    # "Add all the tests from an iterable of TestCase and TestSuite
244    # instances to this test suite."
245    #
246    # "This is equivalent to iterating over tests, calling addTest() for
247    # each element"
248    def test_addTests(self):
249        class Foo(unittest2.TestCase):
250
251            def test_1(self): pass
252
253            def test_2(self): pass
254
255        test_1 = Foo('test_1')
256        test_2 = Foo('test_2')
257        inner_suite = unittest2.TestSuite([test_2])
258
259        def gen():
260            yield test_1
261            yield test_2
262            yield inner_suite
263
264        suite_1 = unittest2.TestSuite()
265        suite_1.addTests(gen())
266
267        self.assertEqual(list(suite_1), list(gen()))
268
269        # "This is equivalent to iterating over tests, calling addTest() for
270        # each element"
271        suite_2 = unittest2.TestSuite()
272        for t in gen():
273            suite_2.addTest(t)
274
275        self.assertEqual(suite_1, suite_2)
276
277    # "Add all the tests from an iterable of TestCase and TestSuite
278    # instances to this test suite."
279    #
280    # What happens if it doesn't get an iterable?
281    def test_addTest__noniterable(self):
282        suite = unittest2.TestSuite()
283
284        try:
285            suite.addTests(5)
286        except TypeError:
287            pass
288        else:
289            self.fail("Failed to raise TypeError")
290
291    def test_addTest__noncallable(self):
292        suite = unittest2.TestSuite()
293        self.assertRaises(TypeError, suite.addTest, 5)
294
295    def test_addTest__casesuiteclass(self):
296        suite = unittest2.TestSuite()
297        self.assertRaises(TypeError, suite.addTest, Test_TestSuite)
298        self.assertRaises(TypeError, suite.addTest, unittest2.TestSuite)
299
300    def test_addTests__string(self):
301        suite = unittest2.TestSuite()
302        self.assertRaises(TypeError, suite.addTests, "foo")
303
304    def test_function_in_suite(self):
305        def f(_):
306            pass
307        suite = unittest2.TestSuite()
308        suite.addTest(f)
309
310        # when the bug is fixed this line will not crash
311        suite.run(unittest2.TestResult())
312
313    def test_basetestsuite(self):
314        class Test(unittest2.TestCase):
315            wasSetUp = False
316            wasTornDown = False
317
318            @classmethod
319            def setUpClass(cls):
320                cls.wasSetUp = True
321
322            @classmethod
323            def tearDownClass(cls):
324                cls.wasTornDown = True
325
326            def testPass(self):
327                pass
328
329            def testFail(self):
330                fail
331
332        class Module(object):
333            wasSetUp = False
334            wasTornDown = False
335
336            @staticmethod
337            def setUpModule():
338                Module.wasSetUp = True
339
340            @staticmethod
341            def tearDownModule():
342                Module.wasTornDown = True
343
344        Test.__module__ = 'Module'
345        sys.modules['Module'] = Module
346        self.addCleanup(sys.modules.pop, 'Module')
347
348        suite = unittest2.BaseTestSuite()
349        suite.addTests([Test('testPass'), Test('testFail')])
350        self.assertEqual(suite.countTestCases(), 2)
351
352        result = unittest2.TestResult()
353        suite.run(result)
354        self.assertFalse(Module.wasSetUp)
355        self.assertFalse(Module.wasTornDown)
356        self.assertFalse(Test.wasSetUp)
357        self.assertFalse(Test.wasTornDown)
358        self.assertEqual(len(result.errors), 1)
359        self.assertEqual(len(result.failures), 0)
360        self.assertEqual(result.testsRun, 2)
361
362if __name__ == '__main__':
363    unittest2.main()
364