From 590ea17e134b1ec6e55879709aebcb36002faff7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jim=20Br=C3=A4nnlund?= Date: Sun, 10 Dec 2023 16:19:17 +0100 Subject: [PATCH] fix: Extras from setup/teardown missing in report --- src/pytest_html/basereport.py | 18 ++++++++++++++---- testing/test_integration.py | 16 ++++++++-------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/pytest_html/basereport.py b/src/pytest_html/basereport.py index f3aa6455..4f02763c 100644 --- a/src/pytest_html/basereport.py +++ b/src/pytest_html/basereport.py @@ -196,7 +196,7 @@ def pytest_terminal_summary(self, terminalreporter): @pytest.hookimpl(trylast=True) def pytest_collectreport(self, report): if report.failed: - self._process_report(report, 0) + self._process_report(report, 0, []) @pytest.hookimpl(trylast=True) def pytest_collection_finish(self, session): @@ -238,16 +238,25 @@ def pytest_runtest_logreport(self, report): if outcome != "rerun": test_duration += reports[0].duration + processed_extras = [] + for key, reports in self._reports[report.nodeid].items(): + when, _ = key + for each in reports: + test_id = report.nodeid + if when != "call": + test_id += f"::{when}" + processed_extras += self._process_extras(each, test_id) + for key, reports in self._reports[report.nodeid].items(): when, _ = key for each in reports: dur = test_duration if when == "call" else each.duration - self._process_report(each, dur) + self._process_report(each, dur, processed_extras) if self._config.getini("generate_report_on_test"): self._generate_report() - def _process_report(self, report, duration): + def _process_report(self, report, duration, processed_extras): outcome = _process_outcome(report) try: # hook returns as list for some reason @@ -262,8 +271,9 @@ def _process_report(self, report, duration): test_id += f"::{report.when}" data = { - "extras": self._process_extras(report, test_id), + "extras": processed_extras, # self._process_extras(report, test_id), } + print(processed_extras) links = [ extra for extra in data["extras"] diff --git a/testing/test_integration.py b/testing/test_integration.py index 145dd34b..c1fa4ed8 100644 --- a/testing/test_integration.py +++ b/testing/test_integration.py @@ -518,26 +518,26 @@ def pytest_runtest_makereport(item, call): ) def test_extra_url(self, pytester): - content = str(random.random()) pytester.makeconftest( - f""" + """ import pytest @pytest.hookimpl(hookwrapper=True) def pytest_runtest_makereport(item, call): outcome = yield report = outcome.get_result() - if report.when == 'call': - from pytest_html import extras - report.extras = [extras.url('{content}')] + from pytest_html import extras + report.extras = [extras.url(f'{report.when}')] """ ) pytester.makepyfile("def test_pass(): pass") page = run(pytester) - element = page.select_one("a[class='col-links__extra url']") - assert_that(element.string).is_equal_to("URL") - assert_that(element["href"]).is_equal_to(content) + elements = page.select("a[class='col-links__extra url']") + for each in zip(elements, ["setup", "call", "teardown"]): + element, when = each + assert_that(element.string).is_equal_to("URL") + assert_that(element["href"]).is_equal_to(when) @pytest.mark.parametrize( "mime_type, extension",