Skip to content

Commit c259ac4

Browse files
committed
Parametrized tests
1 parent 985fec2 commit c259ac4

25 files changed

Lines changed: 350 additions & 522 deletions

Tests/test_features.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,14 @@ def test_libimagequant_version():
7070
assert re.search(r"\d+\.\d+\.\d+$", features.version("libimagequant"))
7171

7272

73-
def test_check_modules():
74-
for feature in features.modules:
75-
assert features.check_module(feature) in [True, False]
73+
@pytest.mark.parametrize("feature", features.modules)
74+
def test_check_modules(feature):
75+
assert features.check_module(feature) in [True, False]
7676

7777

78-
def test_check_codecs():
79-
for feature in features.codecs:
80-
assert features.check_codec(feature) in [True, False]
78+
@pytest.mark.parametrize("feature", features.codecs)
79+
def test_check_codecs(feature):
80+
assert features.check_codec(feature) in [True, False]
8181

8282

8383
def test_check_warns_on_nonexistent():

Tests/test_file_apng.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,12 @@ def test_apng_basic():
3939
assert im.getpixel((64, 32)) == (0, 255, 0, 255)
4040

4141

42-
def test_apng_fdat():
43-
with Image.open("Tests/images/apng/split_fdat.png") as im:
44-
im.seek(im.n_frames - 1)
45-
assert im.getpixel((0, 0)) == (0, 255, 0, 255)
46-
assert im.getpixel((64, 32)) == (0, 255, 0, 255)
47-
48-
with Image.open("Tests/images/apng/split_fdat_zero_chunk.png") as im:
42+
@pytest.mark.parametrize(
43+
"filename",
44+
("Tests/images/apng/split_fdat.png", "Tests/images/apng/split_fdat_zero_chunk.png"),
45+
)
46+
def test_apng_fdat(filename):
47+
with Image.open(filename) as im:
4948
im.seek(im.n_frames - 1)
5049
assert im.getpixel((0, 0)) == (0, 255, 0, 255)
5150
assert im.getpixel((64, 32)) == (0, 255, 0, 255)

Tests/test_file_eps.py

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -195,25 +195,23 @@ def test_render_scale2():
195195

196196

197197
@pytest.mark.skipif(not HAS_GHOSTSCRIPT, reason="Ghostscript not available")
198-
def test_resize():
199-
files = [FILE1, FILE2, "Tests/images/illu10_preview.eps"]
200-
for fn in files:
201-
with Image.open(fn) as im:
202-
new_size = (100, 100)
203-
im = im.resize(new_size)
204-
assert im.size == new_size
198+
@pytest.mark.parametrize("filename", (FILE1, FILE2, "Tests/images/illu10_preview.eps"))
199+
def test_resize(filename):
200+
with Image.open(filename) as im:
201+
new_size = (100, 100)
202+
im = im.resize(new_size)
203+
assert im.size == new_size
205204

206205

207206
@pytest.mark.skipif(not HAS_GHOSTSCRIPT, reason="Ghostscript not available")
208-
def test_thumbnail():
207+
@pytest.mark.parametrize("filename", (FILE1, FILE2))
208+
def test_thumbnail(filename):
209209
# Issue #619
210210
# Arrange
211-
files = [FILE1, FILE2]
212-
for fn in files:
213-
with Image.open(FILE1) as im:
214-
new_size = (100, 100)
215-
im.thumbnail(new_size)
216-
assert max(im.size) == max(new_size)
211+
with Image.open(filename) as im:
212+
new_size = (100, 100)
213+
im.thumbnail(new_size)
214+
assert max(im.size) == max(new_size)
217215

218216

219217
def test_read_binary_preview():
@@ -258,20 +256,19 @@ def _test_readline_file_psfile(test_string, ending):
258256
_test_readline_file_psfile(s, ending)
259257

260258

