[Python-checkins] bpo-45102: unittest: add tests for skipping and errors in cleanup (GH-28166)

miss-islington webhook-mailer at python.org
Sun Sep 5 03:56:28 EDT 2021


https://github.com/python/cpython/commit/f91d974ce61b2a9922391f723b388a623284fb0a
commit: f91d974ce61b2a9922391f723b388a623284fb0a
branch: 3.9
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2021-09-05T00:56:24-07:00
summary:

bpo-45102: unittest: add tests for skipping and errors in cleanup (GH-28166)

(cherry picked from commit 28264269de9ff88d9ee7110fc56ac2d2db275bec)

Co-authored-by: Serhiy Storchaka <storchaka at gmail.com>

files:
M Lib/unittest/test/test_skipping.py

diff --git a/Lib/unittest/test/test_skipping.py b/Lib/unittest/test/test_skipping.py
index 79b27e6daae255..c14410afbefdc4 100644
--- a/Lib/unittest/test/test_skipping.py
+++ b/Lib/unittest/test/test_skipping.py
@@ -161,6 +161,58 @@ class Foo(Mixin, unittest.TestCase):
         self.assertEqual(result.skipped, [(test, "testing")])
         self.assertEqual(record, [])
 
+    def test_skip_in_setup(self):
+        class Foo(unittest.TestCase):
+            def setUp(self):
+                self.skipTest("skip")
+            def test_skip_me(self):
+                self.fail("shouldn't come here")
+        events = []
+        result = LoggingResult(events)
+        test = Foo("test_skip_me")
+        self.assertIs(test.run(result), result)
+        self.assertEqual(events, ['startTest', 'addSkip', 'stopTest'])
+        self.assertEqual(result.skipped, [(test, "skip")])
+
+    def test_skip_in_cleanup(self):
+        class Foo(unittest.TestCase):
+            def test_skip_me(self):
+                pass
+            def tearDown(self):
+                self.skipTest("skip")
+        events = []
+        result = LoggingResult(events)
+        test = Foo("test_skip_me")
+        self.assertIs(test.run(result), result)
+        self.assertEqual(events, ['startTest', 'addSkip', 'stopTest'])
+        self.assertEqual(result.skipped, [(test, "skip")])
+
+    def test_failure_and_skip_in_cleanup(self):
+        class Foo(unittest.TestCase):
+            def test_skip_me(self):
+                self.fail("fail")
+            def tearDown(self):
+                self.skipTest("skip")
+        events = []
+        result = LoggingResult(events)
+        test = Foo("test_skip_me")
+        self.assertIs(test.run(result), result)
+        self.assertEqual(events, ['startTest', 'addSkip', 'addFailure', 'stopTest'])
+        self.assertEqual(result.skipped, [(test, "skip")])
+
+    def test_skipping_and_fail_in_cleanup(self):
+        class Foo(unittest.TestCase):
+            def test_skip_me(self):
+                self.skipTest("skip")
+            def tearDown(self):
+                self.fail("fail")
+        events = []
+        result = LoggingResult(events)
+        test = Foo("test_skip_me")
+        self.assertIs(test.run(result), result)
+        self.assertEqual(events, ['startTest', 'addSkip', 'addFailure', 'stopTest'])
+        self.assertEqual(result.skipped, [(test, "skip")])
+
     def test_expected_failure(self):
         class Foo(unittest.TestCase):
             @unittest.expectedFailure
@@ -172,7 +224,9 @@ def test_die(self):
         self.assertIs(test.run(result), result)
         self.assertEqual(events,
                          ['startTest', 'addExpectedFailure', 'stopTest'])
+        self.assertFalse(result.failures)
         self.assertEqual(result.expectedFailures[0][0], test)
+        self.assertFalse(result.unexpectedSuccesses)
         self.assertTrue(result.wasSuccessful())
 
     def test_expected_failure_with_wrapped_class(self):
@@ -187,7 +241,9 @@ def test_1(self):
         self.assertIs(test.run(result), result)
         self.assertEqual(events,
                          ['startTest', 'addExpectedFailure', 'stopTest'])
+        self.assertFalse(result.failures)
         self.assertEqual(result.expectedFailures[0][0], test)
+        self.assertFalse(result.unexpectedSuccesses)
         self.assertTrue(result.wasSuccessful())
 
     def test_expected_failure_with_wrapped_subclass(self):
@@ -205,7 +261,9 @@ class Bar(Foo):
         self.assertIs(test.run(result), result)
         self.assertEqual(events,
                          ['startTest', 'addExpectedFailure', 'stopTest'])
+        self.assertFalse(result.failures)
         self.assertEqual(result.expectedFailures[0][0], test)
