Skip to content

Commit f87505c

Browse files
authored
Merge pull request #4436 from hugovk/pytest.importorskip
Use pytest.importorskip to skip on a missing import dependency
2 parents 30f6177 + f43efb0 commit f87505c

File tree

9 files changed

+358
-393
lines changed

9 files changed

+358
-393
lines changed

Tests/test_file_fpx.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
import pytest
22
from PIL import Image
33

4-
try:
5-
from PIL import FpxImagePlugin
6-
except ImportError:
7-
olefile_installed = False
8-
else:
9-
olefile_installed = True
10-
11-
pytestmark = pytest.mark.skipif(
12-
not olefile_installed, reason="olefile package not installed"
4+
FpxImagePlugin = pytest.importorskip(
5+
"PIL.FpxImagePlugin", reason="olefile not installed"
136
)
147

158

Tests/test_file_mic.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,13 @@
33

44
from .helper import assert_image_similar, hopper, skip_unless_feature
55

6-
try:
7-
from PIL import MicImagePlugin
8-
except ImportError:
9-
olefile_installed = False
10-
else:
11-
olefile_installed = True
12-
6+
MicImagePlugin = pytest.importorskip(
7+
"PIL.MicImagePlugin", reason="olefile not installed"
8+
)
9+
pytestmark = skip_unless_feature("libtiff")
1310
TEST_FILE = "Tests/images/hopper.mic"
1411

1512

16-
pytestmark = [
17-
pytest.mark.skipif(not olefile_installed, reason="olefile package not installed"),
18-
skip_unless_feature("libtiff"),
19-
]
20-
21-
2213
def test_sanity():
2314
with Image.open(TEST_FILE) as im:
2415
im.load()

Tests/test_file_webp_alpha.py

Lines changed: 88 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,119 +1,115 @@
1-
import unittest
2-
1+
import pytest
32
from PIL import Image
43

5-
from .helper import PillowTestCase, assert_image_equal, assert_image_similar, hopper
4+
from .helper import assert_image_equal, assert_image_similar, hopper
5+
6+
_webp = pytest.importorskip("PIL._webp", reason="WebP support not installed")
7+
8+
9+
def setup_module():
10+
if _webp.WebPDecoderBuggyAlpha():
11+
pytest.skip("Buggy early version of WebP installed, not testing transparency")
612

7-
try:
8-
from PIL import _webp
9-
except ImportError:
10-
_webp = None
1113

14+
def test_read_rgba():
15+
"""
16+
Can we read an RGBA mode file without error?
17+
Does it have the bits we expect?
18+
"""
1219

13-
@unittest.skipIf(_webp is None, "WebP support not installed")
14-
class TestFileWebpAlpha(PillowTestCase):
15-
def setUp(self):
16-
if _webp.WebPDecoderBuggyAlpha(self):
17-
self.skipTest(
18-
"Buggy early version of WebP installed, not testing transparency"
19-
)
20+
# Generated with `cwebp transparent.png -o transparent.webp`
21+
file_path = "Tests/images/transparent.webp"
22+
with Image.open(file_path) as image:
23+
assert image.mode == "RGBA"
24+
assert image.size == (200, 150)
25+
assert image.format == "WEBP"
26+
image.load()
27+
image.getdata()
2028

21-
def test_read_rgba(self):
22-
"""
23-
Can we read an RGBA mode file without error?
24-
Does it have the bits we expect?
25-
"""
29+
image.tobytes()
2630

27-
# Generated with `cwebp transparent.png -o transparent.webp`
28-
file_path = "Tests/images/transparent.webp"
29-
with Image.open(file_path) as image:
30-
self.assertEqual(image.mode, "RGBA")
31-
self.assertEqual(image.size, (200, 150))
32-
self.assertEqual(image.format, "WEBP")
33-
image.load()
34-
image.getdata()
31+
with Image.open("Tests/images/transparent.png") as target:
32+
assert_image_similar(image, target, 20.0)
3533

36-
image.tobytes()
3734

38-
with Image.open("Tests/images/transparent.png") as target:
39-
assert_image_similar(image, target, 20.0)
35+
def test_write_lossless_rgb(tmp_path):
36+
"""
37+
Can we write an RGBA mode file with lossless compression without error?
38+
Does it have the bits we expect?
39+
"""
4040

41-
def test_write_lossless_rgb(self):
42-
"""
43-
Can we write an RGBA mode file with lossless compression without
44-
error? Does it have the bits we expect?
45-
"""
41+
temp_file = str(tmp_path / "temp.webp")
42+
# temp_file = "temp.webp"
4643

47-
temp_file = self.tempfile("temp.webp")
48-
# temp_file = "temp.webp"
44+
pil_image = hopper("RGBA")
4945

50-
pil_image = hopper("RGBA")
46+
mask = Image.new("RGBA", (64, 64), (128, 128, 128, 128))
47+
# Add some partially transparent bits:
48+
pil_image.paste(mask, (0, 0), mask)
5149

52-
mask = Image.new("RGBA", (64, 64), (128, 128, 128, 128))
53-
# Add some partially transparent bits:
54-
pil_image.paste(mask, (0, 0), mask)
50+
pil_image.save(temp_file, lossless=True)
5551

56-
pil_image.save(temp_file, lossless=True)
52+
with Image.open(temp_file) as image:
53+
image.load()
5754

58-
with Image.open(temp_file) as image:
59-
image.load()
55+
assert image.mode == "RGBA"
56+
assert image.size == pil_image.size
57+
assert image.format == "WEBP"
58+
image.load()
59+
image.getdata()
6060

61-
self.assertEqual(image.mode, "RGBA")
62-
self.assertEqual(image.size, pil_image.size)
63-
self.assertEqual(image.format, "WEBP")
64-
image.load()
65-
image.getdata()
61+
assert_image_equal(image, pil_image)
6662

67-
assert_image_equal(image, pil_image)
6863

69-
def test_write_rgba(self):
70-
"""
71-
Can we write a RGBA mode file to webp without error.
72-
Does it have the bits we expect?
73-
"""
64+
def test_write_rgba(tmp_path):
65+
"""
66+
Can we write a RGBA mode file to WebP without error.
67+
Does it have the bits we expect?
68+
"""
7469

75-
temp_file = self.tempfile("temp.webp")
70+
temp_file = str(tmp_path / "temp.webp")
7671

77-
pil_image = Image.new("RGBA", (10, 10), (255, 0, 0, 20))
78-
pil_image.save(temp_file)
72+
pil_image = Image.new("RGBA", (10, 10), (255, 0, 0, 20))
73+
pil_image.save(temp_file)
7974

80-
if _webp.WebPDecoderBuggyAlpha(self):
81-
return
75+
if _webp.WebPDecoderBuggyAlpha():
76+
return
8277

83-
with Image.open(temp_file) as image:
84-
image.load()
78+
with Image.open(temp_file) as image:
79+
image.load()
8580

86-
self.assertEqual(image.mode, "RGBA")
87-
self.assertEqual(image.size, (10, 10))
88-
self.assertEqual(image.format, "WEBP")
89-
image.load()
90-
image.getdata()
81+
assert image.mode == "RGBA"
82+
assert image.size == (10, 10)
83+
assert image.format == "WEBP"
84+
image.load()
85+
image.getdata()
9186

92-
# early versions of webp are known to produce higher deviations:
93-
# deal with it
94-
if _webp.WebPDecoderVersion(self) <= 0x201:
95-
assert_image_similar(image, pil_image, 3.0)
96-
else:
97-
assert_image_similar(image, pil_image, 1.0)
87+
# Early versions of WebP are known to produce higher deviations:
88+
# deal with it
89+
if _webp.WebPDecoderVersion() <= 0x201:
90+
assert_image_similar(image, pil_image, 3.0)
91+
else:
92+
assert_image_similar(image, pil_image, 1.0)
9893

99-
def test_write_unsupported_mode_PA(self):
100-
"""
101-
Saving a palette-based file with transparency to WebP format
102-
should work, and be similar to the original file.
103-
"""
10494

105-
temp_file = self.tempfile("temp.webp")
106-
file_path = "Tests/images/transparent.gif"
95+
def test_write_unsupported_mode_PA(tmp_path):
96+
"""
97+
Saving a palette-based file with transparency to WebP format
98+
should work, and be similar to the original file.
99+
"""
100+
101+
temp_file = str(tmp_path / "temp.webp")
102+
file_path = "Tests/images/transparent.gif"
103+
with Image.open(file_path) as im:
104+
im.save(temp_file)
105+
with Image.open(temp_file) as image:
106+
assert image.mode == "RGBA"
107+
assert image.size == (200, 150)
108+
assert image.format == "WEBP"
109+
110+
image.load()
111+
image.getdata()
107112
with Image.open(file_path) as im:
108-
im.save(temp_file)
109-
with Image.open(temp_file) as image:
110-
self.assertEqual(image.mode, "RGBA")
111-
self.assertEqual(image.size, (200, 150))
112-
self.assertEqual(image.format, "WEBP")
113-
114-
image.load()
115-
image.getdata()
116-
with Image.open(file_path) as im:
117-
target = im.convert("RGBA")
118-
119-
assert_image_similar(image, target, 25.0)
113+
target = im.convert("RGBA")
114+
115+
assert_image_similar(image, target, 25.0)

0 commit comments

Comments
 (0)