261-
def test_open_eps():
262-
# https://github.com/python-pillow/Pillow/issues/1104
263-
# Arrange
264-
FILES = [
259+
@pytest.mark.parametrize(
260+
"filename",
261+
(
265262
"Tests/images/illu10_no_preview.eps",
266263
"Tests/images/illu10_preview.eps",
267264
"Tests/images/illuCS6_no_preview.eps",
268265
"Tests/images/illuCS6_preview.eps",
269-
]
270-
271-
# Act / Assert
272-
for filename in FILES:
273-
with Image.open(filename) as img:
274-
assert img.mode == "RGB"
266+
),
267+
)
268+
def test_open_eps(filename):
269+
# https://github.com/python-pillow/Pillow/issues/1104
270+
with Image.open(filename) as img:
271+
assert img.mode == "RGB"
275272

276273

277274
@pytest.mark.skipif(not HAS_GHOSTSCRIPT, reason="Ghostscript not available")

Tests/test_file_gif.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -793,24 +793,24 @@ def test_identical_frames(tmp_path):
793793
assert reread.info["duration"] == 4500
794794

795795

796-
def test_identical_frames_to_single_frame(tmp_path):
797-
for duration in ([1000, 1500, 2000, 4000], (1000, 1500, 2000, 4000), 8500):
798-
out = str(tmp_path / "temp.gif")
799-
im_list = [
800-
Image.new("L", (100, 100), "#000"),
801-
Image.new("L", (100, 100), "#000"),
802-
Image.new("L", (100, 100), "#000"),
803-
]
804-
805-
im_list[0].save(
806-
out, save_all=True, append_images=im_list[1:], duration=duration
807-
)
808-
with Image.open(out) as reread:
809-
# Assert that all frames were combined
810-
assert reread.n_frames == 1
796+
@pytest.mark.parametrize(
797+
"duration", ([1000, 1500, 2000, 4000], (1000, 1500, 2000, 4000), 8500)
798+
)
799+
def test_identical_frames_to_single_frame(duration, tmp_path):
800+
out = str(tmp_path / "temp.gif")
801+
im_list = [
802+
Image.new("L", (100, 100), "#000"),
803+
Image.new("L", (100, 100), "#000"),
804+
Image.new("L", (100, 100), "#000"),
805+
]
811806

812-
# Assert that the new duration is the total of the identical frames
813-
assert reread.info["duration"] == 8500
807+
im_list[0].save(out, save_all=True, append_images=im_list[1:], duration=duration)
808+
with Image.open(out) as reread:
809+
# Assert that all frames were combined
810+
assert reread.n_frames == 1
811+
812+
# Assert that the new duration is the total of the identical frames
813+
assert reread.info["duration"] == 8500
814814

815815

816816
def test_number_of_loops(tmp_path):

Tests/test_file_jpeg.py

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -150,27 +150,30 @@ def test_icc(self, tmp_path):
150150
assert not im1.info.get("icc_profile")
151151
assert im2.info.get("icc_profile")
152152

153-
def test_icc_big(self):
153+
@pytest.mark.parametrize(
154+
"n",
155+
(
156+
0,
157+
1,
158+
3,
159+
4,
160+
5,
161+
65533 - 14, # full JPEG marker block
162+
65533 - 14 + 1, # full block plus one byte
163+
ImageFile.MAXBLOCK, # full buffer block
164+
ImageFile.MAXBLOCK + 1, # full buffer block plus one byte
165+
ImageFile.MAXBLOCK * 4 + 3, # large block
166+
),
167+
)
168+
def test_icc_big(self, n):
154169
# Make sure that the "extra" support handles large blocks
155-
def test(n):
156-
# The ICC APP marker can store 65519 bytes per marker, so
157-
# using a 4-byte test code should allow us to detect out of
158-
# order issues.
159-
icc_profile = (b"Test" * int(n / 4 + 1))[:n]
160-
assert len(icc_profile) == n # sanity
161-
im1 = self.roundtrip(hopper(), icc_profile=icc_profile)
162-
assert im1.info.get("icc_profile") == (icc_profile or None)
163-
164-
test(0)
165-
test(1)
166-
test(3)
167-
test(4)
168-
test(5)
169-
test(65533 - 14) # full JPEG marker block
170-
test(65533 - 14 + 1) # full block plus one byte
171-
test(ImageFile.MAXBLOCK) # full buffer block
172-
test(ImageFile.MAXBLOCK + 1) # full buffer block plus one byte
173-
test(ImageFile.MAXBLOCK * 4 + 3) # large block
170+
# The ICC APP marker can store 65519 bytes per marker, so
171+
# using a 4-byte test code should allow us to detect out of
172+
# order issues.
173+
icc_profile = (b"Test" * int(n / 4 + 1))[:n]
174+
assert len(icc_profile) == n # sanity
175+
im1 = self.roundtrip(hopper(), icc_profile=icc_profile)
176+
assert im1.info.get("icc_profile") == (icc_profile or None)
174177

