fix: Extras from setup/teardown missing in report (#784)

This commit is contained in:
Jim Brännlund 2023-12-10 16:47:03 +01:00 committed by GitHub
parent cfd32d0848
commit cd0ed431e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 12 deletions

View File

@ -196,7 +196,7 @@ class BaseReport:
@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 @@ class BaseReport:
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 @@ class BaseReport:
test_id += f"::{report.when}"
data = {
"extras": self._process_extras(report, test_id),
"extras": processed_extras,
}
links = [
extra
for extra in data["extras"]

View File

@ -518,26 +518,27 @@ class TestHTML:
)
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']")
assert_that(elements).is_length(3)
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",