diff --git a/CHANGES.rst b/CHANGES.rst index 813439ed..42b26194 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -3,6 +3,10 @@ Release Notes **2.1.2 (unreleased)** +* Respect ``--capture=no`` and ``-s`` pytest flags (`#171 `_) + + * Thanks to `@bigunyak `_ for reporting and `@gnikonorov `_ for the fix + * Make the ``Results`` table ``Links`` column sortable (`#242 `_) * Thanks to `@vashirov `_ for reporting and `@gnikonorov `_ for the fix diff --git a/pytest_html/plugin.py b/pytest_html/plugin.py index 8d69c73c..45b7aa74 100644 --- a/pytest_html/plugin.py +++ b/pytest_html/plugin.py @@ -173,7 +173,7 @@ 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) + self.append_log_html(report, self.additional_html, config.option.capture) cells = [ html.td(self.outcome, class_="col-result"), @@ -277,41 +277,43 @@ def append_extra_html(self, extra, extra_index, test_index): ) self.links_html.append(" ") - def append_log_html(self, report, additional_html): + def append_log_html(self, report, additional_html, pytest_capture_value): log = html.div(class_="log") - 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")) + + 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]) else: - log.append(raw(escape(line))) + 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(html.br()) - for section in report.sections: - header, content = map(escape, section) - log.append(f" {header:-^80} ") - 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) + 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()) + log.append(raw(content)) + log.append(html.br()) 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 0e814910..ba076621 100644 --- a/testing/test_pytest_html.py +++ b/testing/test_pytest_html.py @@ -1036,3 +1036,33 @@ def test_setup(teardown): assert result.ret == 1 assert_results(html, tests=0, passed=0, errors=1) assert "this is the test case" in html + + @pytest.mark.parametrize( + "capture_flag, should_capture", + [("-s", False), ("--capture=no", False), ("--capture=sys", True)], + ) + def test_extra_log_reporting_respects_capture_no( + self, testdir, capture_flag, should_capture + ): + testdir.makepyfile( + """ + import logging + import sys + def test_logcapture(): + print("stdout print line") + print("stderr print line", file=sys.stderr) + """ + ) + + result, html = run(testdir, "report.html", capture_flag) + assert result.ret == 0 + assert_results(html) + + 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