From ce0fd6af59a9922421f00e95450e545270e65628 Mon Sep 17 00:00:00 2001 From: Gustavo RPS <516827+gustavorps@users.noreply.github.com> Date: Wed, 8 Oct 2025 19:59:45 -0300 Subject: [PATCH 1/4] Added report_path expansion method with support to string format --- src/pytest_html/basereport.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/pytest_html/basereport.py b/src/pytest_html/basereport.py index cfcc74b2..0fa4a425 100644 --- a/src/pytest_html/basereport.py +++ b/src/pytest_html/basereport.py @@ -1,7 +1,7 @@ # This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at http://mozilla.org/MPL/2.0/. -import datetime +from datetime import datetime, timezone import json import math import os @@ -20,8 +20,9 @@ class BaseReport: def __init__(self, report_path, config, report_data, template, css): + report_path_expanded = self._expand_path(report_path) self._report_path = ( - Path.cwd() / Path(os.path.expandvars(report_path)).expanduser() + Path.cwd() / Path(report_path_expanded).expanduser() ) self._report_path.parent.mkdir(parents=True, exist_ok=True) self._config = config @@ -32,6 +33,7 @@ def __init__(self, report_path, config, report_data, template, css): ) self._reports = defaultdict(dict) + self._generated = datetime.now(tz=timezone.utc) self._report = report_data self._report.title = self._report_path.name self._suite_start_time = time.time() @@ -40,7 +42,16 @@ def __init__(self, report_path, config, report_data, template, css): def css(self): # implement in subclasses return - + + def _expand_path(self, report_path): + # generated_time: UTC date and time, in ISO format with : replaced with -. + # report-%(generated_time).html will become report-2025-10-08T21-45-08.237134.html + path_expanded = os.path.expandvars(report_path) + path_expanded := path_expanded % { + "generated_time": self._generated.isoformat().replace(":", "-"), + } + return path_expanded + def _asset_filename(self, test_id, extra_index, test_index, file_extension): return "{}_{}_{}.{}".format( re.sub(r"[^\w.]", "_", test_id), @@ -50,13 +61,12 @@ def _asset_filename(self, test_id, extra_index, test_index, file_extension): )[-self._max_asset_filename_length :] def _generate_report(self, self_contained=False): - generated = datetime.datetime.now() test_data = self._report.data test_data = json.dumps(test_data) rendered_report = self._template.render( title=self._report.title, - date=generated.strftime("%d-%b-%Y"), - time=generated.strftime("%H:%M:%S"), + date=self._generated.strftime("%d-%b-%Y"), + time=self._generated.strftime("%H:%M:%S"), version=__version__, styles=self.css, run_count=self._run_count(), From 61dc723e15ec6370599d16a13880c45605ec9e3d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 8 Oct 2025 23:02:56 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/pytest_html/basereport.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/pytest_html/basereport.py b/src/pytest_html/basereport.py index 0fa4a425..78047bbe 100644 --- a/src/pytest_html/basereport.py +++ b/src/pytest_html/basereport.py @@ -1,7 +1,6 @@ # This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at http://mozilla.org/MPL/2.0/. -from datetime import datetime, timezone import json import math import os @@ -9,6 +8,8 @@ import time import warnings from collections import defaultdict +from datetime import datetime +from datetime import timezone from html import escape from pathlib import Path @@ -42,7 +43,7 @@ def __init__(self, report_path, config, report_data, template, css): def css(self): # implement in subclasses return - + def _expand_path(self, report_path): # generated_time: UTC date and time, in ISO format with : replaced with -. # report-%(generated_time).html will become report-2025-10-08T21-45-08.237134.html @@ -51,7 +52,7 @@ def _expand_path(self, report_path): "generated_time": self._generated.isoformat().replace(":", "-"), } return path_expanded - + def _asset_filename(self, test_id, extra_index, test_index, file_extension): return "{}_{}_{}.{}".format( re.sub(r"[^\w.]", "_", test_id), From c424245cb9ec994db3aa81aec20d0caa2e677a5e Mon Sep 17 00:00:00 2001 From: Gustavo RPS <516827+gustavorps@users.noreply.github.com> Date: Wed, 8 Oct 2025 20:05:36 -0300 Subject: [PATCH 3/4] fixed self._generated attribute order --- src/pytest_html/basereport.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/pytest_html/basereport.py b/src/pytest_html/basereport.py index 78047bbe..caf8825c 100644 --- a/src/pytest_html/basereport.py +++ b/src/pytest_html/basereport.py @@ -21,6 +21,8 @@ class BaseReport: def __init__(self, report_path, config, report_data, template, css): + self._generated = datetime.now(tz=timezone.utc) + report_path_expanded = self._expand_path(report_path) self._report_path = ( Path.cwd() / Path(report_path_expanded).expanduser() @@ -34,7 +36,6 @@ def __init__(self, report_path, config, report_data, template, css): ) self._reports = defaultdict(dict) - self._generated = datetime.now(tz=timezone.utc) self._report = report_data self._report.title = self._report_path.name self._suite_start_time = time.time() From 55d27cc950d24e686f28b354105bab2ef5332df1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 8 Oct 2025 23:07:29 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/pytest_html/basereport.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pytest_html/basereport.py b/src/pytest_html/basereport.py index caf8825c..1d9ef7f1 100644 --- a/src/pytest_html/basereport.py +++ b/src/pytest_html/basereport.py @@ -22,7 +22,7 @@ class BaseReport: def __init__(self, report_path, config, report_data, template, css): self._generated = datetime.now(tz=timezone.utc) - + report_path_expanded = self._expand_path(report_path) self._report_path = ( Path.cwd() / Path(report_path_expanded).expanduser()