+        self.assertFalse(result.unexpectedSuccesses)
         self.assertTrue(result.wasSuccessful())
 
     def test_expected_failure_subtests(self):
@@ -229,8 +287,48 @@ def test_die(self):
         self.assertEqual(events,
                          ['startTest', 'addSubTestSuccess',
                           'addExpectedFailure', 'stopTest'])
+        self.assertFalse(result.failures)
         self.assertEqual(len(result.expectedFailures), 1)
         self.assertIs(result.expectedFailures[0][0], test)
+        self.assertFalse(result.unexpectedSuccesses)
+        self.assertTrue(result.wasSuccessful())
+
+    def test_expected_failure_and_fail_in_cleanup(self):
+        class Foo(unittest.TestCase):
+            @unittest.expectedFailure
+            def test_die(self):
+                self.fail("help me!")
+            def tearDown(self):
+                self.fail("bad tearDown")
+        events = []
+        result = LoggingResult(events)
+        test = Foo("test_die")
+        self.assertIs(test.run(result), result)
+        self.assertEqual(events,
+                         ['startTest', 'addFailure', 'stopTest'])
+        self.assertEqual(len(result.failures), 1)
+        self.assertIn('AssertionError: bad tearDown', result.failures[0][1])
+        self.assertFalse(result.expectedFailures)
+        self.assertFalse(result.unexpectedSuccesses)
+        self.assertFalse(result.wasSuccessful())
+
+    def test_expected_failure_and_skip_in_cleanup(self):
+        class Foo(unittest.TestCase):
+            @unittest.expectedFailure
+            def test_die(self):
+                self.fail("help me!")
+            def tearDown(self):
+                self.skipTest("skip")
+        events = []
+        result = LoggingResult(events)
+        test = Foo("test_die")
+        self.assertIs(test.run(result), result)
+        self.assertEqual(events,
+                         ['startTest', 'addSkip', 'stopTest'])
+        self.assertFalse(result.failures)
+        self.assertFalse(result.expectedFailures)
+        self.assertFalse(result.unexpectedSuccesses)
+        self.assertEqual(result.skipped, [(test, "skip")])
         self.assertTrue(result.wasSuccessful())
 
     def test_unexpected_success(self):
@@ -245,6 +343,7 @@ def test_die(self):
         self.assertEqual(events,
                          ['startTest', 'addUnexpectedSuccess', 'stopTest'])
         self.assertFalse(result.failures)
+        self.assertFalse(result.expectedFailures)
         self.assertEqual(result.unexpectedSuccesses, [test])
         self.assertFalse(result.wasSuccessful())
 
@@ -269,9 +368,48 @@ def test_die(self):
                           'addSubTestSuccess', 'addSubTestSuccess',
                           'addUnexpectedSuccess', 'stopTest'])
         self.assertFalse(result.failures)
+        self.assertFalse(result.expectedFailures)
         self.assertEqual(result.unexpectedSuccesses, [test])
         self.assertFalse(result.wasSuccessful())
 
+    def test_unexpected_success_and_fail_in_cleanup(self):
+        class Foo(unittest.TestCase):
+            @unittest.expectedFailure
+            def test_die(self):
+                pass
+            def tearDown(self):
+                self.fail("bad tearDown")
+        events = []
+        result = LoggingResult(events)
+        test = Foo("test_die")
+        self.assertIs(test.run(result), result)
+        self.assertEqual(events,
+                         ['startTest', 'addFailure', 'stopTest'])
+        self.assertEqual(len(result.failures), 1)
+        self.assertIn('AssertionError: bad tearDown', result.failures[0][1])
+        self.assertFalse(result.expectedFailures)
+        self.assertFalse(result.unexpectedSuccesses)
+        self.assertFalse(result.wasSuccessful())
+
+    def test_unexpected_success_and_skip_in_cleanup(self):
+        class Foo(unittest.TestCase):
+            @unittest.expectedFailure
+            def test_die(self):
+                pass
+            def tearDown(self):
+                self.skipTest("skip")
+        events = []
+        result = LoggingResult(events)
+        test = Foo("test_die")
+        self.assertIs(test.run(result), result)
+        self.assertEqual(events,
+                         ['startTest', 'addSkip', 'stopTest'])
+        self.assertFalse(result.failures)
+        self.assertFalse(result.expectedFailures)
+        self.assertFalse(result.unexpectedSuccesses)
+        self.assertEqual(result.skipped, [(test, "skip")])
+        self.assertTrue(result.wasSuccessful())
+
     def test_skip_doesnt_run_setup(self):
         class Foo(unittest.TestCase):
             wasSetUp = False



More information about the Python-checkins mailing list