Skip to content

Respect --show-capture=no flag #359

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 6 commits into from
Oct 29, 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: 2 additions & 2 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -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

Expand Down
86 changes: 52 additions & 34 deletions pytest_html/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -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(
Expand Down
33 changes: 31 additions & 2 deletions testing/test_pytest_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -1061,9 +1061,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)
"""
Expand All @@ -1081,3 +1080,33 @@ 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(
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