Skip to content

Feature: Template test and duration summary #698

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
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
26 changes: 22 additions & 4 deletions src/pytest_html/basereport.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def _generate_report(self, self_contained=False):
generated.strftime("%H:%M:%S"),
__version__,
self.css,
run_count=self._run_count(),
self_contained=self_contained,
outcomes=self._report.data["outcomes"],
test_data=cleanup_unserializable(self._report.data),
Expand Down Expand Up @@ -123,6 +124,7 @@ def _render_html(
time,
version,
styles,
run_count,
self_contained,
outcomes,
test_data,
Expand All @@ -136,6 +138,7 @@ def _render_html(
time=time,
version=version,
styles=styles,
run_count=run_count,
self_contained=self_contained,
outcomes=outcomes,
test_data=json.dumps(test_data),
Expand All @@ -149,6 +152,24 @@ def _write_report(self, rendered_report):
with self._report_path.open("w", encoding="utf-8") as f:
f.write(rendered_report)

def _run_count(self):
data = self._report.data
relevant_outcomes = ["passed", "failed", "xpassed", "xfailed"]
counts = 0
for outcome in data["outcomes"].keys():
if outcome in relevant_outcomes:
counts += data["outcomes"][outcome]["value"]

plural = counts > 1
duration = _format_duration(data["totalDuration"])

if data["runningState"].lower() == "finished":
return f"{counts} {'tests' if plural else 'test'} took {duration}."

return (
f"{counts}/{data['collectedItems']} {'tests' if plural else 'test'} done."
)

@pytest.hookimpl(trylast=True)
def pytest_sessionstart(self, session):
self._report.set_data("environment", self._generate_environment(metadata_key))
Expand Down Expand Up @@ -197,10 +218,7 @@ def pytest_runtest_logreport(self, report):
"result": outcome,
"duration": _format_duration(report.duration),
}

total_duration = self._report.data["totalDuration"]
total_duration["total"] += report.duration
total_duration["formatted"] = _format_duration(total_duration["total"])
self._report.data["totalDuration"] += report.duration

test_id = report.nodeid
if report.when != "call":
Expand Down
5 changes: 1 addition & 4 deletions src/pytest_html/report_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ def __init__(self, config):
self._data = {
"title": "",
"collectedItems": 0,
"totalDuration": {
"total": 0,
"formatted": "",
},
"totalDuration": 0,
"runningState": "not_started",
"environment": {},
"outcomes": outcomes,
Expand Down
4 changes: 2 additions & 2 deletions src/pytest_html/resources/index.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@
{%- for p in prefix %}
{{ p|safe }}
{%- endfor %}
<p class="run-count"></p>
<p class="run-count">{{ run_count }}</p>
<p class="filter">(Un)check the boxes to filter the results.</p>
<div class="summary__reload">
<div class="summary__reload__button" onclick="location.reload()">
<div class="summary__reload__button {{'hidden' if 'took' in run_count}}" onclick="location.reload()">
<div>There are still tests running. <br />Reload this page to ge the latest results!</div>
</div>
</div>
Expand Down
11 changes: 0 additions & 11 deletions src/pytest_html/scripts/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,6 @@ const renderDerived = (tests, collectedItems, isFinished, formattedDuration) =>
const input = document.querySelector(`input[data-test-result="${result}"]`)
input.checked = currentFilter.includes(result)
})

const numberOfTests = tests.filter(({ result }) =>
['Passed', 'Failed', 'XPassed', 'XFailed'].includes(result)).length

if (isFinished) {
const testWord = numberOfTests > 1 ? 'tests' : 'test'
document.querySelector('.run-count').innerText = `${numberOfTests} ${testWord} took ${formattedDuration}.`
document.querySelector('.summary__reload__button').classList.add('hidden')
} else {
document.querySelector('.run-count').innerText = `${numberOfTests} / ${collectedItems} tests done`
}
}

const bindEvents = () => {
Expand Down