diff --git a/src/pytest_html/basereport.py b/src/pytest_html/basereport.py index 9fa47d44..10a94165 100644 --- a/src/pytest_html/basereport.py +++ b/src/pytest_html/basereport.py @@ -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), @@ -123,6 +124,7 @@ def _render_html( time, version, styles, + run_count, self_contained, outcomes, test_data, @@ -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), @@ -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)) @@ -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": diff --git a/src/pytest_html/report_data.py b/src/pytest_html/report_data.py index 2524df80..d1e9183f 100644 --- a/src/pytest_html/report_data.py +++ b/src/pytest_html/report_data.py @@ -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, diff --git a/src/pytest_html/resources/index.jinja2 b/src/pytest_html/resources/index.jinja2 index 5b0b5b8e..29ef9b69 100644 --- a/src/pytest_html/resources/index.jinja2 +++ b/src/pytest_html/resources/index.jinja2 @@ -81,10 +81,10 @@ {%- for p in prefix %} {{ p|safe }} {%- endfor %} -

+

{{ run_count }}

(Un)check the boxes to filter the results.

-
+
There are still tests running.
Reload this page to ge the latest results!
diff --git a/src/pytest_html/scripts/main.js b/src/pytest_html/scripts/main.js index 46fec6a2..6e66c72a 100644 --- a/src/pytest_html/scripts/main.js +++ b/src/pytest_html/scripts/main.js @@ -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 = () => {