Skip to content

Commit

Permalink
fix: Extras from setup/teardown missing in report (#784)
Browse files Browse the repository at this point in the history
  • Loading branch information
BeyondEvil committed Dec 10, 2023
1 parent cfd32d0 commit cd0ed43
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
18 changes: 14 additions & 4 deletions src/pytest_html/basereport.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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,
}

links = [
extra
for extra in data["extras"]
Expand Down
17 changes: 9 additions & 8 deletions testing/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,26 +518,27 @@ 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']")
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",
Expand Down

0 comments on commit cd0ed43

Please sign in to comment.