added branch coverage calculations

This commit is contained in:
isidorajovanovic 2024-06-12 12:08:25 +02:00
parent 0a45381c2b
commit 42abc817a8
3 changed files with 30 additions and 1 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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")