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

Skip to content

Commit 6cd08a5

Browse files
[3.11] gh-106300: Improve assertRaises(Exception) usages in tests (GH-106302). (GH-106545)
(cherry picked from commit 6e6a4cd) Co-authored-by: Nikita Sobolev <[email protected]>
1 parent 1931c2a commit 6cd08a5

File tree

7 files changed

+20
-12
lines changed

7 files changed

+20
-12
lines changed

Lib/test/test_abc.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,15 +448,16 @@ class S(metaclass=abc_ABCMeta):
448448

449449
# Also check that issubclass() propagates exceptions raised by
450450
# __subclasses__.
451+
class CustomError(Exception): ...
451452
exc_msg = "exception from __subclasses__"
452453

453454
def raise_exc():
454-
raise Exception(exc_msg)
455+
raise CustomError(exc_msg)
455456

456457
class S(metaclass=abc_ABCMeta):
457458
__subclasses__ = raise_exc
458459

459-
with self.assertRaisesRegex(Exception, exc_msg):
460+
with self.assertRaisesRegex(CustomError, exc_msg):
460461
issubclass(int, S)
461462

462463
def test_subclasshook(self):

Lib/test/test_codecs.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2823,15 +2823,16 @@ def test_binary_to_text_denylists_text_transforms(self):
28232823
def test_custom_zlib_error_is_wrapped(self):
28242824
# Check zlib codec gives a good error for malformed input
28252825
msg = "^decoding with 'zlib_codec' codec failed"
2826-
with self.assertRaisesRegex(Exception, msg) as failure:
2826+
with self.assertRaises(zlib.error) as failure:
28272827
codecs.decode(b"hello", "zlib_codec")
28282828
self.assertIsInstance(failure.exception.__cause__,
28292829
type(failure.exception))
28302830

28312831
def test_custom_hex_error_is_wrapped(self):
28322832
# Check hex codec gives a good error for malformed input
2833+
import binascii
28332834
msg = "^decoding with 'hex_codec' codec failed"
2834-
with self.assertRaisesRegex(Exception, msg) as failure:
2835+
with self.assertRaises(binascii.Error) as failure:
28352836
codecs.decode(b"hello", "hex_codec")
28362837
self.assertIsInstance(failure.exception.__cause__,
28372838
type(failure.exception))

Lib/test/test_email/test_message.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -696,14 +696,16 @@ def subtype_as_add(self, method, subtype, outcome):
696696
self.assertIsNone(part['Content-Disposition'])
697697

698698
class _TestSetRaisingContentManager:
699+
class CustomError(Exception):
700+
pass
699701
def set_content(self, msg, content, *args, **kw):
700-
raise Exception('test')
702+
raise self.CustomError('test')
701703

702704
def test_default_content_manager_for_add_comes_from_policy(self):
703705
cm = self._TestSetRaisingContentManager()
704706
m = self.message(policy=self.policy.clone(content_manager=cm))
705707
for method in ('add_related', 'add_alternative', 'add_attachment'):
706-
with self.assertRaises(Exception) as ar:
708+
with self.assertRaises(self._TestSetRaisingContentManager.CustomError) as ar:
707709
getattr(m, method)('')
708710
self.assertEqual(str(ar.exception), 'test')
709711

Lib/test/test_importlib/test_main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def test_new_style_classes(self):
5656
dict(name=''),
5757
)
5858
def test_invalid_inputs_to_from_name(self, name):
59-
with self.assertRaises(Exception):
59+
with self.assertRaises(ValueError):
6060
Distribution.from_name(name)
6161

6262

Lib/test/test_mailbox.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,13 @@ def test_add_nonascii_string_header_raises(self):
116116
self.assertMailboxEmpty()
117117

118118
def test_add_that_raises_leaves_mailbox_empty(self):
119+
class CustomError(Exception): ...
120+
exc_msg = "a fake error"
121+
119122
def raiser(*args, **kw):
120-
raise Exception("a fake error")
123+
raise CustomError(exc_msg)
121124
support.patch(self, email.generator.BytesGenerator, 'flatten', raiser)
122-
with self.assertRaises(Exception):
125+
with self.assertRaisesRegex(CustomError, exc_msg):
123126
self._box.add(email.message_from_string("From: Alphöso"))
124127
self.assertEqual(len(self._box), 0)
125128
self._box.close()

Lib/test/test_shutil.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2451,7 +2451,7 @@ def test_regular_copy(self):
24512451
def test_same_file(self):
24522452
self.addCleanup(self.reset)
24532453
with self.get_files() as (src, dst):
2454-
with self.assertRaises(Exception):
2454+
with self.assertRaises((OSError, _GiveupOnFastCopy)):
24552455
self.zerocopy_fun(src, src)
24562456
# Make sure src file is not corrupted.
24572457
self.assertEqual(read_file(TESTFN, binary=True), self.FILEDATA)

Lib/unittest/test/testmock/testasync.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,9 +427,10 @@ async def addition(self, var): pass
427427
self.assertEqual(output, 10)
428428

429429
async def test_add_side_effect_exception(self):
430+
class CustomError(Exception): pass
430431
async def addition(var): pass
431-
mock = AsyncMock(addition, side_effect=Exception('err'))
432-
with self.assertRaises(Exception):
432+
mock = AsyncMock(addition, side_effect=CustomError('side-effect'))
433+
with self.assertRaisesRegex(CustomError, 'side-effect'):
433434
await mock(5)
434435

435436
async def test_add_side_effect_coroutine(self):

0 commit comments

Comments
 (0)