From 9a3d554c1d38ec23fe3180458793098518d3cf2f Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Fri, 8 Jun 2018 19:17:48 +1000 Subject: [PATCH] Changed Exception tests to be more specific --- Tests/test_file_jpeg.py | 12 ++++++------ Tests/test_file_tiff.py | 2 +- Tests/test_image_quantize.py | 2 +- Tests/test_imagemorph.py | 37 ++++++++++++++++++++++++++++-------- 4 files changed, 37 insertions(+), 16 deletions(-) diff --git a/Tests/test_file_jpeg.py b/Tests/test_file_jpeg.py index e7e639f05..9804c2676 100644 --- a/Tests/test_file_jpeg.py +++ b/Tests/test_file_jpeg.py @@ -439,17 +439,17 @@ class TestFileJpeg(PillowTestCase): self._n_qtables_helper(4, "Tests/images/pil_sample_cmyk.jpg") # not a sequence - self.assertRaises(Exception, self.roundtrip, im, qtables='a') + self.assertRaises(ValueError, self.roundtrip, im, qtables='a') # sequence wrong length - self.assertRaises(Exception, self.roundtrip, im, qtables=[]) + self.assertRaises(ValueError, self.roundtrip, im, qtables=[]) # sequence wrong length - self.assertRaises(Exception, + self.assertRaises(ValueError, self.roundtrip, im, qtables=[1, 2, 3, 4, 5]) # qtable entry not a sequence - self.assertRaises(Exception, self.roundtrip, im, qtables=[1]) + self.assertRaises(ValueError, self.roundtrip, im, qtables=[1]) # qtable entry has wrong number of items - self.assertRaises(Exception, + self.assertRaises(ValueError, self.roundtrip, im, qtables=[[1, 2, 3, 4]]) @unittest.skipUnless(djpeg_available(), "djpeg not available") @@ -599,7 +599,7 @@ class TestFileCloseW32(PillowTestCase): im = Image.open(tmpfile) fp = im.fp self.assertFalse(fp.closed) - self.assertRaises(Exception, os.remove, tmpfile) + self.assertRaises(WindowsError, os.remove, tmpfile) im.load() self.assertTrue(fp.closed) # this should not fail, as load should have closed the file. diff --git a/Tests/test_file_tiff.py b/Tests/test_file_tiff.py index 3d63e084d..aac845e0f 100644 --- a/Tests/test_file_tiff.py +++ b/Tests/test_file_tiff.py @@ -505,7 +505,7 @@ class TestFileTiffW32(PillowTestCase): im = Image.open(tmpfile) fp = im.fp self.assertFalse(fp.closed) - self.assertRaises(Exception, os.remove, tmpfile) + self.assertRaises(WindowsError, os.remove, tmpfile) im.load() self.assertTrue(fp.closed) diff --git a/Tests/test_image_quantize.py b/Tests/test_image_quantize.py index 6a9ff1187..5b10d2de3 100644 --- a/Tests/test_image_quantize.py +++ b/Tests/test_image_quantize.py @@ -39,7 +39,7 @@ class TestImageQuantize(PillowTestCase): def test_rgba_quantize(self): image = hopper('RGBA') image.quantize() - self.assertRaises(Exception, image.quantize, method=0) + self.assertRaises(ValueError, image.quantize, method=0) def test_quantize(self): image = Image.open('Tests/images/caption_6_33_22.png').convert('RGB') diff --git a/Tests/test_imagemorph.py b/Tests/test_imagemorph.py index cad3caebd..83fa0b5a9 100644 --- a/Tests/test_imagemorph.py +++ b/Tests/test_imagemorph.py @@ -80,9 +80,15 @@ class MorphTests(PillowTestCase): def test_no_operator_loaded(self): mop = ImageMorph.MorphOp() - self.assertRaises(Exception, mop.apply, None) - self.assertRaises(Exception, mop.match, None) - self.assertRaises(Exception, mop.save_lut, None) + with self.assertRaises(Exception) as e: + mop.apply(None) + self.assertEqual(str(e.exception), 'No operator loaded') + with self.assertRaises(Exception) as e: + mop.match(None) + self.assertEqual(str(e.exception), 'No operator loaded') + with self.assertRaises(Exception) as e: + mop.save_lut(None) + self.assertEqual(str(e.exception), 'No operator loaded') # Test the named patterns def test_erosion8(self): @@ -213,9 +219,18 @@ class MorphTests(PillowTestCase): im = hopper('RGB') mop = ImageMorph.MorphOp(op_name="erosion8") - self.assertRaises(Exception, mop.apply, im) - self.assertRaises(Exception, mop.match, im) - self.assertRaises(Exception, mop.get_on_pixels, im) + with self.assertRaises(Exception) as e: + mop.apply(im) + self.assertEqual(str(e.exception), + 'Image must be binary, meaning it must use mode L') + with self.assertRaises(Exception) as e: + mop.match(im) + self.assertEqual(str(e.exception), + 'Image must be binary, meaning it must use mode L') + with self.assertRaises(Exception) as e: + mop.get_on_pixels(im) + self.assertEqual(str(e.exception), + 'Image must be binary, meaning it must use mode L') def test_add_patterns(self): # Arrange @@ -248,7 +263,10 @@ class MorphTests(PillowTestCase): lb.add_patterns(new_patterns) # Act / Assert - self.assertRaises(Exception, lb.build_lut) + with self.assertRaises(Exception) as e: + lb.build_lut() + self.assertEqual(str(e.exception), + 'Syntax error in pattern "a pattern with a syntax error"') def test_load_invalid_mrl(self): # Arrange @@ -256,7 +274,10 @@ class MorphTests(PillowTestCase): mop = ImageMorph.MorphOp() # Act / Assert - self.assertRaises(Exception, mop.load_lut, invalid_mrl) + with self.assertRaises(Exception) as e: + mop.load_lut(invalid_mrl) + self.assertEqual(str(e.exception), + 'Wrong size operator file!') def test_roundtrip_mrl(self): # Arrange