Skip to content

Commit f740846

Browse files
authored
Feature: Template test and duration summary (#698)
1 parent 2ddef71 commit f740846

File tree

4 files changed

+25
-21
lines changed

4 files changed

+25
-21
lines changed

src/pytest_html/basereport.py

Lines changed: 22 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,24 @@ 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+
data = self._report.data
157+
relevant_outcomes = ["passed", "failed", "xpassed", "xfailed"]
158+
counts = 0
159+
for outcome in data["outcomes"].keys():
160+
if outcome in relevant_outcomes:
161+
counts += data["outcomes"][outcome]["value"]
162+
163+
plural = counts > 1
164+
duration = _format_duration(data["totalDuration"])
165+
166+
if data["runningState"].lower() == "finished":
167+
return f"{counts} {'tests' if plural else 'test'} took {duration}."
168+
169+
return (
170+
f"{counts}/{data['collectedItems']} {'tests' if plural else 'test'} done."
171+
)
172+
152173
@pytest.hookimpl(trylast=True)
153174
def pytest_sessionstart(self, session):
154175
self._report.set_data("environment", self._generate_environment(metadata_key))
@@ -197,10 +218,7 @@ def pytest_runtest_logreport(self, report):
197218
"result": outcome,
198219
"duration": _format_duration(report.duration),
199220
}
200-
201-
total_duration = self._report.data["totalDuration"]
202-
total_duration["total"] += report.duration
203-
total_duration["formatted"] = _format_duration(total_duration["total"])
221+
self._report.data["totalDuration"] += report.duration
204222

205223
test_id = report.nodeid
206224
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)