Skip to content

Commit d971d00

Browse files
authored
Respect --show-capture=no flag (#359)
1 parent 8fd44c8 commit d971d00

File tree

3 files changed

+85
-38
lines changed

3 files changed

+85
-38
lines changed

CHANGES.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
Release Notes
22
-------------
33

4-
**3.0.0 (2020-10-25)**
4+
**3.0.0 (2020-10-28)**
55

6-
* Respect ``--capture=no`` and ``-s`` pytest flags (`#171 <https://github.com/pytest-dev/pytest-html/issues/171>`_)
6+
* Respect ``--capture=no``, ``--show-capture=no``, and ``-s`` pytest flags (`#171 <https://github.com/pytest-dev/pytest-html/issues/171>`_)
77

88
* Thanks to `@bigunyak <https://github.com/bigunyak>`_ for reporting and `@gnikonorov <https://github.com/gnikonorov>`_ for the fix
99

pytest_html/plugin.py

+52-34
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,12 @@ def __init__(self, outcome, report, logfile, config):
173173
for extra_index, extra in enumerate(getattr(report, "extra", [])):
174174
self.append_extra_html(extra, extra_index, test_index)
175175

176-
self.append_log_html(report, self.additional_html, config.option.capture)
176+
self.append_log_html(
177+
report,
178+
self.additional_html,
179+
config.option.capture,
180+
config.option.showcapture,
181+
)
177182

178183
cells = [
179184
html.td(self.outcome, class_="col-result"),
@@ -277,47 +282,60 @@ def append_extra_html(self, extra, extra_index, test_index):
277282
)
278283
self.links_html.append(" ")
279284

280-
def append_log_html(self, report, additional_html, pytest_capture_value):
281-
log = html.div(class_="log")
282-
283-
if pytest_capture_value != "no":
284-
if report.longrepr:
285-
# longreprtext is only filled out on failure by pytest
286-
# otherwise will be None.
287-
# Use full_text if longreprtext is None-ish
288-
# we added full_text elsewhere in this file.
289-
text = report.longreprtext or report.full_text
290-
for line in text.splitlines():
291-
separator = line.startswith("_ " * 10)
292-
if separator:
293-
log.append(line[:80])
285+
def _populate_html_log_div(self, log, report):
286+
if report.longrepr:
287+
# longreprtext is only filled out on failure by pytest
288+
# otherwise will be None.
289+
# Use full_text if longreprtext is None-ish
290+
# we added full_text elsewhere in this file.
291+
text = report.longreprtext or report.full_text
292+
for line in text.splitlines():
293+
separator = line.startswith("_ " * 10)
294+
if separator:
295+
log.append(line[:80])
296+
else:
297+
exception = line.startswith("E ")
298+
if exception:
299+
log.append(html.span(raw(escape(line)), class_="error"))
294300
else:
295-
exception = line.startswith("E ")
296-
if exception:
297-
log.append(html.span(raw(escape(line)), class_="error"))
298-
else:
299-
log.append(raw(escape(line)))
300-
log.append(html.br())
301-
302-
for section in report.sections:
303-
header, content = map(escape, section)
304-
log.append(f" {header:-^80} ")
301+
log.append(raw(escape(line)))
305302
log.append(html.br())
306303

307-
if ansi_support():
308-
converter = ansi_support().Ansi2HTMLConverter(
309-
inline=False, escaped=False
310-
)
311-
content = converter.convert(content, full=False)
312-
else:
313-
content = _remove_ansi_escape_sequences(content)
304+
for section in report.sections:
305+
header, content = map(escape, section)
306+
log.append(f" {header:-^80} ")
307+
log.append(html.br())
314308

315-
log.append(raw(content))
316-
log.append(html.br())
309+
if ansi_support():
310+
converter = ansi_support().Ansi2HTMLConverter(
311+
inline=False, escaped=False
312+
)
313+
content = converter.convert(content, full=False)
314+
else:
315+
content = _remove_ansi_escape_sequences(content)
316+
317+
log.append(raw(content))
318+
log.append(html.br())
319+
320+
def append_log_html(
321+
self,
322+
report,
323+
additional_html,
324+
pytest_capture_value,
325+
pytest_show_capture_value,
326+
):
327+
log = html.div(class_="log")
328+
329+
should_skip_captured_output = pytest_capture_value == "no"
330+
if report.outcome == "failed" and not should_skip_captured_output:
331+
should_skip_captured_output = pytest_show_capture_value == "no"
332+
if not should_skip_captured_output:
333+
self._populate_html_log_div(log, report)
317334

318335
if len(log) == 0:
319336
log = html.div(class_="empty log")
320337
log.append("No log output captured.")
338+
321339
additional_html.append(log)
322340

323341
def _make_media_html_div(

testing/test_pytest_html.py

+31-2
Original file line numberDiff line numberDiff line change
@@ -1061,9 +1061,8 @@ def test_extra_log_reporting_respects_capture_no(
10611061
):
10621062
testdir.makepyfile(
10631063
"""
1064-
import logging
10651064
import sys
1066-
def test_logcapture():
1065+
def test_capture_no():
10671066
print("stdout print line")
10681067
print("stderr print line", file=sys.stderr)
10691068
"""
@@ -1081,3 +1080,33 @@ def test_logcapture():
10811080
assert extra_log_div_regex.search(html) is not None
10821081
else:
10831082
assert extra_log_div_regex.search(html) is None
1083+
1084+
@pytest.mark.parametrize(
1085+
"show_capture_flag, should_capture",
1086+
[("--show-capture=no", False), ("--show-capture=all", True)],
1087+
)
1088+
def test_extra_log_reporting_respects_show_capture_no(
1089+
self, testdir, show_capture_flag, should_capture
1090+
):
1091+
testdir.makepyfile(
1092+
"""
1093+
import sys
1094+
def test_show_capture_no():
1095+
print("stdout print line")
1096+
print("stderr print line", file=sys.stderr)
1097+
assert False
1098+
"""
1099+
)
1100+
1101+
result, html = run(testdir, "report.html", show_capture_flag)
1102+
assert result.ret == 1
1103+
assert_results(html, passed=0, failed=1)
1104+
1105+
extra_log_div_regex = re.compile(
1106+
'<div class="log">.*-+Captured stdout call-+ <br/>stdout print line\n<br/> '
1107+
"-+Captured stderr call-+ <br/>stderr print line\n<br/></div>"
1108+
)
1109+
if should_capture:
1110+
assert extra_log_div_regex.search(html) is not None
1111+
else:
1112+
assert extra_log_div_regex.search(html) is None

0 commit comments

Comments
 (0)