Skip to content

Commit 5d4d3f5

Browse files
committed
Feature: Template test and duration summary
1 parent 2ddef71 commit 5d4d3f5

File tree

4 files changed

+22
-21
lines changed

4 files changed

+22
-21
lines changed

src/pytest_html/basereport.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def _generate_report(self, self_contained=False):
5151
generated.strftime("%H:%M:%S"),
5252
__version__,
5353
self.css,
54+
run_count=self._run_count(),
5455
self_contained=self_contained,
5556
outcomes=self._report.data["outcomes"],
5657
test_data=cleanup_unserializable(self._report.data),
@@ -123,6 +124,7 @@ def _render_html(
123124
time,
124125
version,
125126
styles,
127+
run_count,
126128
self_contained,
127129
outcomes,
128130
test_data,
@@ -136,6 +138,7 @@ def _render_html(
136138
time=time,
137139
version=version,
138140
styles=styles,
141+
run_count=run_count,
139142
self_contained=self_contained,
140143
outcomes=outcomes,
141144
test_data=json.dumps(test_data),
@@ -149,6 +152,21 @@ def _write_report(self, rendered_report):
149152
with self._report_path.open("w", encoding="utf-8") as f:
150153
f.write(rendered_report)
151154

155+
def _run_count(self):
156+
relevant_outcomes = ["passed", "failed", "xpassed", "xfailed"]
157+
counts = 0
158+
for outcome in self._report.data["outcomes"].keys():
159+
if outcome in relevant_outcomes:
160+
counts += self._report.data["outcomes"][outcome]["value"]
161+
162+
plural = counts > 1
163+
duration = _format_duration(self._report.data["totalDuration"])
164+
165+
if self._report.data["runningState"].lower() == "finished":
166+
return f"{counts} {'tests' if plural else 'test'} took {duration}."
167+
168+
return f"{counts}/{self._report.data['collectedItems']} {'tests' if plural else 'test'} done."
169+
152170
@pytest.hookimpl(trylast=True)
153171
def pytest_sessionstart(self, session):
154172
self._report.set_data("environment", self._generate_environment(metadata_key))
@@ -197,10 +215,7 @@ def pytest_runtest_logreport(self, report):
197215
"result": outcome,
198216
"duration": _format_duration(report.duration),
199217
}
200-
201-
total_duration = self._report.data["totalDuration"]
202-
total_duration["total"] += report.duration
203-
total_duration["formatted"] = _format_duration(total_duration["total"])
218+
self._report.data["totalDuration"] += report.duration
204219

205220
test_id = report.nodeid
206221
if report.when != "call":

src/pytest_html/report_data.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@ def __init__(self, config):
3131
self._data = {
3232
"title": "",
3333
"collectedItems": 0,
34-
"totalDuration": {
35-
"total": 0,
36-
"formatted": "",
37-
},
34+
"totalDuration": 0,
3835
"runningState": "not_started",
3936
"environment": {},
4037
"outcomes": outcomes,

src/pytest_html/resources/index.jinja2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@
8181
{%- for p in prefix %}
8282
{{ p|safe }}
8383
{%- endfor %}
84-
<p class="run-count"></p>
84+
<p class="run-count">{{ run_count }}</p>
8585
<p class="filter">(Un)check the boxes to filter the results.</p>
8686
<div class="summary__reload">
87-
<div class="summary__reload__button" onclick="location.reload()">
87+
<div class="summary__reload__button {{'hidden' if 'took' in run_count}}" onclick="location.reload()">
8888
<div>There are still tests running. <br />Reload this page to ge the latest results!</div>
8989
</div>
9090
</div>

src/pytest_html/scripts/main.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,6 @@ const renderDerived = (tests, collectedItems, isFinished, formattedDuration) =>
7272
const input = document.querySelector(`input[data-test-result="${result}"]`)
7373
input.checked = currentFilter.includes(result)
7474
})
75-
76-
const numberOfTests = tests.filter(({ result }) =>
77-
['Passed', 'Failed', 'XPassed', 'XFailed'].includes(result)).length
78-
79-
if (isFinished) {
80-
const testWord = numberOfTests > 1 ? 'tests' : 'test'
81-
document.querySelector('.run-count').innerText = `${numberOfTests} ${testWord} took ${formattedDuration}.`
82-
document.querySelector('.summary__reload__button').classList.add('hidden')
83-
} else {
84-
document.querySelector('.run-count').innerText = `${numberOfTests} / ${collectedItems} tests done`
85-
}
8675
}
8776

8877
const bindEvents = () => {

0 commit comments

Comments
 (0)