Pillow/Tests/test_imagesequence.py
Jon Dufresne 7da17ad41e Improve pytest configuration to allow specific tests as CLI args
The previous test configuration made it difficult to run a single test
with the pytest CLI. There were two major issues:

- The Tests directory was not a package. It now includes a __init__.py
  file and imports from other tests modules are done with relative
  imports.

- setup.cfg always specified the Tests directory. So even if a specific
  test were specified as a CLI arg, this configuration would also always
  include all tests. This configuration has been removed to allow
  specifying a single test on the command line.

Contributors can now run specific tests with a single command such as:

  $ tox -e py37 -- Tests/test_file_pdf.py::TestFilePdf.test_rgb

This makes it easy and faster to iterate on a single test failure and is
very familiar to those that have previously used tox and pytest.

When running tox or pytest with no arguments, they still discover and
runs all tests in the Tests directory.
2019-01-13 09:00:12 -08:00

76 lines
2.2 KiB
Python

from .helper import unittest, PillowTestCase, hopper
from PIL import Image, ImageSequence, TiffImagePlugin
class TestImageSequence(PillowTestCase):
def test_sanity(self):
test_file = self.tempfile("temp.im")
im = hopper("RGB")
im.save(test_file)
seq = ImageSequence.Iterator(im)
index = 0
for frame in seq:
self.assert_image_equal(im, frame)
self.assertEqual(im.tell(), index)
index += 1
self.assertEqual(index, 1)
self.assertRaises(AttributeError, ImageSequence.Iterator, 0)
def test_iterator(self):
im = Image.open('Tests/images/multipage.tiff')
i = ImageSequence.Iterator(im)
for index in range(0, im.n_frames):
self.assertEqual(i[index], next(i))
self.assertRaises(IndexError, lambda: i[index+1])
self.assertRaises(StopIteration, next, i)
def _test_multipage_tiff(self):
im = Image.open('Tests/images/multipage.tiff')
for index, frame in enumerate(ImageSequence.Iterator(im)):
frame.load()
self.assertEqual(index, im.tell())
frame.convert('RGB')
def test_tiff(self):
self._test_multipage_tiff()
def test_libtiff(self):
codecs = dir(Image.core)
if "libtiff_encoder" not in codecs or "libtiff_decoder" not in codecs:
self.skipTest("tiff support not available")
TiffImagePlugin.READ_LIBTIFF = True
self._test_multipage_tiff()
TiffImagePlugin.READ_LIBTIFF = False
def test_consecutive(self):
im = Image.open('Tests/images/multipage.tiff')
firstFrame = None
for frame in ImageSequence.Iterator(im):
if firstFrame is None:
firstFrame = frame.copy()
for frame in ImageSequence.Iterator(im):
self.assert_image_equal(frame, firstFrame)
break
def test_palette_mmap(self):
# Using mmap in ImageFile can require to reload the palette.
im = Image.open('Tests/images/multipage-mmap.tiff')
color1 = im.getpalette()[0:3]
im.seek(0)
color2 = im.getpalette()[0:3]
self.assertEqual(color1, color2)
if __name__ == '__main__':
unittest.main()