Skip to content

Commit b609a1d

Browse files
authored
Respect pytest --capture=no and -s flags (#353)
1 parent ce1f728 commit b609a1d

File tree

3 files changed

+67
-31
lines changed

3 files changed

+67
-31
lines changed

CHANGES.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ Release Notes
33

44
**2.1.2 (unreleased)**
55

6+
* Respect ``--capture=no`` and ``-s`` pytest flags (`#171 <https://github.com/pytest-dev/pytest-html/issues/171>`_)
7+
8+
* Thanks to `@bigunyak <https://github.com/bigunyak>`_ for reporting and `@gnikonorov <https://github.com/gnikonorov>`_ for the fix
9+
610
* Make the ``Results`` table ``Links`` column sortable (`#242 <https://github.com/pytest-dev/pytest-html/issues/242>`_)
711

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

pytest_html/plugin.py

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ 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)
176+
self.append_log_html(report, self.additional_html, config.option.capture)
177177

178178
cells = [
179179
html.td(self.outcome, class_="col-result"),
@@ -277,41 +277,43 @@ def append_extra_html(self, extra, extra_index, test_index):
277277
)
278278
self.links_html.append(" ")
279279

280-
def append_log_html(self, report, additional_html):
280+
def append_log_html(self, report, additional_html, pytest_capture_value):
281281
log = html.div(class_="log")
282-
if report.longrepr:
283-
# longreprtext is only filled out on failure by pytest
284-
# otherwise will be None.
285-
# Use full_text if longreprtext is None-ish
286-
# we added full_text elsewhere in this file.
287-
text = report.longreprtext or report.full_text
288-
for line in text.splitlines():
289-
separator = line.startswith("_ " * 10)
290-
if separator:
291-
log.append(line[:80])
292-
else:
293-
exception = line.startswith("E ")
294-
if exception:
295-
log.append(html.span(raw(escape(line)), class_="error"))
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])
296294
else:
297-
log.append(raw(escape(line)))
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} ")
298305
log.append(html.br())
299306

300-
for section in report.sections:
301-
header, content = map(escape, section)
302-
log.append(f" {header:-^80} ")
303-
log.append(html.br())
304-
305-
if ansi_support():
306-
converter = ansi_support().Ansi2HTMLConverter(
307-
inline=False, escaped=False
308-
)
309-
content = converter.convert(content, full=False)
310-
else:
311-
content = _remove_ansi_escape_sequences(content)
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)
312314

313-
log.append(raw(content))
314-
log.append(html.br())
315+
log.append(raw(content))
316+
log.append(html.br())
315317

316318
if len(log) == 0:
317319
log = html.div(class_="empty log")

testing/test_pytest_html.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,3 +1036,33 @@ def test_setup(teardown):
10361036
assert result.ret == 1
10371037
assert_results(html, tests=0, passed=0, errors=1)
10381038
assert "this is the test case" in html
1039+
1040+
@pytest.mark.parametrize(
1041+
"capture_flag, should_capture",
1042+
[("-s", False), ("--capture=no", False), ("--capture=sys", True)],
1043+
)
1044+
def test_extra_log_reporting_respects_capture_no(
1045+
self, testdir, capture_flag, should_capture
1046+
):
1047+
testdir.makepyfile(
1048+
"""
1049+
import logging
1050+
import sys
1051+
def test_logcapture():
1052+
print("stdout print line")
1053+
print("stderr print line", file=sys.stderr)
1054+
"""
1055+
)
1056+
1057+
result, html = run(testdir, "report.html", capture_flag)
1058+
assert result.ret == 0
1059+
assert_results(html)
1060+
1061+
extra_log_div_regex = re.compile(
1062+
'<div class="log"> -+Captured stdout call-+ <br/>stdout print line\n<br/> '
1063+
"-+Captured stderr call-+ <br/>stderr print line\n<br/></div>"
1064+
)
1065+
if should_capture:
1066+
assert extra_log_div_regex.search(html) is not None
1067+
else:
1068+
assert extra_log_div_regex.search(html) is None

0 commit comments

Comments
 (0)