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