Pillow/Tests/test_features.py
Jon Dufresne 4f185329f4 Streamline test skipping based on supported features
This adds a new test decorator: skip_unless_feature(). The argument is
the same as passed to features.check(). If the feature is not supported,
the test will be skipped.

This removes several kinds of boilerplate copied and pasted around tests
so test feature checking is handled and displayed more consistently.

Refs #4193
2020-02-18 13:07:01 -08:00

96 lines
2.5 KiB
Python

import io
import pytest
from PIL import features
from .helper import skip_unless_feature
try:
from PIL import _webp
except ImportError:
pass
def test_check():
# Check the correctness of the convenience function
for module in features.modules:
assert features.check_module(module) == features.check(module)
for codec in features.codecs:
assert features.check_codec(codec) == features.check(codec)
for feature in features.features:
assert features.check_feature(feature) == features.check(feature)
@skip_unless_feature("webp")
def test_webp_transparency():
assert features.check("transp_webp") != _webp.WebPDecoderBuggyAlpha()
assert features.check("transp_webp") == _webp.HAVE_TRANSPARENCY
@skip_unless_feature("webp")
def test_webp_mux():
assert features.check("webp_mux") == _webp.HAVE_WEBPMUX
@skip_unless_feature("webp")
def test_webp_anim():
assert features.check("webp_anim") == _webp.HAVE_WEBPANIM
def test_check_modules():
for feature in features.modules:
assert features.check_module(feature) in [True, False]
for feature in features.codecs:
assert features.check_codec(feature) in [True, False]
def test_supported_modules():
assert isinstance(features.get_supported_modules(), list)
assert isinstance(features.get_supported_codecs(), list)
assert isinstance(features.get_supported_features(), list)
assert isinstance(features.get_supported(), list)
def test_unsupported_codec():
# Arrange
codec = "unsupported_codec"
# Act / Assert
with pytest.raises(ValueError):
features.check_codec(codec)
def test_unsupported_module():
# Arrange
module = "unsupported_module"
# Act / Assert
with pytest.raises(ValueError):
features.check_module(module)
def test_pilinfo():
buf = io.StringIO()
features.pilinfo(buf)
out = buf.getvalue()
lines = out.splitlines()
assert lines[0] == "-" * 68
assert lines[1].startswith("Pillow ")
assert lines[2].startswith("Python ")
lines = lines[3:]
while lines[0].startswith(" "):
lines = lines[1:]
assert lines[0] == "-" * 68
assert lines[1].startswith("Python modules loaded from ")
assert lines[2].startswith("Binary modules loaded from ")
assert lines[3] == "-" * 68
jpeg = (
"\n"
+ "-" * 68
+ "\n"
+ "JPEG image/jpeg\n"
+ "Extensions: .jfif, .jpe, .jpeg, .jpg\n"
+ "Features: open, save\n"
+ "-" * 68
+ "\n"
)
assert jpeg in out