[Python-checkins] bpo-41322: Add unit tests for deprecation of test return values (GH-27846)

serhiy-storchaka webhook-mailer at python.org
Sun Aug 22 14:32:54 EDT 2021


https://github.com/python/cpython/commit/b1db308c6114bb18c06656c94c0ffa73ba954977
commit: b1db308c6114bb18c06656c94c0ffa73ba954977
branch: main
author: andrei kulakov <andrei.avk at gmail.com>
committer: serhiy-storchaka <storchaka at gmail.com>
date: 2021-08-22T21:32:45+03:00
summary:

bpo-41322: Add unit tests for deprecation of test return values (GH-27846)

Also fix the traceback of warnings.

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

diff --git a/Doc/library/unittest.rst b/Doc/library/unittest.rst
index 99c2f6e029448..f0fba94677a91 100644
--- a/Doc/library/unittest.rst
+++ b/Doc/library/unittest.rst
@@ -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:
 
diff --git a/Doc/whatsnew/3.11.rst b/Doc/whatsnew/3.11.rst
index 49b4364be9bd7..cb8f84c4b625e 100644
--- a/Doc/whatsnew/3.11.rst
+++ b/Doc/whatsnew/3.11.rst
@@ -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.
diff --git a/Lib/unittest/async_case.py b/Lib/unittest/async_case.py
index cc404cc06f8b6..bfc68a76e84d9 100644
--- a/Lib/unittest/async_case.py
+++ b/Lib/unittest/async_case.py
@@ -65,7 +65,7 @@ def _callSetUp(self):
     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)
diff --git a/Lib/unittest/case.py b/Lib/unittest/case.py
index 625d27ef6dc61..8775ba9e241c4 100644
--- a/Lib/unittest/case.py
+++ b/Lib/unittest/case.py
@@ -548,7 +548,7 @@ def _callSetUp(self):
     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()
diff --git a/Lib/unittest/test/test_async_case.py b/Lib/unittest/test/test_async_case.py
index 6e48b9e4bfed3..93ef1997e0c99 100644
--- a/Lib/unittest/test/test_async_case.py
+++ b/Lib/unittest/test/test_async_case.py
@@ -167,6 +167,26 @@ async def on_cleanup(self):
         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 = []
 
diff --git a/Lib/unittest/test/test_case.py b/Lib/unittest/test/test_case.py
index b8aca92a8ebe9..f3cabe44d1c88 100644
--- a/Lib/unittest/test/test_case.py
+++ b/Lib/unittest/test/test_case.py
@@ -306,6 +306,26 @@ def test(self):
 
         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):



More information about the Python-checkins mailing list