Skip to content

Commit 243402e

Browse files
authored
Merge pull request #6634 from radarhere/parametrized_tests
2 parents 285f9cb + 1735872 commit 243402e

25 files changed

Lines changed: 350 additions & 534 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 & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,6 @@ def test_file_object(tmp_path):
124124
image1.save(fh, "EPS")
125125

126126

127-
@pytest.mark.skipif(not HAS_GHOSTSCRIPT, reason="Ghostscript not available")
128-
def test_iobase_object(tmp_path):
129-
# issue 479
130-
with Image.open(FILE1) as image1:
131-
with open(str(tmp_path / "temp_iobase.eps"), "wb") as fh:
132-
image1.save(fh, "EPS")
133-
134-
135127
@pytest.mark.skipif(not HAS_GHOSTSCRIPT, reason="Ghostscript not available")
136128
def test_bytesio_object():
137129
with open(FILE1, "rb") as f:
@@ -203,25 +195,23 @@ def test_render_scale2():
203195

204196

205197
@pytest.mark.skipif(not HAS_GHOSTSCRIPT, reason="Ghostscript not available")
206-
def test_resize():
207-
files = [FILE1, FILE2, "Tests/images/illu10_preview.eps"]
208-
for fn in files:
209-
with Image.open(fn) as im:
210-
new_size = (100, 100)
211-
im = im.resize(new_size)
212-
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
213204

214205

215206
@pytest.mark.skipif(not HAS_GHOSTSCRIPT, reason="Ghostscript not available")
216-
def test_thumbnail():
207+
@pytest.mark.parametrize("filename", (FILE1, FILE2))
208+
def test_thumbnail(filename):
217209
# Issue #619
218210
# Arrange
219-
files = [FILE1, FILE2]
220-
for fn in files:
221-
with Image.open(FILE1) as im:
222-
new_size = (100, 100)
223-
im.thumbnail(new_size)
224-
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)
225215

226216

227217
def test_read_binary_preview():
@@ -266,20 +256,19 @@ def _test_readline_file_psfile(test_string, ending):
266256
_test_readline_file_psfile(s, ending)
267257

268258

269-
def test_open_eps():
270-
# https://github.com/python-pillow/Pillow/issues/1104
271-
# Arrange
272-
FILES = [
259+
@pytest.mark.parametrize(
260+
"filename",
261+
(
273262
"Tests/images/illu10_no_preview.eps",
274263
"Tests/images/illu10_preview.eps",
275264
"Tests/images/illuCS6_no_preview.eps",
276265
"Tests/images/illuCS6_preview.eps",
277-
]
278-
279-
# Act / Assert
280-
for filename in FILES:
281-
with Image.open(filename) as img:
282-
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"
283272

284273

285274
@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 & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -509,20 +509,13 @@ 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):
513-
"""This test passes, but when running all tests causes a failure due
514-
to output on stderr from the error thrown by libtiff. We need to
515-
capture that but not now"""
516-
512+
@pytest.mark.parametrize("compression", ("tiff_ccitt", "group3", "group4"))
513+
def test_bw_compression_w_rgb(self, compression, tmp_path):
517514
im = hopper("RGB")
518515
out = str(tmp_path / "temp.tif")
519516

520517
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")
518+
im.save(out, compression=compression)
526519

527520
def test_fp_leak(self):
528521
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():

0 commit comments

Comments
 (0)