diff --git a/src/pytest_html/result.py b/src/pytest_html/result.py index f791e6d7..c6e6e602 100644 --- a/src/pytest_html/result.py +++ b/src/pytest_html/result.py @@ -40,7 +40,6 @@ def __init__(self, outcome, report, logfile, config): self.append_log_html( report, self.additional_html, - config.option.capture, config.option.showcapture, ) @@ -175,7 +174,7 @@ def _format_time(self, report): duration_as_gmtime = time.gmtime(report.duration) return time.strftime(duration_formatter, duration_as_gmtime) - def _populate_html_log_div(self, log, report): + def _populate_html_log_div(self, log, report, show_capture): if report.longrepr: # longreprtext is only filled out on failure by pytest # otherwise will be None. @@ -194,8 +193,13 @@ def _populate_html_log_div(self, log, report): log.append(raw(escape(line))) log.append(html.br()) + if show_capture == "no": + return + for section in report.sections: header, content = map(escape, section) + if show_capture != "all" and show_capture not in header: + continue log.append(f" {header:-^80} ") log.append(html.br()) @@ -214,16 +218,11 @@ def append_log_html( self, report, additional_html, - pytest_capture_value, pytest_show_capture_value, ): log = html.div(class_="log") - should_skip_captured_output = pytest_capture_value == "no" - if report.outcome == "failed" and not should_skip_captured_output: - should_skip_captured_output = pytest_show_capture_value == "no" - if not should_skip_captured_output: - self._populate_html_log_div(log, report) + self._populate_html_log_div(log, report, pytest_show_capture_value) if len(log) == 0: log = html.div(class_="empty log") diff --git a/testing/test_pytest_html.py b/testing/test_pytest_html.py index 69341caf..5cc9a1a4 100644 --- a/testing/test_pytest_html.py +++ b/testing/test_pytest_html.py @@ -1152,27 +1152,28 @@ def test_setup(teardown): assert "this is the test case" in html @pytest.mark.parametrize( - "capture_flag, should_capture", - [("-s", False), ("--capture=no", False), ("--capture=sys", True)], + "show_capture_flag, should_capture", + [("--show-capture=no", False), ("--show-capture=all", True)], ) - def test_extra_log_reporting_respects_capture_no( - self, testdir, capture_flag, should_capture + def test_extra_log_reporting_respects_show_capture_no( + self, testdir, show_capture_flag, should_capture ): testdir.makepyfile( """ import sys - def test_capture_no(): + def test_show_capture_no(): print("stdout print line") print("stderr print line", file=sys.stderr) + assert False """ ) - result, html = run(testdir, "report.html", capture_flag) - assert result.ret == 0 - assert_results(html) + result, html = run(testdir, "report.html", show_capture_flag) + assert result.ret == 1 + assert_results(html, passed=0, failed=1) extra_log_div_regex = re.compile( - '