Pillow/Tests/test_imagetk.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

94 lines
2.4 KiB
Python

from .helper import unittest, PillowTestCase, hopper
from PIL import Image
from PIL._util import py3
try:
from PIL import ImageTk
if py3:
import tkinter as tk
else:
import Tkinter as tk
dir(ImageTk)
HAS_TK = True
except (OSError, ImportError):
# Skipped via setUp()
HAS_TK = False
TK_MODES = ('1', 'L', 'P', 'RGB', 'RGBA')
@unittest.skipIf(not HAS_TK, "Tk not installed")
class TestImageTk(PillowTestCase):
def setUp(self):
try:
# setup tk
tk.Frame()
# root = tk.Tk()
except tk.TclError as v:
self.skipTest("TCL Error: %s" % v)
def test_kw(self):
TEST_JPG = "Tests/images/hopper.jpg"
TEST_PNG = "Tests/images/hopper.png"
im1 = Image.open(TEST_JPG)
im2 = Image.open(TEST_PNG)
with open(TEST_PNG, 'rb') as fp:
data = fp.read()
kw = {"file": TEST_JPG, "data": data}
# Test "file"
im = ImageTk._get_image_from_kw(kw)
self.assert_image_equal(im, im1)
# Test "data"
im = ImageTk._get_image_from_kw(kw)
self.assert_image_equal(im, im2)
# Test no relevant entry
im = ImageTk._get_image_from_kw(kw)
self.assertIsNone(im)
def test_photoimage(self):
for mode in TK_MODES:
# test as image:
im = hopper(mode)
# this should not crash
im_tk = ImageTk.PhotoImage(im)
self.assertEqual(im_tk.width(), im.width)
self.assertEqual(im_tk.height(), im.height)
# _tkinter.TclError: this function is not yet supported
# reloaded = ImageTk.getimage(im_tk)
# self.assert_image_equal(reloaded, im)
def test_photoimage_blank(self):
# test a image using mode/size:
for mode in TK_MODES:
im_tk = ImageTk.PhotoImage(mode, (100, 100))
self.assertEqual(im_tk.width(), 100)
self.assertEqual(im_tk.height(), 100)
# reloaded = ImageTk.getimage(im_tk)
# self.assert_image_equal(reloaded, im)
def test_bitmapimage(self):
im = hopper('1')
# this should not crash
im_tk = ImageTk.BitmapImage(im)
self.assertEqual(im_tk.width(), im.width)
self.assertEqual(im_tk.height(), im.height)
# reloaded = ImageTk.getimage(im_tk)
# self.assert_image_equal(reloaded, im)
if __name__ == '__main__':
unittest.main()