]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-41322: Add unit tests for deprecation of test return values (GH-27846)
authorandrei kulakov <andrei.avk@gmail.com>
Sun, 22 Aug 2021 18:32:45 +0000 (14:32 -0400)
committerGitHub <noreply@github.com>
Sun, 22 Aug 2021 18:32:45 +0000 (21:32 +0300)
Also fix the traceback of warnings.

Doc/library/unittest.rst
Doc/whatsnew/3.11.rst
Lib/unittest/async_case.py
Lib/unittest/case.py
Lib/unittest/test/test_async_case.py
Lib/unittest/test/test_case.py

index 99c2f6e029448ed42eb3cb5ed8271725e50bf4b0..f0fba94677a917577f5f0ec6ef5dcc230a9623c3 100644 (file)
@@ -151,6 +151,10 @@ The above examples show the most commonly used :mod:`unittest` features which
 are sufficient to meet many everyday testing needs.  The remainder of the
 documentation explores the full feature set from first principles.
 
+.. versionchanged:: 3.11
+   The behavior of returning a value from a test method (other than the default
+   ``None`` value), is now deprecated.
+
 
 .. _unittest-command-line-interface:
 
index 49b4364be9bd7f72d091d567d8ae7261481e326d..cb8f84c4b625e51093d6d13d96d5ff6acedd1ad3 100644 (file)
@@ -395,3 +395,7 @@ Removed
   :func:`~gettext.install` are also removed, since they are only used for
   the ``l*gettext()`` functions.
   (Contributed by Dong-hee Na and Serhiy Storchaka in :issue:`44235`.)
+
+* The behavior of returning a value from a :class:`~unittest.TestCase` and
+  :class:`~unittest.IsolatedAsyncioTestCase` test methods (other than the default ``None``
+  value), is now deprecated.
index cc404cc06f8b65a56fcce19681fe216260bd8caf..bfc68a76e84d9347e0535535058ca4bd942313e8 100644 (file)
@@ -65,7 +65,7 @@ class IsolatedAsyncioTestCase(TestCase):
     def _callTestMethod(self, method):
         if self._callMaybeAsync(method) is not None:
             warnings.warn(f'It is deprecated to return a value!=None from a '
-                          f'test case ({method})', DeprecationWarning)
+                          f'test case ({method})', DeprecationWarning, stacklevel=4)
 
     def _callTearDown(self):
         self._callAsync(self.asyncTearDown)
index 625d27ef6dc61e60ae5f58c3253c5dd898363971..8775ba9e241c4447cd64425632385dfc1ea2f568 100644 (file)
@@ -548,7 +548,7 @@ class TestCase(object):
     def _callTestMethod(self, method):
         if method() is not None:
             warnings.warn(f'It is deprecated to return a value!=None from a '
-                          f'test case ({method})', DeprecationWarning)
+                          f'test case ({method})', DeprecationWarning, stacklevel=3)
 
     def _callTearDown(self):
         self.tearDown()
index 6e48b9e4bfed367a1ceb0b2008fdfd5fe06b380e..93ef1997e0c99f4948bb9cec684aa0c73736de49 100644 (file)
@@ -167,6 +167,26 @@ class TestAsyncCase(unittest.TestCase):
         test.run()
         self.assertEqual(events, ['asyncSetUp', 'test', 'asyncTearDown', 'cleanup'])
 
+    def test_deprecation_of_return_val_from_test(self):
+        # Issue 41322 - deprecate return of value!=None from a test
+        class Test(unittest.IsolatedAsyncioTestCase):
+            async def test1(self):
+                return 1
+            async def test2(self):
+                yield 1
+
+        with self.assertWarns(DeprecationWarning) as w:
+            Test('test1').run()
+        self.assertIn('It is deprecated to return a value!=None', str(w.warnings[0].message))
+        self.assertIn('test1', str(w.warnings[0].message))
+        self.assertEqual(w.warnings[0].filename, __file__)
+
+        with self.assertWarns(DeprecationWarning) as w:
+            Test('test2').run()
+        self.assertIn('It is deprecated to return a value!=None', str(w.warnings[0].message))
+        self.assertIn('test2', str(w.warnings[0].message))
+        self.assertEqual(w.warnings[0].filename, __file__)
+
     def test_cleanups_interleave_order(self):
         events = []
 
index b8aca92a8ebe9f74304cea2bb277a76fd11b4894..f3cabe44d1c88ea54f311e2e84f47192c1955b07 100644 (file)
@@ -306,6 +306,26 @@ class Test_TestCase(unittest.TestCase, TestEquality, TestHashing):
 
         Foo('test').run()
 
+    def test_deprecation_of_return_val_from_test(self):
+        # Issue 41322 - deprecate return of value!=None from a test
+        class Foo(unittest.TestCase):
+            def test1(self):
+                return 1
+            def test2(self):
+                yield 1
+
+        with self.assertWarns(DeprecationWarning) as w:
+            Foo('test1').run()
+        self.assertIn('It is deprecated to return a value!=None', str(w.warnings[0].message))
+        self.assertIn('test1', str(w.warnings[0].message))
+        self.assertEqual(w.warnings[0].filename, __file__)
+
+        with self.assertWarns(DeprecationWarning) as w:
+            Foo('test2').run()
+        self.assertIn('It is deprecated to return a value!=None', str(w.warnings[0].message))
+        self.assertIn('test2', str(w.warnings[0].message))
+        self.assertEqual(w.warnings[0].filename, __file__)
+
     def _check_call_order__subtests(self, result, events, expected_events):
         class Foo(Test.LoggingTestCase):
             def test(self):