Skip to content

Commit b1db308

Browse files
authored
bpo-41322: Add unit tests for deprecation of test return values (GH-27846)
Also fix the traceback of warnings.
1 parent 6dd1cdb commit b1db308

File tree

6 files changed

+50
-2
lines changed

6 files changed

+50
-2
lines changed

Doc/library/unittest.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ The above examples show the most commonly used :mod:`unittest` features which
151151
are sufficient to meet many everyday testing needs. The remainder of the
152152
documentation explores the full feature set from first principles.
153153

154+
.. versionchanged:: 3.11
155+
The behavior of returning a value from a test method (other than the default
156+
``None`` value), is now deprecated.
157+
154158

155159
.. _unittest-command-line-interface:
156160

Doc/whatsnew/3.11.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,3 +395,7 @@ Removed
395395
:func:`~gettext.install` are also removed, since they are only used for
396396
the ``l*gettext()`` functions.
397397
(Contributed by Dong-hee Na and Serhiy Storchaka in :issue:`44235`.)
398+
399+
* The behavior of returning a value from a :class:`~unittest.TestCase` and
400+
:class:`~unittest.IsolatedAsyncioTestCase` test methods (other than the default ``None``
401+
value), is now deprecated.

Lib/unittest/async_case.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def _callSetUp(self):
6565
def _callTestMethod(self, method):
6666
if self._callMaybeAsync(method) is not None:
6767
warnings.warn(f'It is deprecated to return a value!=None from a '
68-
f'test case ({method})', DeprecationWarning)
68+
f'test case ({method})', DeprecationWarning, stacklevel=4)
6969

7070
def _callTearDown(self):
7171
self._callAsync(self.asyncTearDown)

Lib/unittest/case.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ def _callSetUp(self):
548548
def _callTestMethod(self, method):
549549
if method() is not None:
550550
warnings.warn(f'It is deprecated to return a value!=None from a '
551-
f'test case ({method})', DeprecationWarning)
551+
f'test case ({method})', DeprecationWarning, stacklevel=3)
552552

553553
def _callTearDown(self):
554554
self.tearDown()

Lib/unittest/test/test_async_case.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,26 @@ async def on_cleanup(self):
167167
test.run()
168168
self.assertEqual(events, ['asyncSetUp', 'test', 'asyncTearDown', 'cleanup'])
169169

170+
def test_deprecation_of_return_val_from_test(self):
171+
# Issue 41322 - deprecate return of value!=None from a test
172+
class Test(unittest.IsolatedAsyncioTestCase):
173+
async def test1(self):
174+
return 1
175+
async def test2(self):
176+
yield 1
177+
178+
with self.assertWarns(DeprecationWarning) as w:
179+
Test('test1').run()
180+
self.assertIn('It is deprecated to return a value!=None', str(w.warnings[0].message))
181+
self.assertIn('test1', str(w.warnings[0].message))
182+
self.assertEqual(w.warnings[0].filename, __file__)
183+
184+
with self.assertWarns(DeprecationWarning) as w:
185+
Test('test2').run()
186+
self.assertIn('It is deprecated to return a value!=None', str(w.warnings[0].message))
187+
self.assertIn('test2', str(w.warnings[0].message))
188+
self.assertEqual(w.warnings[0].filename, __file__)
189+
170190
def test_cleanups_interleave_order(self):
171191
events = []
172192

Lib/unittest/test/test_case.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,26 @@ def test(self):
306306

307307
Foo('test').run()
308308

309+
def test_deprecation_of_return_val_from_test(self):
310+
# Issue 41322 - deprecate return of value!=None from a test
311+
class Foo(unittest.TestCase):
312+
def test1(self):
313+
return 1
314+
def test2(self):
315+
yield 1
316+
317+
with self.assertWarns(DeprecationWarning) as w:
318+
Foo('test1').run()
319+
self.assertIn('It is deprecated to return a value!=None', str(w.warnings[0].message))
320+
self.assertIn('test1', str(w.warnings[0].message))
321+
self.assertEqual(w.warnings[0].filename, __file__)
322+
323+
with self.assertWarns(DeprecationWarning) as w:
324+
Foo('test2').run()
325+
self.assertIn('It is deprecated to return a value!=None', str(w.warnings[0].message))
326+
self.assertIn('test2', str(w.warnings[0].message))
327+
self.assertEqual(w.warnings[0].filename, __file__)
328+
309329
def _check_call_order__subtests(self, result, events, expected_events):
310330
class Foo(Test.LoggingTestCase):
311331
def test(self):

0 commit comments

Comments
 (0)