Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions src/pytest_html/basereport.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# 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
import json
import math
import os
import re
import time
import warnings
from collections import defaultdict
from datetime import datetime
from datetime import timezone
from html import escape
from pathlib import Path

Expand All @@ -20,8 +21,11 @@

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(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
Expand All @@ -41,6 +45,15 @@ 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),
Expand All @@ -50,13 +63,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(),
Expand Down