From 1c713c472cd9470d77dabcb2657710f976d486ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=81=E4=BA=8C=E6=9C=88=E4=BD=9C=E6=9B=B2=E5=AE=B6?= Date: Thu, 18 Jun 2026 23:00:18 +0800 Subject: [PATCH] fix: make non-base64 media paths relative to report location When extra media content is a file path (not base64), it was previously used as-is in the HTML, creating absolute paths that break when the report is moved. Now resolves relative to the report's parent directory. --- src/pytest_html/report.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/pytest_html/report.py b/src/pytest_html/report.py index 22ef7e14..dfe211d2 100644 --- a/src/pytest_html/report.py +++ b/src/pytest_html/report.py @@ -32,8 +32,13 @@ def _media_content(self, content, asset_name, *args, **kwargs): media_data = base64.b64decode(content.encode("utf-8"), validate=True) return self._write_content(media_data, asset_name) except binascii.Error: - # if not base64 content, just return as it's a file or link - return content + # if not base64 content, try to make it relative to the report + try: + abs_path = Path(content).resolve() + return str(abs_path.relative_to(self._report_path.parent.resolve())) + except (ValueError, OSError): + # On different drives (Windows) or unresolvable, return as-is + return content def _write_content(self, content, asset_name): content_relative_path = Path(self._assets_path, asset_name)