175178
@mark_if_feature_version(
176179
pytest.mark.valgrind_known_error, "libjpeg_turbo", "2.0", reason="Known Failing"
@@ -649,19 +652,19 @@ def test_bad_mpo_header(self):
649652
# Assert
650653
assert im.format == "JPEG"
651654

652-
def test_save_correct_modes(self):
655+
@pytest.mark.parametrize("mode", ("1", "L", "RGB", "RGBX", "CMYK", "YCbCr"))
656+
def test_save_correct_modes(self, mode):
653657
out = BytesIO()
654-
for mode in ["1", "L", "RGB", "RGBX", "CMYK", "YCbCr"]:
655-
img = Image.new(mode, (20, 20))
656-
img.save(out, "JPEG")
658+
img = Image.new(mode, (20, 20))
659+
img.save(out, "JPEG")
657660

658-
def test_save_wrong_modes(self):
661+
@pytest.mark.parametrize("mode", ("LA", "La", "RGBA", "RGBa", "P"))
662+
def test_save_wrong_modes(self, mode):
659663
# ref https://github.com/python-pillow/Pillow/issues/2005
660664
out = BytesIO()
661-
for mode in ["LA", "La", "RGBA", "RGBa", "P"]:
662-
img = Image.new(mode, (20, 20))
663-
with pytest.raises(OSError):
664-
img.save(out, "JPEG")
665+
img = Image.new(mode, (20, 20))
666+
with pytest.raises(OSError):
667+
img.save(out, "JPEG")
665668

666669
def test_save_tiff_with_dpi(self, tmp_path):
667670
# Arrange

Tests/test_file_jpeg2k.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,14 @@ def test_prog_res_rt():
126126
assert_image_equal(im, test_card)
127127

128128

129-
def test_default_num_resolutions():
130-
for num_resolutions in range(2, 6):
131-
d = 1 << (num_resolutions - 1)
132-
im = test_card.resize((d - 1, d - 1))
133-
with pytest.raises(OSError):
134-
roundtrip(im, num_resolutions=num_resolutions)
135-
reloaded = roundtrip(im)
136-
assert_image_equal(im, reloaded)
129+
@pytest.mark.parametrize("num_resolutions", range(2, 6))
130+
def test_default_num_resolutions(num_resolutions):
131+
d = 1 << (num_resolutions - 1)
132+
im = test_card.resize((d - 1, d - 1))
133+
with pytest.raises(OSError):
134+
roundtrip(im, num_resolutions=num_resolutions)
135+
reloaded = roundtrip(im)
136+
assert_image_equal(im, reloaded)
137137

138138

139139
def test_reduce():
@@ -266,14 +266,11 @@ def test_rgba():
266266
assert jp2.mode == "RGBA"
267267

268268

269-
def test_16bit_monochrome_has_correct_mode():
270-
with Image.open("Tests/images/16bit.cropped.j2k") as j2k:
271-
j2k.load()
272-
assert j2k.mode == "I;16"
273-
274-
with Image.open("Tests/images/16bit.cropped.jp2") as jp2:
275-
jp2.load()
276-
assert jp2.mode == "I;16"
269+
@pytest.mark.parametrize("ext", (".j2k", ".jp2"))
270+
def test_16bit_monochrome_has_correct_mode(ext):
271+
with Image.open("Tests/images/16bit.cropped" + ext) as im:
272+
im.load()
273+
assert im.mode == "I;16"
277274

278275

279276
def test_16bit_monochrome_jp2_like_tiff():

Tests/test_file_libtiff.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,8 @@ def test_palette_save(self, im, tmp_path):
509509
# colormap/palette tag
510510
assert len(reloaded.tag_v2[320]) == 768
511511

512-
def xtest_bw_compression_w_rgb(self, tmp_path):
512+
@pytest.mark.parametrize("compression", ("tiff_ccitt", "group3", "group4"))
513+
def xtest_bw_compression_w_rgb(self, compression, tmp_path):
513514
"""This test passes, but when running all tests causes a failure due
514515
to output on stderr from the error thrown by libtiff. We need to
515516
capture that but not now"""
@@ -518,11 +519,7 @@ def xtest_bw_compression_w_rgb(self, tmp_path):
518519
out = str(tmp_path / "temp.tif")
519520

520521
with pytest.raises(OSError):
521-
im.save(out, compression="tiff_ccitt")
522-
with pytest.raises(OSError):
523-
im.save(out, compression="group3")
524-
with pytest.raises(OSError):
525-
im.save(out, compression="group4")
522+
im.save(out, compression=compression)
526523

527524
def test_fp_leak(self):
528525
im = Image.open("Tests/images/hopper_g4_500.tif")

Tests/test_file_palm.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,7 @@ def test_p_mode(tmp_path):
6363
roundtrip(tmp_path, mode)
6464

6565

66-
def test_l_oserror(tmp_path):
67-
# Arrange
68-
mode = "L"
69-
70-
# Act / Assert
71-
with pytest.raises(OSError):
72-
helper_save_as_palm(tmp_path, mode)
73-
74-
75-
def test_rgb_oserror(tmp_path):
76-
# Arrange
77-
mode = "RGB"
78-
79-
# Act / Assert
66+
@pytest.mark.parametrize("mode", ("L", "RGB"))
67+
def test_oserror(tmp_path, mode):
8068
with pytest.raises(OSError):
8169
helper_save_as_palm(tmp_path, mode)

Tests/test_file_pcx.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ def test_invalid_file():
3939
PcxImagePlugin.PcxImageFile(invalid_file)
4040

4141

42-
def test_odd(tmp_path):
42+
@pytest.mark.parametrize("mode", ("1", "L", "P", "RGB"))
43+
def test_odd(tmp_path, mode):
4344
# See issue #523, odd sized images should have a stride that's even.
4445
# Not that ImageMagick or GIMP write PCX that way.
4546
# We were not handling properly.
46-
for mode in ("1", "L", "P", "RGB"):
47-
# larger, odd sized images are better here to ensure that
48-
# we handle interrupted scan lines properly.
49-
_roundtrip(tmp_path, hopper(mode).resize((511, 511)))
47+
# larger, odd sized images are better here to ensure that
48+
# we handle interrupted scan lines properly.
49+
_roundtrip(tmp_path, hopper(mode).resize((511, 511)))
5050

5151

5252
def test_odd_read():

Tests/test_file_pdf.py

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ def helper_save_as_pdf(tmp_path, mode, **kwargs):
3737
return outfile
3838

3939

40+
@pytest.mark.parametrize("mode", ("L", "P", "RGB", "CMYK"))
41+
def test_save(tmp_path, mode):
42+
helper_save_as_pdf(tmp_path, mode)
43+
44+
4045
@pytest.mark.valgrind_known_error(reason="Temporary skip")
4146
def test_monochrome(tmp_path):
4247
# Arrange
@@ -47,38 +52,6 @@ def test_monochrome(tmp_path):
4752
assert os.path.getsize(outfile) < (5000 if features.check("libtiff") else 15000)
4853

4954

50-
def test_greyscale(tmp_path):
51-
# Arrange
52-
mode = "L"
53-
54-
# Act / Assert
55-
helper_save_as_pdf(tmp_path, mode)
56-
57-
58-
def test_rgb(tmp_path):
59-
# Arrange
60-
mode = "RGB"
61-
62-
# Act / Assert
63-
helper_save_as_pdf(tmp_path, mode)
64-
65-
66-
def test_p_mode(tmp_path):
67-
# Arrange
68-
mode = "P"
69-
70-
# Act / Assert
71-
helper_save_as_pdf(tmp_path, mode)
72-
73-
74-
def test_cmyk_mode(tmp_path):
75-
# Arrange
76-
mode = "CMYK"
77-
78-
# Act / Assert
79-
helper_save_as_pdf(tmp_path, mode)
80-
81-
8255
def test_unsupported_mode(tmp_path):
8356
im = hopper("LA")
8457
outfile = str(tmp_path / "temp_LA.pdf")

0 commit comments

Comments
 (0)