Skip to content

Commit 1359f1a

Browse files
committed
Fix: Logging issues with teardown
1 parent f6f623d commit 1359f1a

File tree

3 files changed

+55
-41
lines changed

3 files changed

+55
-41
lines changed

src/pytest_html/basereport.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -248,15 +248,16 @@ def _process_logs(report):
248248
log = []
249249
if report.longreprtext:
250250
log.append(report.longreprtext.replace("<", "&lt;").replace(">", "&gt;") + "\n")
251-
for section in report.sections:
252-
header, content = section
253-
log.append(f"{' ' + header + ' ':-^80}\n{content}")
254-
255-
# weird formatting related to logs
256-
if "log" in header:
257-
log.append("")
258-
if "call" in header:
251+
if report.outcome != "rerun":
252+
for section in report.sections:
253+
header, content = section
254+
log.append(f"{' ' + header + ' ':-^80}\n{content}")
255+
256+
# weird formatting related to logs
257+
if "log" in header:
259258
log.append("")
259+
if "call" in header:
260+
log.append("")
260261
if not log:
261262
log.append("No log output captured.")
262263
return log

src/pytest_html/report_data.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def set_data(self, key, value):
6767
def add_test(self, test_data, report, logs):
6868
# regardless of pass or fail we must add teardown logging to "call"
6969
if report.when == "teardown":
70-
self.update_test_log(report)
70+
self.append_teardown_log(report)
7171

7272
# passed "setup" and "teardown" are not added to the html
7373
if report.when == "call" or (
@@ -79,12 +79,13 @@ def add_test(self, test_data, report, logs):
7979

8080
return False
8181

82-
def update_test_log(self, report):
82+
def append_teardown_log(self, report):
8383
log = []
84-
for test in self._data["tests"][report.nodeid]:
85-
if test["testId"] == report.nodeid and "log" in test:
86-
for section in report.sections:
87-
header, content = section
88-
if "teardown" in header:
89-
log.append(f"{' ' + header + ' ':-^80}\n{content}")
90-
test["log"] += _handle_ansi("\n".join(log))
84+
if self._data["tests"][report.nodeid]:
85+
# Last index is "call"
86+
test = self._data["tests"][report.nodeid][-1]
87+
for section in report.sections:
88+
header, content = section
89+
if "teardown" in header:
90+
log.append(f"{' ' + header + ' ':-^80}\n{content}")
91+
test["log"] += _handle_ansi("\n".join(log))

testing/test_integration.py

+36-24
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ def test_pass(): pass
684684
685685
"""
686686
)
687-
page = run(pytester)
687+
page = run(pytester, cmd_flags=["-s"])
688688
assert_results(page, passed=1)
689689

690690
def test_results_table_hook_pop(self, pytester):
@@ -759,25 +759,29 @@ def log_cli(self, pytester):
759759

760760
@pytest.fixture
761761
def test_file(self):
762-
return """
763-
import pytest
764-
import logging
765-
@pytest.fixture
766-
def setup():
767-
logging.info("this is setup")
768-
{setup}
769-
yield
770-
logging.info("this is teardown")
771-
{teardown}
762+
def formatter(assertion, setup="", teardown="", flaky=""):
763+
return f"""
764+
import pytest
765+
import logging
766+
@pytest.fixture
767+
def setup():
768+
logging.info("this is setup")
769+
{setup}
770+
yield
771+
logging.info("this is teardown")
772+
{teardown}
773+
774+
{flaky}
775+
def test_logging(setup):
776+
logging.info("this is test")
777+
assert {assertion}
778+
"""
772779

773-
def test_logging(setup):
774-
logging.info("this is test")
775-
assert {assertion}
776-
"""
780+
return formatter
777781

778782
@pytest.mark.usefixtures("log_cli")
779783
def test_all_pass(self, test_file, pytester):
780-
pytester.makepyfile(test_file.format(setup="", teardown="", assertion=True))
784+
pytester.makepyfile(test_file(assertion=True))
781785
page = run(pytester)
782786
assert_results(page, passed=1)
783787

@@ -787,9 +791,7 @@ def test_all_pass(self, test_file, pytester):
787791

788792
@pytest.mark.usefixtures("log_cli")
789793
def test_setup_error(self, test_file, pytester):
790-
pytester.makepyfile(
791-
test_file.format(setup="error", teardown="", assertion=True)
792-
)
794+
pytester.makepyfile(test_file(assertion=True, setup="error"))
793795
page = run(pytester)
794796
assert_results(page, error=1)
795797

@@ -800,7 +802,7 @@ def test_setup_error(self, test_file, pytester):
800802

801803
@pytest.mark.usefixtures("log_cli")
802804
def test_test_fails(self, test_file, pytester):
803-
pytester.makepyfile(test_file.format(setup="", teardown="", assertion=False))
805+
pytester.makepyfile(test_file(assertion=False))
804806
page = run(pytester)
805807
assert_results(page, failed=1)
806808

@@ -813,9 +815,7 @@ def test_test_fails(self, test_file, pytester):
813815
"assertion, result", [(True, {"passed": 1}), (False, {"failed": 1})]
814816
)
815817
def test_teardown_error(self, test_file, pytester, assertion, result):
816-
pytester.makepyfile(
817-
test_file.format(setup="", teardown="error", assertion=assertion)
818-
)
818+
pytester.makepyfile(test_file(assertion=assertion, teardown="error"))
819819
page = run(pytester)
820820
assert_results(page, error=1, **result)
821821

@@ -825,7 +825,7 @@ def test_teardown_error(self, test_file, pytester, assertion, result):
825825
assert_that(log).matches(self.LOG_LINE_REGEX.format(when))
826826

827827
def test_no_log(self, test_file, pytester):
828-
pytester.makepyfile(test_file.format(setup="", teardown="", assertion=True))
828+
pytester.makepyfile(test_file(assertion=True))
829829
page = run(pytester)
830830
assert_results(page, passed=1)
831831

@@ -834,6 +834,18 @@ def test_no_log(self, test_file, pytester):
834834
for when in ["setup", "test", "teardown"]:
835835
assert_that(log).does_not_match(self.LOG_LINE_REGEX.format(when))
836836

837+
@pytest.mark.usefixtures("log_cli")
838+
def test_rerun(self, test_file, pytester):
839+
pytester.makepyfile(
840+
test_file(assertion=False, flaky="@pytest.mark.flaky(reruns=2)")
841+
)
842+
page = run(pytester, query_params={"visible": "failed"})
843+
assert_results(page, failed=1, rerun=2)
844+
845+
log = get_log(page)
846+
assert_that(log.count("Captured log setup")).is_equal_to(3)
847+
assert_that(log.count("Captured log teardown")).is_equal_to(5)
848+
837849

838850
class TestCollapsedQueryParam:
839851
@pytest.fixture

0 commit comments

Comments
 (0)