Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
64 changes: 33 additions & 31 deletions pytest_html/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -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")
Expand Down
30 changes: 30 additions & 0 deletions testing/test_pytest_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
'<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