diff --git a/Tests/test_file_jpeg2k.py b/Tests/test_file_jpeg2k.py index 5a208739f..c152f148c 100644 --- a/Tests/test_file_jpeg2k.py +++ b/Tests/test_file_jpeg2k.py @@ -5,6 +5,7 @@ import re from io import BytesIO from pathlib import Path from typing import Any +from conftest import branch_coverage import pytest @@ -36,7 +37,6 @@ test_card.load() # ignore it---it doesn't represent a test failure. # 'Not enough memory to handle tile data' - def roundtrip(im: Image.Image, **options: Any) -> Image.Image: out = BytesIO() im.save(out, "JPEG2000", **options) @@ -447,12 +447,15 @@ def test_plt_marker() -> None: jp2_boxid = _binary.i16be(marker) if jp2_boxid == 0xFF4F: + branch_coverage["1"] = True # SOC has no length continue elif jp2_boxid == 0xFF58: + branch_coverage["2"] = True # PLT return elif jp2_boxid == 0xFF93: + branch_coverage["3"] = True pytest.fail("SOD without finding PLT first") hdr = out.read(2) @@ -464,3 +467,4 @@ def test_9bit(): with Image.open("Tests/images/9bit.j2k") as im: assert im.mode == "I;16" assert im.size == (128, 128) + diff --git a/Tests/test_image_rotate.py b/Tests/test_image_rotate.py index 252a15db7..65ec9af54 100644 --- a/Tests/test_image_rotate.py +++ b/Tests/test_image_rotate.py @@ -3,6 +3,7 @@ from __future__ import annotations import pytest from PIL import Image +from conftest import branch_coverage from .helper import ( assert_image_equal, @@ -25,10 +26,13 @@ def rotate( out = im.rotate(angle, center=center, translate=translate, expand=1) assert out.mode == mode if angle % 180 == 0: + branch_coverage["1"] = True assert out.size == im.size elif im.size == (0, 0): + branch_coverage["2"] = True assert out.size == im.size else: + branch_coverage["3"] = True assert out.size != im.size @@ -154,3 +158,4 @@ def test_alpha_rotate_with_fill() -> None: im = im.rotate(45, expand=1, fillcolor=(255, 0, 0, 255)) corner = im.getpixel((0, 0)) assert corner == (255, 0, 0, 255) + diff --git a/conftest.py b/conftest.py index 4dcd5e053..74b5d4296 100644 --- a/conftest.py +++ b/conftest.py @@ -1,3 +1,23 @@ from __future__ import annotations +import pytest pytest_plugins = ["Tests.helper"] + +branch_coverage1 = { + "1": False, + "2": False, + "3": False, +} + +def calculate_coverage(): + num_branches = len(branch_coverage1) + covered_branches = sum(value is True for value in branch_coverage1.values()) + + coverage = (covered_branches/num_branches) * 100 + + return coverage + +@pytest.hookimpl(tryfirst=True) +def pytest_sessionfinish(session, exitstatus): + coverage = calculate_coverage() + print("\nBRANCH COVERAGE:", coverage, "%\n")