Skip to content

Commit 2ade2fc

Browse files
[3.12] gh-106300: Improve assertRaises(Exception) usages in tests (GH-106302) (GH-106534)
gh-106300: Improve `assertRaises(Exception)` usages in tests (GH-106302) (cherry picked from commit 6e6a4cd) Co-authored-by: Nikita Sobolev <[email protected]>
1 parent 7e883d7 commit 2ade2fc

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
@@ -2822,14 +2822,15 @@ def test_binary_to_text_denylists_text_transforms(self):
28222822
def test_custom_zlib_error_is_noted(self):
28232823
# Check zlib codec gives a good error for malformed input
28242824
msg = "decoding with 'zlib_codec' codec failed"
2825-
with self.assertRaises(Exception) as failure:
2825+
with self.assertRaises(zlib.error) as failure:
28262826
codecs.decode(b"hello", "zlib_codec")
28272827
self.assertEqual(msg, failure.exception.__notes__[0])
28282828

28292829
def test_custom_hex_error_is_noted(self):
28302830
# Check hex codec gives a good error for malformed input
2831+
import binascii
28312832
msg = "decoding with 'hex_codec' codec failed"
2832-
with self.assertRaises(Exception) as failure:
2833+
with self.assertRaises(binascii.Error) as failure:
28332834
codecs.decode(b"hello", "hex_codec")
28342835
self.assertEqual(msg, failure.exception.__notes__[0])
28352836

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
@@ -69,7 +69,7 @@ def test_abc_enforced(self):
6969
dict(name=''),
7070
)
7171
def test_invalid_inputs_to_from_name(self, name):
72-
with self.assertRaises(Exception):
72+
with self.assertRaises(ValueError):
7373
Distribution.from_name(name)
7474

7575

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
@@ -2739,7 +2739,7 @@ def test_regular_copy(self):
27392739
def test_same_file(self):
27402740
self.addCleanup(self.reset)
27412741
with self.get_files() as (src, dst):
2742-
with self.assertRaises(Exception):
2742+
with self.assertRaises((OSError, _GiveupOnFastCopy)):
27432743
self.zerocopy_fun(src, src)
27442744
# Make sure src file is not corrupted.
27452745
self.assertEqual(read_file(TESTFN, binary=True), self.FILEDATA)

Lib/test/test_unittest/testmock/testasync.py

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

438438
async def test_add_side_effect_exception(self):
439+
class CustomError(Exception): pass
439440
async def addition(var): pass
440-
mock = AsyncMock(addition, side_effect=Exception('err'))
441-
with self.assertRaises(Exception):
441+
mock = AsyncMock(addition, side_effect=CustomError('side-effect'))
442+
with self.assertRaisesRegex(CustomError, 'side-effect'):
442443
await mock(5)
443444

444445
async def test_add_side_effect_coroutine(self):

0 commit comments

Comments
 (0)