pdf tests: Save temporary pdfs for failed test debugging

This commit is contained in:
Kara Engelhardt
2026-09-18 16:49:29 +02:00
parent 03bca84e00
commit 547de7aa26
2 changed files with 34 additions and 8 deletions
+23 -1
View File
@@ -21,6 +21,8 @@
#
import inspect
import os
import re
from pathlib import Path
import pytest
from django.core.cache import cache
@@ -28,7 +30,6 @@ from django.test import override_settings
from django.utils import translation
from django_scopes import scopes_disabled
from fakeredis import FakeRedisConnection
from hierarkey.proxy import dirty_cache_keys
from xdist.dsession import DSession
from pretix.testutils.mock import get_redis_connection
@@ -85,6 +86,7 @@ def reset_locale():
@pytest.fixture(autouse=True)
def reset_hierarkey_cache_state():
from hierarkey.proxy import dirty_cache_keys
dirty_cache_keys.set(set())
@@ -143,3 +145,23 @@ def set_lock_namespaces(request):
@pytest.fixture
def class_monkeypatch(request, monkeypatch):
request.cls.monkeypatch = monkeypatch
def pytest_addoption(parser):
parser.addoption(
'--pdf-dir', action='store', help="output dir for files generated by pdf tests", type=Path
)
parser.addoption(
'--pdf-tmp-keep', action='store_true', help="keep the temporary directory created for pdf tests"
)
@pytest.fixture
def pdf_dir(request, tmp_path_factory):
dirname = re.sub('[^a-zA-Z0-9]+', '_', request.node.name).strip("_")
if dir := request.config.getoption('--pdf-dir'):
pdf_dir: Path = (dir / dirname).absolute()
pdf_dir.mkdir(exist_ok=True, parents=True)
yield pdf_dir
else:
yield tmp_path_factory.mktemp(dirname)
+11 -7
View File
@@ -32,7 +32,7 @@
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under the License.
import tempfile
import logging
from datetime import timedelta
from decimal import Decimal
from io import BytesIO
@@ -129,10 +129,15 @@ def asset_path(name):
return Path(__file__).parent / "assets" / name
def compare_pdfs(inp_a: Path | bytes, inp_b: Path | bytes):
def compare_pdfs(pdf_dir: Path, inp_a: Path | bytes, inp_b: Path | bytes):
logging.info(f"Comparing pdfs, writing files to {pdf_dir}")
pdf_a = pypdfium2.PdfDocument(inp_a)
pdf_b = pypdfium2.PdfDocument(inp_b)
pdf_a.save(pdf_dir / "a.pdf")
pdf_a.save(pdf_dir / "b.pdf")
assert len(pdf_a) == len(pdf_b)
for i, (expected_page, output_page) in enumerate(zip(pdf_a, pdf_b)):
@@ -143,9 +148,8 @@ def compare_pdfs(inp_a: Path | bytes, inp_b: Path | bytes):
diff = ImageChops.difference(expected_render.to_pil(), output_render.to_pil())
if diff.getbbox():
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as f:
diff.save(f)
assert not diff.getbbox(), f"Page {i} differs. Diff was written to {f.name}"
diff.save(pdf_dir / f"{i}.png")
assert not diff.getbbox(), f"Page {i} differs."
@pytest.mark.django_db
@@ -154,7 +158,7 @@ def compare_pdfs(inp_a: Path | bytes, inp_b: Path | bytes):
"bg-mediabox-offset",
"bg-cropbox"
])
def test_generate_pdf_weird_bgs(env, case):
def test_generate_pdf_weird_bgs(pdf_dir, env, case):
event, order, shirt = env
asset_folder = asset_path(case)
with open(asset_folder / "bg.pdf", 'rb') as fi:
@@ -168,4 +172,4 @@ def test_generate_pdf_weird_bgs(env, case):
assert ftype == 'application/pdf'
assert buf
compare_pdfs(buf, asset_folder / "expected.pdf")
compare_pdfs(pdf_dir, buf, asset_folder / "expected.pdf")