Skip to content

Commit ce0fd6a

Browse files
authored
Added report_path expansion method with support to string format
1 parent 75aba5e commit ce0fd6a

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

src/pytest_html/basereport.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# This Source Code Form is subject to the terms of the Mozilla Public
22
# License, v. 2.0. If a copy of the MPL was not distributed with this
33
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4-
import datetime
4+
from datetime import datetime, timezone
55
import json
66
import math
77
import os
@@ -20,8 +20,9 @@
2020

2121
class BaseReport:
2222
def __init__(self, report_path, config, report_data, template, css):
23+
report_path_expanded = self._expand_path(report_path)
2324
self._report_path = (
24-
Path.cwd() / Path(os.path.expandvars(report_path)).expanduser()
25+
Path.cwd() / Path(report_path_expanded).expanduser()
2526
)
2627
self._report_path.parent.mkdir(parents=True, exist_ok=True)
2728
self._config = config
@@ -32,6 +33,7 @@ def __init__(self, report_path, config, report_data, template, css):
3233
)
3334

3435
self._reports = defaultdict(dict)
36+
self._generated = datetime.now(tz=timezone.utc)
3537
self._report = report_data
3638
self._report.title = self._report_path.name
3739
self._suite_start_time = time.time()
@@ -40,7 +42,16 @@ def __init__(self, report_path, config, report_data, template, css):
4042
def css(self):
4143
# implement in subclasses
4244
return
43-
45+
46+
def _expand_path(self, report_path):
47+
# generated_time: UTC date and time, in ISO format with : replaced with -.
48+
# report-%(generated_time).html will become report-2025-10-08T21-45-08.237134.html
49+
path_expanded = os.path.expandvars(report_path)
50+
path_expanded := path_expanded % {
51+
"generated_time": self._generated.isoformat().replace(":", "-"),
52+
}
53+
return path_expanded
54+
4455
def _asset_filename(self, test_id, extra_index, test_index, file_extension):
4556
return "{}_{}_{}.{}".format(
4657
re.sub(r"[^\w.]", "_", test_id),
@@ -50,13 +61,12 @@ def _asset_filename(self, test_id, extra_index, test_index, file_extension):
5061
)[-self._max_asset_filename_length :]
5162

5263
def _generate_report(self, self_contained=False):
53-
generated = datetime.datetime.now()
5464
test_data = self._report.data
5565
test_data = json.dumps(test_data)
5666
rendered_report = self._template.render(
5767
title=self._report.title,
58-
date=generated.strftime("%d-%b-%Y"),
59-
time=generated.strftime("%H:%M:%S"),
68+
date=self._generated.strftime("%d-%b-%Y"),
69+
time=self._generated.strftime("%H:%M:%S"),
6070
version=__version__,
6171
styles=self.css,
6272
run_count=self._run_count(),

0 commit comments

Comments
 (0)