From 6878166c2b8d493aa6557cd118027697f73337ea Mon Sep 17 00:00:00 2001 From: Shan Wenbin Date: Tue, 16 Mar 2021 21:19:08 +0100 Subject: [PATCH] use --show-capture as filter --- src/pytest_html/result.py | 15 +++++----- testing/test_pytest_html.py | 56 ++++++++++++++++++++++--------------- 2 files changed, 41 insertions(+), 30 deletions(-) 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( - '
-+Captured stdout call-+
stdout print line\n
' + '
.*-+Captured stdout call-+
stdout print line\n
' "-+Captured stderr call-+
stderr print line\n
" ) if should_capture: @@ -1181,33 +1182,44 @@ def test_capture_no(): assert extra_log_div_regex.search(html) is None @pytest.mark.parametrize( - "show_capture_flag, should_capture", - [("--show-capture=no", False), ("--show-capture=all", True)], + "show_capture_flag, html_log", + [ + ("--show-capture=no", []), + ("--show-capture=all", ["stdout", "stderr", "log"]), + ("--show-capture=stdout", ["stdout"]), + ("--show-capture=stderr", ["stderr"]), + ("--show-capture=log", ["log"]), + ], ) - def test_extra_log_reporting_respects_show_capture_no( - self, testdir, show_capture_flag, should_capture + def test_extra_log_reporting_respects_show_capture( + self, testdir, show_capture_flag, html_log ): testdir.makepyfile( """ import sys - def test_show_capture_no(): + import logging + def test_show_capture(): print("stdout print line") print("stderr print line", file=sys.stderr) + logging.warning("log print line") assert False """ ) result, html = run(testdir, "report.html", show_capture_flag) assert result.ret == 1 - assert_results(html, passed=0, failed=1) + assert_results(html, failed=1, passed=0) - extra_log_div_regex = re.compile( - '
.*-+Captured stdout call-+
stdout print line\n
' - "-+Captured stderr call-+
stderr print line\n
" - ) - if should_capture: - assert extra_log_div_regex.search(html) is not None - else: + for log_key in html_log: + extra_log_div_regex = re.compile( + f" -+Captured {log_key} call-+
.*{log_key} print line\n?
" + ) + assert extra_log_div_regex.search(html) + + if not html_log: + extra_log_div_regex = re.compile( + " -+Captured \\w+ call-+
.* print line\n
" + ) assert extra_log_div_regex.search(html) is None def test_environment_table_redact_list(self, testdir):