From 95a922b3bb3af247ec141d73fcdfbf68bb1d32a5 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Fri, 17 Dec 2021 11:10:55 +0200 Subject: [PATCH] bpo-46111: Fix unittest tests in optimized mode (GH-30163) --- Lib/unittest/test/test_case.py | 2 ++ Lib/unittest/test/test_program.py | 4 ++-- Lib/unittest/test/testmock/testpatch.py | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Lib/unittest/test/test_case.py b/Lib/unittest/test/test_case.py index ee4c0b354f7..067ec8d4586 100644 --- a/Lib/unittest/test/test_case.py +++ b/Lib/unittest/test/test_case.py @@ -631,6 +631,8 @@ class Test_TestCase(unittest.TestCase, TestEquality, TestHashing): 'Tests shortDescription() for a method with a longer ' 'docstring.') + @unittest.skipIf(sys.flags.optimize >= 2, + "Docstrings are omitted with -O2 and above") def testShortDescriptionWhitespaceTrimming(self): """ Tests shortDescription() whitespace is trimmed, so that the first diff --git a/Lib/unittest/test/test_program.py b/Lib/unittest/test/test_program.py index cc02b227e1d..2bf7dd72ed2 100644 --- a/Lib/unittest/test/test_program.py +++ b/Lib/unittest/test/test_program.py @@ -58,9 +58,9 @@ class Test_TestProgram(unittest.TestCase): class FooBar(unittest.TestCase): def testPass(self): - assert True + pass def testFail(self): - assert False + raise AssertionError class FooBarLoader(unittest.TestLoader): """Test loader that returns a suite containing FooBar.""" diff --git a/Lib/unittest/test/testmock/testpatch.py b/Lib/unittest/test/testmock/testpatch.py index d8c1515f834..233a5afffae 100644 --- a/Lib/unittest/test/testmock/testpatch.py +++ b/Lib/unittest/test/testmock/testpatch.py @@ -1875,9 +1875,10 @@ class PatchTest(unittest.TestCase): self.assertEqual(foo(), 1) self.assertEqual(foo(), 0) + orig_doc = foo.__doc__ with patch.object(foo, '__doc__', "FUN"): self.assertEqual(foo.__doc__, "FUN") - self.assertEqual(foo.__doc__, "TEST") + self.assertEqual(foo.__doc__, orig_doc) with patch.object(foo, '__module__', "testpatch2"): self.assertEqual(foo.__module__, "testpatch2")