summaryrefslogtreecommitdiffstats
path: root/Lib/unittest
diff options
context:
space:
mode:
authorandrei kulakov <andrei.avk@gmail.com>2021-08-22 18:32:45 (GMT)
committerGitHub <noreply@github.com>2021-08-22 18:32:45 (GMT)
commitb1db308c6114bb18c06656c94c0ffa73ba954977 (patch)
treec69da403a5693c670028754c8a8d3af69e9e24b6 /Lib/unittest
parent6dd1cdb0cfd6427925863e0ff7ad3fde0d824a8b (diff)
downloadcpython-b1db308c6114bb18c06656c94c0ffa73ba954977.zip
cpython-b1db308c6114bb18c06656c94c0ffa73ba954977.tar.gz
cpython-b1db308c6114bb18c06656c94c0ffa73ba954977.tar.bz2
bpo-41322: Add unit tests for deprecation of test return values (GH-27846)
Also fix the traceback of warnings.
Diffstat (limited to 'Lib/unittest')
-rw-r--r--Lib/unittest/async_case.py2
-rw-r--r--Lib/unittest/case.py2
-rw-r--r--Lib/unittest/test/test_async_case.py20
-rw-r--r--Lib/unittest/test/test_case.py20
4 files changed, 42 insertions, 2 deletions
diff --git a/Lib/unittest/async_case.py b/Lib/unittest/async_case.py
index cc404cc..bfc68a7 100644
--- a/Lib/unittest/async_case.py
+++ b/Lib/unittest/async_case.py
@@ -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)
diff --git a/Lib/unittest/case.py b/Lib/unittest/case.py
index 625d27e..8775ba9 100644
--- a/Lib/unittest/case.py
+++ b/Lib/unittest/case.py
@@ -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()
diff --git a/Lib/unittest/test/test_async_case.py b/Lib/unittest/test/test_async_case.py
index 6e48b9e..93ef199 100644
--- a/Lib/unittest/test/test_async_case.py
+++ b/Lib/unittest/test/test_async_case.py
@@ -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 = []
diff --git a/Lib/unittest/test/test_case.py b/Lib/unittest/test/test_case.py
index b8aca92..f3cabe4 100644
--- a/Lib/unittest/test/test_case.py
+++ b/Lib/unittest/test/test_case.py
@@ -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):