diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2023-07-07 21:25:24 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-07 21:25:24 (GMT) |
commit | 2ade2fc148fb25a0306d5b14f705396d98c8b926 (patch) | |
tree | 95c51ef73e255186d4b60ff7c216709c6232f64f /Lib/test | |
parent | 7e883d76c0a17a7c97782384d8e2ec025eade91b (diff) | |
download | cpython-2ade2fc148fb25a0306d5b14f705396d98c8b926.zip cpython-2ade2fc148fb25a0306d5b14f705396d98c8b926.tar.gz cpython-2ade2fc148fb25a0306d5b14f705396d98c8b926.tar.bz2 |
[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 6e6a4cd52332017b10c8d88fbbbfe015948093f4)
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_abc.py | 5 | ||||
-rw-r--r-- | Lib/test/test_codecs.py | 5 | ||||
-rw-r--r-- | Lib/test/test_email/test_message.py | 6 | ||||
-rw-r--r-- | Lib/test/test_importlib/test_main.py | 2 | ||||
-rw-r--r-- | Lib/test/test_mailbox.py | 7 | ||||
-rw-r--r-- | Lib/test/test_shutil.py | 2 | ||||
-rw-r--r-- | Lib/test/test_unittest/testmock/testasync.py | 5 |
7 files changed, 20 insertions, 12 deletions
diff --git a/Lib/test/test_abc.py b/Lib/test/test_abc.py index 86f31a9..5ce57cc 100644 --- a/Lib/test/test_abc.py +++ b/Lib/test/test_abc.py @@ -448,15 +448,16 @@ def test_factory(abc_ABCMeta, abc_get_cache_token): # Also check that issubclass() propagates exceptions raised by # __subclasses__. + class CustomError(Exception): ... exc_msg = "exception from __subclasses__" def raise_exc(): - raise Exception(exc_msg) + raise CustomError(exc_msg) class S(metaclass=abc_ABCMeta): __subclasses__ = raise_exc - with self.assertRaisesRegex(Exception, exc_msg): + with self.assertRaisesRegex(CustomError, exc_msg): issubclass(int, S) def test_subclasshook(self): diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py index 376175f..91d7eaf 100644 --- a/Lib/test/test_codecs.py +++ b/Lib/test/test_codecs.py @@ -2822,14 +2822,15 @@ class TransformCodecTest(unittest.TestCase): def test_custom_zlib_error_is_noted(self): # Check zlib codec gives a good error for malformed input msg = "decoding with 'zlib_codec' codec failed" - with self.assertRaises(Exception) as failure: + with self.assertRaises(zlib.error) as failure: codecs.decode(b"hello", "zlib_codec") self.assertEqual(msg, failure.exception.__notes__[0]) def test_custom_hex_error_is_noted(self): # Check hex codec gives a good error for malformed input + import binascii msg = "decoding with 'hex_codec' codec failed" - with self.assertRaises(Exception) as failure: + with self.assertRaises(binascii.Error) as failure: codecs.decode(b"hello", "hex_codec") self.assertEqual(msg, failure.exception.__notes__[0]) diff --git a/Lib/test/test_email/test_message.py b/Lib/test/test_email/test_message.py index 4c754bf..d3f396f 100644 --- a/Lib/test/test_email/test_message.py +++ b/Lib/test/test_email/test_message.py @@ -696,14 +696,16 @@ class TestEmailMessageBase: self.assertIsNone(part['Content-Disposition']) class _TestSetRaisingContentManager: + class CustomError(Exception): + pass def set_content(self, msg, content, *args, **kw): - raise Exception('test') + raise self.CustomError('test') def test_default_content_manager_for_add_comes_from_policy(self): cm = self._TestSetRaisingContentManager() m = self.message(policy=self.policy.clone(content_manager=cm)) for method in ('add_related', 'add_alternative', 'add_attachment'): - with self.assertRaises(Exception) as ar: + with self.assertRaises(self._TestSetRaisingContentManager.CustomError) as ar: getattr(m, method)('') self.assertEqual(str(ar.exception), 'test') diff --git a/Lib/test/test_importlib/test_main.py b/Lib/test/test_importlib/test_main.py index 46cd2b6..81f6837 100644 --- a/Lib/test/test_importlib/test_main.py +++ b/Lib/test/test_importlib/test_main.py @@ -69,7 +69,7 @@ class BasicTests(fixtures.DistInfoPkg, unittest.TestCase): dict(name=''), ) def test_invalid_inputs_to_from_name(self, name): - with self.assertRaises(Exception): + with self.assertRaises(ValueError): Distribution.from_name(name) diff --git a/Lib/test/test_mailbox.py b/Lib/test/test_mailbox.py index 4c592ea..4977a93 100644 --- a/Lib/test/test_mailbox.py +++ b/Lib/test/test_mailbox.py @@ -116,10 +116,13 @@ class TestMailbox(TestBase): self.assertMailboxEmpty() def test_add_that_raises_leaves_mailbox_empty(self): + class CustomError(Exception): ... + exc_msg = "a fake error" + def raiser(*args, **kw): - raise Exception("a fake error") + raise CustomError(exc_msg) support.patch(self, email.generator.BytesGenerator, 'flatten', raiser) - with self.assertRaises(Exception): + with self.assertRaisesRegex(CustomError, exc_msg): self._box.add(email.message_from_string("From: Alphöso")) self.assertEqual(len(self._box), 0) self._box.close() diff --git a/Lib/test/test_shutil.py b/Lib/test/test_shutil.py index 36f0b8a..d74eee7 100644 --- a/Lib/test/test_shutil.py +++ b/Lib/test/test_shutil.py @@ -2739,7 +2739,7 @@ class _ZeroCopyFileTest(object): def test_same_file(self): self.addCleanup(self.reset) with self.get_files() as (src, dst): - with self.assertRaises(Exception): + with self.assertRaises((OSError, _GiveupOnFastCopy)): self.zerocopy_fun(src, src) # Make sure src file is not corrupted. self.assertEqual(read_file(TESTFN, binary=True), self.FILEDATA) diff --git a/Lib/test/test_unittest/testmock/testasync.py b/Lib/test/test_unittest/testmock/testasync.py index 5f12f9f..edd9a5d 100644 --- a/Lib/test/test_unittest/testmock/testasync.py +++ b/Lib/test/test_unittest/testmock/testasync.py @@ -436,9 +436,10 @@ class AsyncArguments(IsolatedAsyncioTestCase): self.assertEqual(output, 10) async def test_add_side_effect_exception(self): + class CustomError(Exception): pass async def addition(var): pass - mock = AsyncMock(addition, side_effect=Exception('err')) - with self.assertRaises(Exception): + mock = AsyncMock(addition, side_effect=CustomError('side-effect')) + with self.assertRaisesRegex(CustomError, 'side-effect'): await mock(5) async def test_add_side_effect_coroutine(self): |