Respect pytest --capture=no and -s flags (#353)

This commit is contained in:
Gleb Nikonorov 2020-10-23 09:44:31 -04:00 committed by GitHub
parent ce1f728a97
commit b609a1d914
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 67 additions and 31 deletions

View File

@ -3,6 +3,10 @@ Release Notes
**2.1.2 (unreleased)**
* Respect ``--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
* Make the ``Results`` table ``Links`` column sortable (`#242 <https://github.com/pytest-dev/pytest-html/issues/242>`_)
* Thanks to `@vashirov <https://github.com/vashirov>`_ for reporting and `@gnikonorov <https://github.com/gnikonorov>`_ for the fix

View File

@ -173,7 +173,7 @@ 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)
self.append_log_html(report, self.additional_html, config.option.capture)
cells = [
html.td(self.outcome, class_="col-result"),
@ -277,41 +277,43 @@ class HTMLReport:
)
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")

View File

@ -1036,3 +1036,33 @@ class TestHTML:
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(
'<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