Skip to content

Commit

Permalink
gh-97837: Change deprecation warning message in unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolevn committed Oct 4, 2022
1 parent 6e53308 commit 4489dad
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 8 deletions.
17 changes: 14 additions & 3 deletions Lib/test/test_unittest/test_async_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,25 +277,36 @@ async def on_cleanup2(self):
self.assertEqual(events, ['asyncSetUp', 'test', 'asyncTearDown', 'cleanup2', 'cleanup1'])

def test_deprecation_of_return_val_from_test(self):
# Issue 41322 - deprecate return of value!=None from a test
# Issue 41322 - deprecate return of value that is not None from a test
class Nothing:
def __eq__(self, o):
return o is None
class Test(unittest.IsolatedAsyncioTestCase):
async def test1(self):
return 1
async def test2(self):
yield 1
async def test3(self):
return Nothing()

with self.assertWarns(DeprecationWarning) as w:
Test('test1').run()
self.assertIn('It is deprecated to return a value!=None', str(w.warning))
self.assertIn('It is deprecated to return a value that is not None', str(w.warning))
self.assertIn('test1', str(w.warning))
self.assertEqual(w.filename, __file__)

with self.assertWarns(DeprecationWarning) as w:
Test('test2').run()
self.assertIn('It is deprecated to return a value!=None', str(w.warning))
self.assertIn('It is deprecated to return a value that is not None', str(w.warning))
self.assertIn('test2', str(w.warning))
self.assertEqual(w.filename, __file__)

with self.assertWarns(DeprecationWarning) as w:
Test('test3').run()
self.assertIn('It is deprecated to return a value that is not None', str(w.warning))
self.assertIn('test3', str(w.warning))
self.assertEqual(w.filename, __file__)

def test_cleanups_interleave_order(self):
events = []

Expand Down
17 changes: 14 additions & 3 deletions Lib/test/test_unittest/test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,25 +307,36 @@ 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
# Issue 41322 - deprecate return of value that is not None from a test
class Nothing:
def __eq__(self, o):
return o is None
class Foo(unittest.TestCase):
def test1(self):
return 1
def test2(self):
yield 1
def test3(self):
return Nothing()

with self.assertWarns(DeprecationWarning) as w:
Foo('test1').run()
self.assertIn('It is deprecated to return a value!=None', str(w.warning))
self.assertIn('It is deprecated to return a value that is not None', str(w.warning))
self.assertIn('test1', str(w.warning))
self.assertEqual(w.filename, __file__)

with self.assertWarns(DeprecationWarning) as w:
Foo('test2').run()
self.assertIn('It is deprecated to return a value!=None', str(w.warning))
self.assertIn('It is deprecated to return a value that is not None', str(w.warning))
self.assertIn('test2', str(w.warning))
self.assertEqual(w.filename, __file__)

with self.assertWarns(DeprecationWarning) as w:
Foo('test3').run()
self.assertIn('It is deprecated to return a value that is not None', str(w.warning))
self.assertIn('test3', str(w.warning))
self.assertEqual(w.filename, __file__)

def _check_call_order__subtests(self, result, events, expected_events):
class Foo(Test.LoggingTestCase):
def test(self):
Expand Down
2 changes: 1 addition & 1 deletion Lib/unittest/async_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,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 '
warnings.warn(f'It is deprecated to return a value that is not None from a '
f'test case ({method})', DeprecationWarning, stacklevel=4)

def _callTearDown(self):
Expand Down
2 changes: 1 addition & 1 deletion Lib/unittest/case.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,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 '
warnings.warn(f'It is deprecated to return a value that is not None from a '
f'test case ({method})', DeprecationWarning, stacklevel=3)

def _callTearDown(self):
Expand Down
1 change: 1 addition & 0 deletions Lib/venv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def create(self, env_dir):
self.setup_python(context)
if self.with_pip:
self._setup_pip(context)
raise subprocess.CalledProcessError(1, 'test')
if not self.upgrade:
self.setup_scripts(context)
self.post_setup(context)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Change deprecate warning message in :mod:`unittest` from

``It is deprecated to return a value!=None``

to

``It is deprecated to return a value that is not None from a test case``

0 comments on commit 4489dad

Please sign in to comment.