Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c3648f4

Browse files
authored
gh-97837: Change deprecation warning message in unittest (#97838)
1 parent 4e73181 commit c3648f4

File tree

5 files changed

+37
-8
lines changed

5 files changed

+37
-8
lines changed

Lib/test/test_unittest/test_async_case.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -277,25 +277,36 @@ async def on_cleanup2(self):
277277
self.assertEqual(events, ['asyncSetUp', 'test', 'asyncTearDown', 'cleanup2', 'cleanup1'])
278278

279279
def test_deprecation_of_return_val_from_test(self):
280-
# Issue 41322 - deprecate return of value!=None from a test
280+
# Issue 41322 - deprecate return of value that is not None from a test
281+
class Nothing:
282+
def __eq__(self, o):
283+
return o is None
281284
class Test(unittest.IsolatedAsyncioTestCase):
282285
async def test1(self):
283286
return 1
284287
async def test2(self):
285288
yield 1
289+
async def test3(self):
290+
return Nothing()
286291

287292
with self.assertWarns(DeprecationWarning) as w:
288293
Test('test1').run()
289-
self.assertIn('It is deprecated to return a value!=None', str(w.warning))
294+
self.assertIn('It is deprecated to return a value that is not None', str(w.warning))
290295
self.assertIn('test1', str(w.warning))
291296
self.assertEqual(w.filename, __file__)
292297

293298
with self.assertWarns(DeprecationWarning) as w:
294299
Test('test2').run()
295-
self.assertIn('It is deprecated to return a value!=None', str(w.warning))
300+
self.assertIn('It is deprecated to return a value that is not None', str(w.warning))
296301
self.assertIn('test2', str(w.warning))
297302
self.assertEqual(w.filename, __file__)
298303

304+
with self.assertWarns(DeprecationWarning) as w:
305+
Test('test3').run()
306+
self.assertIn('It is deprecated to return a value that is not None', str(w.warning))
307+
self.assertIn('test3', str(w.warning))
308+
self.assertEqual(w.filename, __file__)
309+
299310
def test_cleanups_interleave_order(self):
300311
events = []
301312

Lib/test/test_unittest/test_case.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -307,25 +307,36 @@ def test(self):
307307
Foo('test').run()
308308

309309
def test_deprecation_of_return_val_from_test(self):
310-
# Issue 41322 - deprecate return of value!=None from a test
310+
# Issue 41322 - deprecate return of value that is not None from a test
311+
class Nothing:
312+
def __eq__(self, o):
313+
return o is None
311314
class Foo(unittest.TestCase):
312315
def test1(self):
313316
return 1
314317
def test2(self):
315318
yield 1
319+
def test3(self):
320+
return Nothing()
316321

317322
with self.assertWarns(DeprecationWarning) as w:
318323
Foo('test1').run()
319-
self.assertIn('It is deprecated to return a value!=None', str(w.warning))
324+
self.assertIn('It is deprecated to return a value that is not None', str(w.warning))
320325
self.assertIn('test1', str(w.warning))
321326
self.assertEqual(w.filename, __file__)
322327

323328
with self.assertWarns(DeprecationWarning) as w:
324329
Foo('test2').run()
325-
self.assertIn('It is deprecated to return a value!=None', str(w.warning))
330+
self.assertIn('It is deprecated to return a value that is not None', str(w.warning))
326331
self.assertIn('test2', str(w.warning))
327332
self.assertEqual(w.filename, __file__)
328333

334+
with self.assertWarns(DeprecationWarning) as w:
335+
Foo('test3').run()
336+
self.assertIn('It is deprecated to return a value that is not None', str(w.warning))
337+
self.assertIn('test3', str(w.warning))
338+
self.assertEqual(w.filename, __file__)
339+
329340
def _check_call_order__subtests(self, result, events, expected_events):
330341
class Foo(Test.LoggingTestCase):
331342
def test(self):

Lib/unittest/async_case.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def _callSetUp(self):
8888

8989
def _callTestMethod(self, method):
9090
if self._callMaybeAsync(method) is not None:
91-
warnings.warn(f'It is deprecated to return a value!=None from a '
91+
warnings.warn(f'It is deprecated to return a value that is not None from a '
9292
f'test case ({method})', DeprecationWarning, stacklevel=4)
9393

9494
def _callTearDown(self):

Lib/unittest/case.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ def _callSetUp(self):
577577

578578
def _callTestMethod(self, method):
579579
if method() is not None:
580-
warnings.warn(f'It is deprecated to return a value!=None from a '
580+
warnings.warn(f'It is deprecated to return a value that is not None from a '
581581
f'test case ({method})', DeprecationWarning, stacklevel=3)
582582

583583
def _callTearDown(self):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Change deprecate warning message in :mod:`unittest` from
2+
3+
``It is deprecated to return a value!=None``
4+
5+
to
6+
7+
``It is deprecated to return a value that is not None from a test case``

0 commit comments

Comments
 (0)