From a12bf970c813c8c46d932256d19793184df21053 Mon Sep 17 00:00:00 2001 From: Gleb Nikonorov Date: Sat, 24 Oct 2020 00:43:55 -0400 Subject: [PATCH 1/4] support pytest's --show-capture=no behavior --- pytest_html/plugin.py | 86 ++++++++++++++++++++++--------------- testing/test_pytest_html.py | 34 ++++++++++++++- 2 files changed, 84 insertions(+), 36 deletions(-) diff --git a/pytest_html/plugin.py b/pytest_html/plugin.py index 45b7aa74..757fc2e3 100644 --- a/pytest_html/plugin.py +++ b/pytest_html/plugin.py @@ -173,7 +173,12 @@ def __init__(self, outcome, report, logfile, config): for extra_index, extra in enumerate(getattr(report, "extra", [])): self.append_extra_html(extra, extra_index, test_index) - self.append_log_html(report, self.additional_html, config.option.capture) + self.append_log_html( + report, + self.additional_html, + config.option.capture, + config.option.showcapture, + ) cells = [ html.td(self.outcome, class_="col-result"), @@ -277,47 +282,60 @@ def append_extra_html(self, extra, extra_index, test_index): ) self.links_html.append(" ") - def append_log_html(self, report, additional_html, pytest_capture_value): - log = html.div(class_="log") - - if pytest_capture_value != "no": - if report.longrepr: - # longreprtext is only filled out on failure by pytest - # otherwise will be None. - # Use full_text if longreprtext is None-ish - # we added full_text elsewhere in this file. - text = report.longreprtext or report.full_text - for line in text.splitlines(): - separator = line.startswith("_ " * 10) - if separator: - log.append(line[:80]) + def _populate_html_log_div(self, log, report): + if report.longrepr: + # longreprtext is only filled out on failure by pytest + # otherwise will be None. + # Use full_text if longreprtext is None-ish + # we added full_text elsewhere in this file. + text = report.longreprtext or report.full_text + for line in text.splitlines(): + separator = line.startswith("_ " * 10) + if separator: + log.append(line[:80]) + else: + exception = line.startswith("E ") + if exception: + log.append(html.span(raw(escape(line)), class_="error")) else: - exception = line.startswith("E ") - if exception: - log.append(html.span(raw(escape(line)), class_="error")) - else: - log.append(raw(escape(line))) - log.append(html.br()) - - for section in report.sections: - header, content = map(escape, section) - log.append(f" {header:-^80} ") + log.append(raw(escape(line))) log.append(html.br()) - if ansi_support(): - converter = ansi_support().Ansi2HTMLConverter( - inline=False, escaped=False - ) - content = converter.convert(content, full=False) - else: - content = _remove_ansi_escape_sequences(content) + for section in report.sections: + header, content = map(escape, section) + log.append(f" {header:-^80} ") + log.append(html.br()) - log.append(raw(content)) - log.append(html.br()) + if ansi_support(): + converter = ansi_support().Ansi2HTMLConverter( + inline=False, escaped=False + ) + content = converter.convert(content, full=False) + else: + content = _remove_ansi_escape_sequences(content) + + log.append(raw(content)) + log.append(html.br()) + + 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) if len(log) == 0: log = html.div(class_="empty log") log.append("No log output captured.") + additional_html.append(log) def _make_media_html_div( diff --git a/testing/test_pytest_html.py b/testing/test_pytest_html.py index ba076621..1312016c 100644 --- a/testing/test_pytest_html.py +++ b/testing/test_pytest_html.py @@ -1046,9 +1046,8 @@ def test_extra_log_reporting_respects_capture_no( ): testdir.makepyfile( """ - import logging import sys - def test_logcapture(): + def test_capture_no(): print("stdout print line") print("stderr print line", file=sys.stderr) """ @@ -1066,3 +1065,34 @@ def test_logcapture(): assert extra_log_div_regex.search(html) is not None else: 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)], + ) + def test_extra_log_reporting_respects_show_capture_no_gleb( + self, testdir, show_capture_flag, should_capture + ): + testdir.makepyfile( + """ + import logging + import sys + def test_logcapture(): + print("stdout print line") + print("stderr print line", file=sys.stderr) + assert False + """ + ) + + 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 stderr call-+
stderr print line\n
" + ) + if should_capture: + assert extra_log_div_regex.search(html) is not None + else: + assert extra_log_div_regex.search(html) is None From c56ce084045c417ef1654cf2b44176787b67b4f3 Mon Sep 17 00:00:00 2001 From: Gleb Nikonorov Date: Sat, 24 Oct 2020 01:11:46 -0400 Subject: [PATCH 2/4] touchups --- testing/test_pytest_html.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/testing/test_pytest_html.py b/testing/test_pytest_html.py index 1312016c..7f8c46a0 100644 --- a/testing/test_pytest_html.py +++ b/testing/test_pytest_html.py @@ -1070,14 +1070,13 @@ def test_capture_no(): "show_capture_flag, should_capture", [("--show-capture=no", False), ("--show-capture=all", True)], ) - def test_extra_log_reporting_respects_show_capture_no_gleb( + def test_extra_log_reporting_respects_show_capture_no( self, testdir, show_capture_flag, should_capture ): testdir.makepyfile( """ - import logging import sys - def test_logcapture(): + def test_show_capture_no(): print("stdout print line") print("stderr print line", file=sys.stderr) assert False From 97d05bca2e575948f4cad77f699f003abbcb47a8 Mon Sep 17 00:00:00 2001 From: Gleb Nikonorov Date: Sat, 24 Oct 2020 01:28:45 -0400 Subject: [PATCH 3/4] Update the changelog --- CHANGES.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index 42b26194..76807d13 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -3,7 +3,7 @@ Release Notes **2.1.2 (unreleased)** -* Respect ``--capture=no`` and ``-s`` pytest flags (`#171 `_) +* Respect ``--capture=no``, ``--show-capture=no``, and ``-s`` pytest flags (`#171 `_) * Thanks to `@bigunyak `_ for reporting and `@gnikonorov `_ for the fix From 6f082776b4cc5a6c5d319c482675065d75ebb29f Mon Sep 17 00:00:00 2001 From: Gleb Nikonorov Date: Tue, 27 Oct 2020 21:48:31 -0400 Subject: [PATCH 4/4] Update 3.0 release date --- CHANGES.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index 06242f9e..82b41805 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,7 +1,7 @@ Release Notes ------------- -**3.0.0 (2020-10-25)** +**3.0.0 (2020-10-28)** * Respect ``--capture=no``, ``--show-capture=no``, and ``-s`` pytest flags (`#171 `_)