From f39a838b806127fd31199be4541651c1cbb6148f Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Mon, 22 Jun 2026 13:01:17 +0300 Subject: [PATCH 1/4] Use pytest.approx in tests --- Tests/test_file_gimpgradient.py | 4 +++- Tests/test_file_libtiff.py | 2 +- Tests/test_file_tiff_metadata.py | 4 ++-- Tests/test_image_entropy.py | 20 +++++++++++--------- Tests/test_pdfparser.py | 2 +- 5 files changed, 18 insertions(+), 14 deletions(-) diff --git a/Tests/test_file_gimpgradient.py b/Tests/test_file_gimpgradient.py index 006ee952d43..6e032df4021 100644 --- a/Tests/test_file_gimpgradient.py +++ b/Tests/test_file_gimpgradient.py @@ -1,5 +1,7 @@ from __future__ import annotations +import pytest + from PIL import GimpGradientFile, ImagePalette @@ -84,7 +86,7 @@ def test_sphere_increasing() -> None: ret = GimpGradientFile.sphere_increasing(middle, pos) # Assert - assert round(abs(ret - 0.9682458365518543), 7) == 0 + assert ret == pytest.approx(0.9682458365518543) def test_sphere_decreasing() -> None: diff --git a/Tests/test_file_libtiff.py b/Tests/test_file_libtiff.py index c9849a056a3..450794179d8 100644 --- a/Tests/test_file_libtiff.py +++ b/Tests/test_file_libtiff.py @@ -321,7 +321,7 @@ def check_tags( and libtiff ): # libtiff does not support real RATIONALS - assert round(abs(float(reloaded_value) - float(value)), 7) == 0 + assert float(reloaded_value) == pytest.approx(float(value)) continue assert reloaded_value == value diff --git a/Tests/test_file_tiff_metadata.py b/Tests/test_file_tiff_metadata.py index a9a367f88c6..3decb74a5eb 100644 --- a/Tests/test_file_tiff_metadata.py +++ b/Tests/test_file_tiff_metadata.py @@ -72,9 +72,9 @@ def test_rt_metadata(tmp_path: Path) -> None: assert loaded.tag_v2[ImageDescription] == reloaded_text_data loaded_float = loaded.tag[TAG_IDS["RollAngle"]][0] - assert round(abs(loaded_float - float_data), 5) == 0 + assert loaded_float == pytest.approx(float_data) loaded_double = loaded.tag[TAG_IDS["YawAngle"]][0] - assert round(abs(loaded_double - double_data), 7) == 0 + assert loaded_double == pytest.approx(double_data) # check with 2 element ImageJMetaDataByteCounts, issue #2006 diff --git a/Tests/test_image_entropy.py b/Tests/test_image_entropy.py index c1dbb879b0b..f6beb592c81 100644 --- a/Tests/test_image_entropy.py +++ b/Tests/test_image_entropy.py @@ -1,5 +1,7 @@ from __future__ import annotations +import pytest + from .helper import hopper @@ -7,12 +9,12 @@ def test_entropy() -> None: def entropy(mode: str) -> float: return hopper(mode).entropy() - assert round(abs(entropy("1") - 0.9138803254693582), 7) == 0 - assert round(abs(entropy("L") - 7.063008716585465), 7) == 0 - assert round(abs(entropy("I") - 7.063008716585465), 7) == 0 - assert round(abs(entropy("F") - 7.063008716585465), 7) == 0 - assert round(abs(entropy("P") - 5.082506854662517), 7) == 0 - assert round(abs(entropy("RGB") - 8.821286587714319), 7) == 0 - assert round(abs(entropy("RGBA") - 7.42724306524488), 7) == 0 - assert round(abs(entropy("CMYK") - 7.4272430652448795), 7) == 0 - assert round(abs(entropy("YCbCr") - 7.698360534903628), 7) == 0 + assert entropy("1") == pytest.approx(0.9138803254693582) + assert entropy("L") == pytest.approx(7.063008716585465) + assert entropy("I") == pytest.approx(7.063008716585465) + assert entropy("F") == pytest.approx(7.063008716585465) + assert entropy("P") == pytest.approx(5.082506854662517) + assert entropy("RGB") == pytest.approx(8.821286587714319) + assert entropy("RGBA") == pytest.approx(7.42724306524488) + assert entropy("CMYK") == pytest.approx(7.4272430652448795) + assert entropy("YCbCr") == pytest.approx(7.698360534903628) diff --git a/Tests/test_pdfparser.py b/Tests/test_pdfparser.py index 4c522eb7a50..5f89205f725 100644 --- a/Tests/test_pdfparser.py +++ b/Tests/test_pdfparser.py @@ -62,7 +62,7 @@ def test_parsing() -> None: assert PdfParser.get_value(b"(\\53a)", 0) == (b"\x2ba", 6) assert PdfParser.get_value(b"(\\1111)", 0) == (b"\x491", 7) assert PdfParser.get_value(b" 123 (", 0) == (123, 4) - assert round(abs(PdfParser.get_value(b" 123.4 %", 0)[0] - 123.4), 7) == 0 + assert PdfParser.get_value(b" 123.4 %", 0)[0] == pytest.approx(123.4) assert PdfParser.get_value(b" 123.4 %", 0)[1] == 6 with pytest.raises(PdfFormatError): PdfParser.get_value(b"]", 0) From 03a39e6d06e9d694bc6db824a28d30e473fe41f7 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Mon, 22 Jun 2026 14:16:48 +0300 Subject: [PATCH 2/4] Use pytest.raises in a test --- Tests/test_file_gif.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Tests/test_file_gif.py b/Tests/test_file_gif.py index 52997e694c1..d5746992933 100644 --- a/Tests/test_file_gif.py +++ b/Tests/test_file_gif.py @@ -422,12 +422,11 @@ def test_seek() -> None: with Image.open("Tests/images/dispose_none.gif") as img: assert isinstance(img, GifImagePlugin.GifImageFile) frame_count = 0 - try: + with pytest.raises(EOFError): while True: frame_count += 1 img.seek(img.tell() + 1) - except EOFError: - assert frame_count == 5 + assert frame_count == 5 img.seek(0) with pytest.raises(ValueError, match="cannot seek to frame 2"): From d7449ecbb3dc78cfb5cce8a17ac90c415085483f Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Thu, 25 Jun 2026 15:22:10 +0300 Subject: [PATCH 3/4] Skip test rather than passing when ImageMagick not available --- Tests/test_file_palm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/test_file_palm.py b/Tests/test_file_palm.py index 58208ba99fa..bc6debeffe7 100644 --- a/Tests/test_file_palm.py +++ b/Tests/test_file_palm.py @@ -36,7 +36,7 @@ def open_with_magick(magick: list[str], tmp_path: Path, f: str) -> Image.Image: def roundtrip(tmp_path: Path, mode: str) -> None: magick = magick_command() if not magick: - return + pytest.skip("ImageMagick not available") im = hopper(mode) outfile = str(tmp_path / "temp.palm") From c85f522e3b3f6c6c7a97060ec37451d16a20f1f3 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Tue, 4 Aug 2026 08:25:29 +0300 Subject: [PATCH 4/4] Remove redundant casts to float Co-authored-by: Andrew Murray <3112309+radarhere@users.noreply.github.com> --- Tests/test_file_libtiff.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/test_file_libtiff.py b/Tests/test_file_libtiff.py index 450794179d8..d6d7be9c898 100644 --- a/Tests/test_file_libtiff.py +++ b/Tests/test_file_libtiff.py @@ -321,7 +321,7 @@ def check_tags( and libtiff ): # libtiff does not support real RATIONALS - assert float(reloaded_value) == pytest.approx(float(value)) + assert reloaded_value == pytest.approx(value) continue assert reloaded_value == value