10a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport unittest
20a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
30a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport sys
40a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofrom .support import LoggingResult, TestEquality
50a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
60a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
70a8c90248264a8b26970b4473770bcc3df8515fJosh Gao### Support code for Test_TestSuite
80a8c90248264a8b26970b4473770bcc3df8515fJosh Gao################################################################
90a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
100a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass Test(object):
110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    class Foo(unittest.TestCase):
120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def test_1(self): pass
130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def test_2(self): pass
140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def test_3(self): pass
150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def runTest(self): pass
160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
170a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef _mk_TestSuite(*names):
180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    return unittest.TestSuite(Test.Foo(n) for n in names)
190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao################################################################
210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
230a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass Test_TestSuite(unittest.TestCase, TestEquality):
240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    ### Set up attributes needed by inherited tests
260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    ################################################################
270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # Used by TestEquality.test_eq
290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    eq_pairs = [(unittest.TestSuite(), unittest.TestSuite()),
300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                (unittest.TestSuite(), unittest.TestSuite([])),
310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao               (_mk_TestSuite('test_1'), _mk_TestSuite('test_1'))]
320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # Used by TestEquality.test_ne
340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    ne_pairs = [(unittest.TestSuite(), _mk_TestSuite('test_1')),
350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                (unittest.TestSuite([]), _mk_TestSuite('test_1')),
360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                (_mk_TestSuite('test_1', 'test_2'), _mk_TestSuite('test_1', 'test_3')),
370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                (_mk_TestSuite('test_1'), _mk_TestSuite('test_2'))]
380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    ################################################################
400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    ### /Set up attributes needed by inherited tests
410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    ### Tests for TestSuite.__init__
430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    ################################################################
440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # "class TestSuite([tests])"
460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    #
470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # The tests iterable should be optional
480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_init__tests_optional(self):
490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        suite = unittest.TestSuite()
500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(suite.countTestCases(), 0)
520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # "class TestSuite([tests])"
540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # ...
550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # "If tests is given, it must be an iterable of individual test cases
560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # or other test suites that will be used to build the suite initially"
570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    #
580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # TestSuite should deal with empty tests iterables by allowing the
590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # creation of an empty suite
600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_init__empty_tests(self):
610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        suite = unittest.TestSuite([])
620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(suite.countTestCases(), 0)
640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # "class TestSuite([tests])"
660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # ...
670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # "If tests is given, it must be an iterable of individual test cases
680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # or other test suites that will be used to build the suite initially"
690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    #
700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # TestSuite should allow any iterable to provide tests
710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_init__tests_from_any_iterable(self):
720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def tests():
730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            yield unittest.FunctionTestCase(lambda: None)
740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            yield unittest.FunctionTestCase(lambda: None)
750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        suite_1 = unittest.TestSuite(tests())
770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(suite_1.countTestCases(), 2)
780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        suite_2 = unittest.TestSuite(suite_1)
800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(suite_2.countTestCases(), 2)
810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        suite_3 = unittest.TestSuite(set(suite_1))
830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(suite_3.countTestCases(), 2)
840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # "class TestSuite([tests])"
860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # ...
870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # "If tests is given, it must be an iterable of individual test cases
880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # or other test suites that will be used to build the suite initially"
890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    #
900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # Does TestSuite() also allow other TestSuite() instances to be present
910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # in the tests iterable?
920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_init__TestSuite_instances_in_tests(self):
930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def tests():
940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            ftc = unittest.FunctionTestCase(lambda: None)
950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            yield unittest.TestSuite([ftc])
960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            yield unittest.FunctionTestCase(lambda: None)
970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        suite = unittest.TestSuite(tests())
990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(suite.countTestCases(), 2)
1000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    ################################################################
1020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    ### /Tests for TestSuite.__init__
1030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # Container types should support the iter protocol
1050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_iter(self):
1060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        test1 = unittest.FunctionTestCase(lambda: None)
1070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        test2 = unittest.FunctionTestCase(lambda: None)
1080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        suite = unittest.TestSuite((test1, test2))
1090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(list(suite), [test1, test2])
1110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # "Return the number of tests represented by the this test object.
1130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # ...this method is also implemented by the TestSuite class, which can
1140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # return larger [greater than 1] values"
1150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    #
1160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # Presumably an empty TestSuite returns 0?
1170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_countTestCases_zero_simple(self):
1180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        suite = unittest.TestSuite()
1190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(suite.countTestCases(), 0)
1210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # "Return the number of tests represented by the this test object.
1230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # ...this method is also implemented by the TestSuite class, which can
1240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # return larger [greater than 1] values"
1250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    #
1260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # Presumably an empty TestSuite (even if it contains other empty
1270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # TestSuite instances) returns 0?
1280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_countTestCases_zero_nested(self):
1290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        class Test1(unittest.TestCase):
1300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def test(self):
1310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                pass
1320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        suite = unittest.TestSuite([unittest.TestSuite()])
1340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(suite.countTestCases(), 0)
1360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # "Return the number of tests represented by the this test object.
1380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # ...this method is also implemented by the TestSuite class, which can
1390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # return larger [greater than 1] values"
1400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_countTestCases_simple(self):
1410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        test1 = unittest.FunctionTestCase(lambda: None)
1420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        test2 = unittest.FunctionTestCase(lambda: None)
1430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        suite = unittest.TestSuite((test1, test2))
1440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(suite.countTestCases(), 2)
1460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # "Return the number of tests represented by the this test object.
1480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # ...this method is also implemented by the TestSuite class, which can
1490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # return larger [greater than 1] values"
1500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    #
1510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # Make sure this holds for nested TestSuite instances, too
1520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_countTestCases_nested(self):
1530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        class Test1(unittest.TestCase):
1540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def test1(self): pass
1550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def test2(self): pass
1560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        test2 = unittest.FunctionTestCase(lambda: None)
1580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        test3 = unittest.FunctionTestCase(lambda: None)
1590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        child = unittest.TestSuite((Test1('test2'), test2))
1600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        parent = unittest.TestSuite((test3, child, Test1('test1')))
1610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(parent.countTestCases(), 4)
1630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # "Run the tests associated with this suite, collecting the result into
1650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # the test result object passed as result."
1660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    #
1670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # And if there are no tests? What then?
1680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_run__empty_suite(self):
1690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        events = []
1700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        result = LoggingResult(events)
1710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        suite = unittest.TestSuite()
1730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        suite.run(result)
1750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(events, [])
1770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # "Note that unlike TestCase.run(), TestSuite.run() requires the
1790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # "result object to be passed in."
1800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_run__requires_result(self):
1810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        suite = unittest.TestSuite()
1820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
1840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            suite.run()
1850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        except TypeError:
1860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            pass
1870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
1880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.fail("Failed to raise TypeError")
1890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # "Run the tests associated with this suite, collecting the result into
1910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # the test result object passed as result."
1920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_run(self):
1930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        events = []
1940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        result = LoggingResult(events)
1950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        class LoggingCase(unittest.TestCase):
1970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def run(self, result):
1980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                events.append('run %s' % self._testMethodName)
1990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def test1(self): pass
2010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def test2(self): pass
2020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        tests = [LoggingCase('test1'), LoggingCase('test2')]
2040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        unittest.TestSuite(tests).run(result)
2060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(events, ['run test1', 'run test2'])
2080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # "Add a TestCase ... to the suite"
2100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_addTest__TestCase(self):
2110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        class Foo(unittest.TestCase):
2120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def test(self): pass
2130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        test = Foo('test')
2150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        suite = unittest.TestSuite()
2160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        suite.addTest(test)
2180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(suite.countTestCases(), 1)
2200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(list(suite), [test])
2210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # "Add a ... TestSuite to the suite"
2230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_addTest__TestSuite(self):
2240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        class Foo(unittest.TestCase):
2250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def test(self): pass
2260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        suite_2 = unittest.TestSuite([Foo('test')])
2280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        suite = unittest.TestSuite()
2300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        suite.addTest(suite_2)
2310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(suite.countTestCases(), 1)
2330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(list(suite), [suite_2])
2340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # "Add all the tests from an iterable of TestCase and TestSuite
2360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # instances to this test suite."
2370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    #
2380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # "This is equivalent to iterating over tests, calling addTest() for
2390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # each element"
2400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_addTests(self):
2410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        class Foo(unittest.TestCase):
2420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def test_1(self): pass
2430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def test_2(self): pass
2440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        test_1 = Foo('test_1')
2460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        test_2 = Foo('test_2')
2470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        inner_suite = unittest.TestSuite([test_2])
2480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def gen():
2500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            yield test_1
2510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            yield test_2
2520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            yield inner_suite
2530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        suite_1 = unittest.TestSuite()
2550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        suite_1.addTests(gen())
2560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(list(suite_1), list(gen()))
2580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # "This is equivalent to iterating over tests, calling addTest() for
2600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # each element"
2610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        suite_2 = unittest.TestSuite()
2620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for t in gen():
2630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            suite_2.addTest(t)
2640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(suite_1, suite_2)
2660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # "Add all the tests from an iterable of TestCase and TestSuite
2680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # instances to this test suite."
2690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    #
2700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # What happens if it doesn't get an iterable?
2710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_addTest__noniterable(self):
2720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        suite = unittest.TestSuite()
2730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
2750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            suite.addTests(5)
2760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        except TypeError:
2770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            pass
2780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
2790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.fail("Failed to raise TypeError")
2800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_addTest__noncallable(self):
2820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        suite = unittest.TestSuite()
2830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(TypeError, suite.addTest, 5)
2840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_addTest__casesuiteclass(self):
2860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        suite = unittest.TestSuite()
2870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(TypeError, suite.addTest, Test_TestSuite)
2880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(TypeError, suite.addTest, unittest.TestSuite)
2890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_addTests__string(self):
2910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        suite = unittest.TestSuite()
2920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(TypeError, suite.addTests, "foo")
2930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_function_in_suite(self):
2950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def f(_):
2960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            pass
2970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        suite = unittest.TestSuite()
2980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        suite.addTest(f)
2990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # when the bug is fixed this line will not crash
3010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        suite.run(unittest.TestResult())
3020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_basetestsuite(self):
3060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        class Test(unittest.TestCase):
3070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            wasSetUp = False
3080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            wasTornDown = False
3090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            @classmethod
3100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def setUpClass(cls):
3110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                cls.wasSetUp = True
3120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            @classmethod
3130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def tearDownClass(cls):
3140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                cls.wasTornDown = True
3150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def testPass(self):
3160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                pass
3170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def testFail(self):
3180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                fail
3190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        class Module(object):
3200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            wasSetUp = False
3210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            wasTornDown = False
3220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            @staticmethod
3230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def setUpModule():
3240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                Module.wasSetUp = True
3250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            @staticmethod
3260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def tearDownModule():
3270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                Module.wasTornDown = True
3280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Test.__module__ = 'Module'
3300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        sys.modules['Module'] = Module
3310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.addCleanup(sys.modules.pop, 'Module')
3320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        suite = unittest.BaseTestSuite()
3340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        suite.addTests([Test('testPass'), Test('testFail')])
3350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(suite.countTestCases(), 2)
3360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        result = unittest.TestResult()
3380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        suite.run(result)
3390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertFalse(Module.wasSetUp)
3400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertFalse(Module.wasTornDown)
3410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertFalse(Test.wasSetUp)
3420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertFalse(Test.wasTornDown)
3430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(len(result.errors), 1)
3440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(len(result.failures), 0)
3450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(result.testsRun, 2)
3460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_overriding_call(self):
3490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        class MySuite(unittest.TestSuite):
3500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            called = False
3510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def __call__(self, *args, **kw):
3520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.called = True
3530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                unittest.TestSuite.__call__(self, *args, **kw)
3540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        suite = MySuite()
3560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        result = unittest.TestResult()
3570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        wrapper = unittest.TestSuite()
3580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        wrapper.addTest(suite)
3590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        wrapper(result)
3600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertTrue(suite.called)
3610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # reusing results should be permitted even if abominable
3630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertFalse(result._testRunEntered)
3640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3660a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoif __name__ == '__main__':
3670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    unittest.main()
368