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

85 lines
2.3 KiB
Python

from .helper import unittest, PillowTestCase, hopper
from PIL import ImageQt
if ImageQt.qt_is_installed:
from PIL.ImageQt import qRgba
def skip_if_qt_is_not_installed(_):
pass
else:
def skip_if_qt_is_not_installed(test_case):
test_case.skipTest('Qt bindings are not installed')
class PillowQtTestCase(object):
def setUp(self):
skip_if_qt_is_not_installed(self)
def tearDown(self):
pass
class PillowQPixmapTestCase(PillowQtTestCase):
def setUp(self):
PillowQtTestCase.setUp(self)
try:
if ImageQt.qt_version == '5':
from PyQt5.QtGui import QGuiApplication
elif ImageQt.qt_version == '4':
from PyQt4.QtGui import QGuiApplication
elif ImageQt.qt_version == 'side':
from PySide.QtGui import QGuiApplication
elif ImageQt.qt_version == 'side2':
from PySide2.QtGui import QGuiApplication
except ImportError:
self.skipTest('QGuiApplication not installed')
self.app = QGuiApplication([])
def tearDown(self):
PillowQtTestCase.tearDown(self)
self.app.quit()
class TestImageQt(PillowQtTestCase, PillowTestCase):
def test_rgb(self):
# from https://doc.qt.io/archives/qt-4.8/qcolor.html
# typedef QRgb
# An ARGB quadruplet on the format #AARRGGBB,
# equivalent to an unsigned int.
if ImageQt.qt_version == '5':
from PyQt5.QtGui import qRgb
elif ImageQt.qt_version == '4':
from PyQt4.QtGui import qRgb
elif ImageQt.qt_version == 'side':
from PySide.QtGui import qRgb
elif ImageQt.qt_version == 'side2':
from PySide2.QtGui import qRgb
self.assertEqual(qRgb(0, 0, 0), qRgba(0, 0, 0, 255))
def checkrgb(r, g, b):
val = ImageQt.rgb(r, g, b)
val = val % 2**24 # drop the alpha
self.assertEqual(val >> 16, r)
self.assertEqual(((val >> 8) % 2**8), g)
self.assertEqual(val % 2**8, b)
checkrgb(0, 0, 0)
checkrgb(255, 0, 0)
checkrgb(0, 255, 0)
checkrgb(0, 0, 255)
def test_image(self):
for mode in ('1', 'RGB', 'RGBA', 'L', 'P'):
ImageQt.ImageQt(hopper(mode))
if __name__ == '__main__':
unittest.main()