mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 13:41:24 +00:00 
			
		
		
		
	gh-106300: Improve assertRaises(Exception) usages in tests (GH-106302)
				
					
				
			This commit is contained in:
		
							parent
							
								
									80b9b3a517
								
							
						
					
					
						commit
						6e6a4cd523
					
				
					 7 changed files with 20 additions and 12 deletions
				
			
		|  | @ -448,15 +448,16 @@ class S(metaclass=abc_ABCMeta): | ||||||
| 
 | 
 | ||||||
|             # Also check that issubclass() propagates exceptions raised by |             # Also check that issubclass() propagates exceptions raised by | ||||||
|             # __subclasses__. |             # __subclasses__. | ||||||
|  |             class CustomError(Exception): ... | ||||||
|             exc_msg = "exception from __subclasses__" |             exc_msg = "exception from __subclasses__" | ||||||
| 
 | 
 | ||||||
|             def raise_exc(): |             def raise_exc(): | ||||||
|                 raise Exception(exc_msg) |                 raise CustomError(exc_msg) | ||||||
| 
 | 
 | ||||||
|             class S(metaclass=abc_ABCMeta): |             class S(metaclass=abc_ABCMeta): | ||||||
|                 __subclasses__ = raise_exc |                 __subclasses__ = raise_exc | ||||||
| 
 | 
 | ||||||
|             with self.assertRaisesRegex(Exception, exc_msg): |             with self.assertRaisesRegex(CustomError, exc_msg): | ||||||
|                 issubclass(int, S) |                 issubclass(int, S) | ||||||
| 
 | 
 | ||||||
|         def test_subclasshook(self): |         def test_subclasshook(self): | ||||||
|  |  | ||||||
|  | @ -2822,14 +2822,15 @@ def test_binary_to_text_denylists_text_transforms(self): | ||||||
|     def test_custom_zlib_error_is_noted(self): |     def test_custom_zlib_error_is_noted(self): | ||||||
|         # Check zlib codec gives a good error for malformed input |         # Check zlib codec gives a good error for malformed input | ||||||
|         msg = "decoding with 'zlib_codec' codec failed" |         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") |             codecs.decode(b"hello", "zlib_codec") | ||||||
|         self.assertEqual(msg, failure.exception.__notes__[0]) |         self.assertEqual(msg, failure.exception.__notes__[0]) | ||||||
| 
 | 
 | ||||||
|     def test_custom_hex_error_is_noted(self): |     def test_custom_hex_error_is_noted(self): | ||||||
|         # Check hex codec gives a good error for malformed input |         # Check hex codec gives a good error for malformed input | ||||||
|  |         import binascii | ||||||
|         msg = "decoding with 'hex_codec' codec failed" |         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") |             codecs.decode(b"hello", "hex_codec") | ||||||
|         self.assertEqual(msg, failure.exception.__notes__[0]) |         self.assertEqual(msg, failure.exception.__notes__[0]) | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -696,14 +696,16 @@ def subtype_as_add(self, method, subtype, outcome): | ||||||
|             self.assertIsNone(part['Content-Disposition']) |             self.assertIsNone(part['Content-Disposition']) | ||||||
| 
 | 
 | ||||||
|     class _TestSetRaisingContentManager: |     class _TestSetRaisingContentManager: | ||||||
|  |         class CustomError(Exception): | ||||||
|  |             pass | ||||||
|         def set_content(self, msg, content, *args, **kw): |         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): |     def test_default_content_manager_for_add_comes_from_policy(self): | ||||||
|         cm = self._TestSetRaisingContentManager() |         cm = self._TestSetRaisingContentManager() | ||||||
|         m = self.message(policy=self.policy.clone(content_manager=cm)) |         m = self.message(policy=self.policy.clone(content_manager=cm)) | ||||||
|         for method in ('add_related', 'add_alternative', 'add_attachment'): |         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)('') |                 getattr(m, method)('') | ||||||
|             self.assertEqual(str(ar.exception), 'test') |             self.assertEqual(str(ar.exception), 'test') | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -68,7 +68,7 @@ def test_abc_enforced(self): | ||||||
|         dict(name=''), |         dict(name=''), | ||||||
|     ) |     ) | ||||||
|     def test_invalid_inputs_to_from_name(self, name): |     def test_invalid_inputs_to_from_name(self, name): | ||||||
|         with self.assertRaises(Exception): |         with self.assertRaises(ValueError): | ||||||
|             Distribution.from_name(name) |             Distribution.from_name(name) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -116,10 +116,13 @@ def test_add_nonascii_string_header_raises(self): | ||||||
|         self.assertMailboxEmpty() |         self.assertMailboxEmpty() | ||||||
| 
 | 
 | ||||||
|     def test_add_that_raises_leaves_mailbox_empty(self): |     def test_add_that_raises_leaves_mailbox_empty(self): | ||||||
|  |         class CustomError(Exception): ... | ||||||
|  |         exc_msg = "a fake error" | ||||||
|  | 
 | ||||||
|         def raiser(*args, **kw): |         def raiser(*args, **kw): | ||||||
|             raise Exception("a fake error") |             raise CustomError(exc_msg) | ||||||
|         support.patch(self, email.generator.BytesGenerator, 'flatten', raiser) |         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._box.add(email.message_from_string("From: Alphöso")) | ||||||
|         self.assertEqual(len(self._box), 0) |         self.assertEqual(len(self._box), 0) | ||||||
|         self._box.close() |         self._box.close() | ||||||
|  |  | ||||||
|  | @ -2738,7 +2738,7 @@ def test_regular_copy(self): | ||||||
|     def test_same_file(self): |     def test_same_file(self): | ||||||
|         self.addCleanup(self.reset) |         self.addCleanup(self.reset) | ||||||
|         with self.get_files() as (src, dst): |         with self.get_files() as (src, dst): | ||||||
|             with self.assertRaises(Exception): |             with self.assertRaises((OSError, _GiveupOnFastCopy)): | ||||||
|                 self.zerocopy_fun(src, src) |                 self.zerocopy_fun(src, src) | ||||||
|         # Make sure src file is not corrupted. |         # Make sure src file is not corrupted. | ||||||
|         self.assertEqual(read_file(TESTFN, binary=True), self.FILEDATA) |         self.assertEqual(read_file(TESTFN, binary=True), self.FILEDATA) | ||||||
|  |  | ||||||
|  | @ -459,9 +459,10 @@ async def addition(self, var): pass | ||||||
|         self.assertEqual(output, 10) |         self.assertEqual(output, 10) | ||||||
| 
 | 
 | ||||||
|     async def test_add_side_effect_exception(self): |     async def test_add_side_effect_exception(self): | ||||||
|  |         class CustomError(Exception): pass | ||||||
|         async def addition(var): pass |         async def addition(var): pass | ||||||
|         mock = AsyncMock(addition, side_effect=Exception('err')) |         mock = AsyncMock(addition, side_effect=CustomError('side-effect')) | ||||||
|         with self.assertRaises(Exception): |         with self.assertRaisesRegex(CustomError, 'side-effect'): | ||||||
|             await mock(5) |             await mock(5) | ||||||
| 
 | 
 | ||||||
|     async def test_add_side_effect_coroutine(self): |     async def test_add_side_effect_coroutine(self): | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Nikita Sobolev
						Nikita Sobolev