175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chenfrom unittest2.test.support import LoggingResult
275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen
375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chenimport unittest2
475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen
575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen
675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chenclass Test_TestSkipping(unittest2.TestCase):
775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen
875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen    def test_skipping(self):
975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        class Foo(unittest2.TestCase):
1075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            def test_skip_me(self):
1175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen                self.skipTest("skip")
1275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        events = []
1375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        result = LoggingResult(events)
1475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        test = Foo("test_skip_me")
1575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        test.run(result)
1675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        self.assertEqual(events, ['startTest', 'addSkip', 'stopTest'])
1775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        self.assertEqual(result.skipped, [(test, "skip")])
1875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen
1975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        # Try letting setUp skip the test now.
2075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        class Foo(unittest2.TestCase):
2175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            def setUp(self):
2275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen                self.skipTest("testing")
2375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            def test_nothing(self): pass
2475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        events = []
2575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        result = LoggingResult(events)
2675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        test = Foo("test_nothing")
2775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        test.run(result)
2875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        self.assertEqual(events, ['startTest', 'addSkip', 'stopTest'])
2975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        self.assertEqual(result.skipped, [(test, "testing")])
3075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        self.assertEqual(result.testsRun, 1)
3175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen
3275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen    def test_skipping_decorators(self):
3375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        op_table = ((unittest2.skipUnless, False, True),
3475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen                    (unittest2.skipIf, True, False))
3575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        for deco, do_skip, dont_skip in op_table:
3675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            class Foo(unittest2.TestCase):
3775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen                @deco(do_skip, "testing")
3875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen                def test_skip(self):
3975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen                    pass
4075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen
4175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen                @deco(dont_skip, "testing")
4275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen                def test_dont_skip(self):
4375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen                    pass
4475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen
4575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            test_do_skip = Foo("test_skip")
4675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            test_dont_skip = Foo("test_dont_skip")
4775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            suite = unittest2.TestSuite([test_do_skip, test_dont_skip])
4875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            events = []
4975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            result = LoggingResult(events)
5075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            suite.run(result)
5175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            self.assertEqual(len(result.skipped), 1)
5275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            expected = ['startTest', 'addSkip', 'stopTest',
5375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen                        'startTest', 'addSuccess', 'stopTest']
5475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            self.assertEqual(events, expected)
5575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            self.assertEqual(result.testsRun, 2)
5675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            self.assertEqual(result.skipped, [(test_do_skip, "testing")])
5775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            self.assertTrue(result.wasSuccessful())
5875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen
5975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen    def test_skip_class(self):
6075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        class Foo(unittest2.TestCase):
6175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            def test_1(self):
6275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen                record.append(1)
6375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen
6475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        # was originally a class decorator...
6575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        Foo = unittest2.skip("testing")(Foo)
6675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        record = []
6775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        result = unittest2.TestResult()
6875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        test = Foo("test_1")
6975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        suite = unittest2.TestSuite([test])
7075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        suite.run(result)
7175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        self.assertEqual(result.skipped, [(test, "testing")])
7275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        self.assertEqual(record, [])
7375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen
7475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen    def test_expected_failure(self):
7575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        class Foo(unittest2.TestCase):
7675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            @unittest2.expectedFailure
7775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            def test_die(self):
7875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen                self.fail("help me!")
7975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        events = []
8075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        result = LoggingResult(events)
8175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        test = Foo("test_die")
8275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        test.run(result)
8375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        self.assertEqual(events,
8475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen                         ['startTest', 'addExpectedFailure', 'stopTest'])
8575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        self.assertEqual(result.expectedFailures[0][0], test)
8675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        self.assertTrue(result.wasSuccessful())
8775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen
8875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen    def test_unexpected_success(self):
8975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        class Foo(unittest2.TestCase):
9075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            @unittest2.expectedFailure
9175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            def test_die(self):
9275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen                pass
9375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        events = []
9475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        result = LoggingResult(events)
9575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        test = Foo("test_die")
9675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        test.run(result)
9775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        self.assertEqual(events,
9875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen                         ['startTest', 'addUnexpectedSuccess', 'stopTest'])
9975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        self.assertFalse(result.failures)
10075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        self.assertEqual(result.unexpectedSuccesses, [test])
10175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        self.assertTrue(result.wasSuccessful())
10275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen
10375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen    def test_skip_doesnt_run_setup(self):
10475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        class Foo(unittest2.TestCase):
10575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            wasSetUp = False
10675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            wasTornDown = False
10775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            def setUp(self):
10875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen                Foo.wasSetUp = True
10975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            def tornDown(self):
11075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen                Foo.wasTornDown = True
11175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            @unittest2.skip('testing')
11275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            def test_1(self):
11375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen                pass
11475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen
11575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        result = unittest2.TestResult()
11675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        test = Foo("test_1")
11775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        suite = unittest2.TestSuite([test])
11875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        suite.run(result)
11975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        self.assertEqual(result.skipped, [(test, "testing")])
12075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        self.assertFalse(Foo.wasSetUp)
12175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        self.assertFalse(Foo.wasTornDown)
12275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen
12375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen    def test_decorated_skip(self):
12475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        def decorator(func):
12575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            def inner(*a):
12675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen                return func(*a)
12775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            return inner
12875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen
12975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        class Foo(unittest2.TestCase):
13075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            @decorator
13175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            @unittest2.skip('testing')
13275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            def test_1(self):
13375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen                pass
13475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen
13575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        result = unittest2.TestResult()
13675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        test = Foo("test_1")
13775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        suite = unittest2.TestSuite([test])
13875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        suite.run(result)
13975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        self.assertEqual(result.skipped, [(test, "testing")])
14075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen
14175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen
14275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chenif __name__ == '__main__':
14375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen    unittest2.main()
144