Changed Exception tests to be more specific

This commit is contained in:
Andrew Murray 2018-06-08 19:17:48 +10:00
parent 62c870f5cf
commit 9a3d554c1d
4 changed files with 37 additions and 16 deletions

View File

@ -439,17 +439,17 @@ class TestFileJpeg(PillowTestCase):
self._n_qtables_helper(4, "Tests/images/pil_sample_cmyk.jpg") self._n_qtables_helper(4, "Tests/images/pil_sample_cmyk.jpg")
# not a sequence # not a sequence
self.assertRaises(Exception, self.roundtrip, im, qtables='a') self.assertRaises(ValueError, self.roundtrip, im, qtables='a')
# sequence wrong length # sequence wrong length
self.assertRaises(Exception, self.roundtrip, im, qtables=[]) self.assertRaises(ValueError, self.roundtrip, im, qtables=[])
# sequence wrong length # sequence wrong length
self.assertRaises(Exception, self.assertRaises(ValueError,
self.roundtrip, im, qtables=[1, 2, 3, 4, 5]) self.roundtrip, im, qtables=[1, 2, 3, 4, 5])
# qtable entry not a sequence # 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 # qtable entry has wrong number of items
self.assertRaises(Exception, self.assertRaises(ValueError,
self.roundtrip, im, qtables=[[1, 2, 3, 4]]) self.roundtrip, im, qtables=[[1, 2, 3, 4]])
@unittest.skipUnless(djpeg_available(), "djpeg not available") @unittest.skipUnless(djpeg_available(), "djpeg not available")
@ -599,7 +599,7 @@ class TestFileCloseW32(PillowTestCase):
im = Image.open(tmpfile) im = Image.open(tmpfile)
fp = im.fp fp = im.fp
self.assertFalse(fp.closed) self.assertFalse(fp.closed)
self.assertRaises(Exception, os.remove, tmpfile) self.assertRaises(WindowsError, os.remove, tmpfile)
im.load() im.load()
self.assertTrue(fp.closed) self.assertTrue(fp.closed)
# this should not fail, as load should have closed the file. # this should not fail, as load should have closed the file.

View File

@ -505,7 +505,7 @@ class TestFileTiffW32(PillowTestCase):
im = Image.open(tmpfile) im = Image.open(tmpfile)
fp = im.fp fp = im.fp
self.assertFalse(fp.closed) self.assertFalse(fp.closed)
self.assertRaises(Exception, os.remove, tmpfile) self.assertRaises(WindowsError, os.remove, tmpfile)
im.load() im.load()
self.assertTrue(fp.closed) self.assertTrue(fp.closed)

View File

@ -39,7 +39,7 @@ class TestImageQuantize(PillowTestCase):
def test_rgba_quantize(self): def test_rgba_quantize(self):
image = hopper('RGBA') image = hopper('RGBA')
image.quantize() image.quantize()
self.assertRaises(Exception, image.quantize, method=0) self.assertRaises(ValueError, image.quantize, method=0)
def test_quantize(self): def test_quantize(self):
image = Image.open('Tests/images/caption_6_33_22.png').convert('RGB') image = Image.open('Tests/images/caption_6_33_22.png').convert('RGB')

View File

@ -80,9 +80,15 @@ class MorphTests(PillowTestCase):
def test_no_operator_loaded(self): def test_no_operator_loaded(self):
mop = ImageMorph.MorphOp() mop = ImageMorph.MorphOp()
self.assertRaises(Exception, mop.apply, None) with self.assertRaises(Exception) as e:
self.assertRaises(Exception, mop.match, None) mop.apply(None)
self.assertRaises(Exception, mop.save_lut, 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 # Test the named patterns
def test_erosion8(self): def test_erosion8(self):
@ -213,9 +219,18 @@ class MorphTests(PillowTestCase):
im = hopper('RGB') im = hopper('RGB')
mop = ImageMorph.MorphOp(op_name="erosion8") mop = ImageMorph.MorphOp(op_name="erosion8")
self.assertRaises(Exception, mop.apply, im) with self.assertRaises(Exception) as e:
self.assertRaises(Exception, mop.match, im) mop.apply(im)
self.assertRaises(Exception, mop.get_on_pixels, 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): def test_add_patterns(self):
# Arrange # Arrange
@ -248,7 +263,10 @@ class MorphTests(PillowTestCase):
lb.add_patterns(new_patterns) lb.add_patterns(new_patterns)
# Act / Assert # 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): def test_load_invalid_mrl(self):
# Arrange # Arrange
@ -256,7 +274,10 @@ class MorphTests(PillowTestCase):
mop = ImageMorph.MorphOp() mop = ImageMorph.MorphOp()
# Act / Assert # 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): def test_roundtrip_mrl(self):
# Arrange # Arrange