Respect --show-capture=no flag (#359)

This commit is contained in:
Gleb Nikonorov 2020-10-29 11:57:27 -04:00 committed by GitHub
parent 8fd44c8119
commit d971d0065d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 87 additions and 40 deletions

View File

@ -1,9 +1,9 @@
Release Notes
-------------
**3.0.0 (2020-10-25)**
**3.0.0 (2020-10-28)**
* Respect ``--capture=no`` and ``-s`` pytest flags (`#171 <https://github.com/pytest-dev/pytest-html/issues/171>`_)
* Respect ``--capture=no``, ``--show-capture=no``, and ``-s`` pytest flags (`#171 <https://github.com/pytest-dev/pytest-html/issues/171>`_)
* Thanks to `@bigunyak <https://github.com/bigunyak>`_ for reporting and `@gnikonorov <https://github.com/gnikonorov>`_ for the fix

View File

@ -173,7 +173,12 @@ class HTMLReport:
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 @@ class HTMLReport:
)
self.links_html.append(" ")
def append_log_html(self, report, additional_html, pytest_capture_value):
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:
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())
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")
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:
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())
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())
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(

View File

@ -1061,9 +1061,8 @@ class TestHTML:
):
testdir.makepyfile(
"""
import logging
import sys
def test_logcapture():
def test_capture_no():
print("stdout print line")
print("stderr print line", file=sys.stderr)
"""
@ -1081,3 +1080,33 @@ class TestHTML:
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(
self, testdir, show_capture_flag, should_capture
):
testdir.makepyfile(
"""
import sys
def test_show_capture_no():
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(
'<div class="log">.*-+Captured stdout call-+ <br/>stdout print line\n<br/> '
"-+Captured stderr call-+ <br/>stderr print line\n<br/></div>"
)
if should_capture:
assert extra_log_div_regex.search(html) is not None
else:
assert extra_log_div_regex.search(html) is None