Skip to content

use --show-capture as filter #447

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

Closed
wants to merge 1 commit into from
Closed
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
15 changes: 7 additions & 8 deletions src/pytest_html/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ def __init__(self, outcome, report, logfile, config):
self.append_log_html(
report,
self.additional_html,
config.option.capture,
config.option.showcapture,
)

Expand Down Expand Up @@ -175,7 +174,7 @@ def _format_time(self, report):
duration_as_gmtime = time.gmtime(report.duration)
return time.strftime(duration_formatter, duration_as_gmtime)

def _populate_html_log_div(self, log, report):
def _populate_html_log_div(self, log, report, show_capture):
if report.longrepr:
# longreprtext is only filled out on failure by pytest
# otherwise will be None.
Expand All @@ -194,8 +193,13 @@ def _populate_html_log_div(self, log, report):
log.append(raw(escape(line)))
log.append(html.br())

if show_capture == "no":
return

for section in report.sections:
header, content = map(escape, section)
if show_capture != "all" and show_capture not in header:
continue
log.append(f" {header:-^80} ")
log.append(html.br())

Expand All @@ -214,16 +218,11 @@ 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)
self._populate_html_log_div(log, report, pytest_show_capture_value)

if len(log) == 0:
log = html.div(class_="empty log")
Expand Down
56 changes: 34 additions & 22 deletions testing/test_pytest_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -1152,27 +1152,28 @@ def test_setup(teardown):
assert "this is the test case" in html

@pytest.mark.parametrize(
"capture_flag, should_capture",
[("-s", False), ("--capture=no", False), ("--capture=sys", True)],
"show_capture_flag, should_capture",
[("--show-capture=no", False), ("--show-capture=all", True)],
)
def test_extra_log_reporting_respects_capture_no(
self, testdir, capture_flag, should_capture
def test_extra_log_reporting_respects_show_capture_no(
self, testdir, show_capture_flag, should_capture
):
testdir.makepyfile(
"""
import sys
def test_capture_no():
def test_show_capture_no():
print("stdout print line")
print("stderr print line", file=sys.stderr)
assert False
"""
)

result, html = run(testdir, "report.html", capture_flag)
assert result.ret == 0
assert_results(html)
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/> '
'<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:
Expand All @@ -1181,33 +1182,44 @@ def test_capture_no():
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)],
"show_capture_flag, html_log",
[
("--show-capture=no", []),
("--show-capture=all", ["stdout", "stderr", "log"]),
("--show-capture=stdout", ["stdout"]),
("--show-capture=stderr", ["stderr"]),
("--show-capture=log", ["log"]),
],
)
def test_extra_log_reporting_respects_show_capture_no(
self, testdir, show_capture_flag, should_capture
def test_extra_log_reporting_respects_show_capture(
self, testdir, show_capture_flag, html_log
):
testdir.makepyfile(
"""
import sys
def test_show_capture_no():
import logging
def test_show_capture():
print("stdout print line")
print("stderr print line", file=sys.stderr)
logging.warning("log print line")
assert False
"""
)

result, html = run(testdir, "report.html", show_capture_flag)
assert result.ret == 1
assert_results(html, passed=0, failed=1)
assert_results(html, failed=1, passed=0)

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:
for log_key in html_log:
extra_log_div_regex = re.compile(
f" -+Captured {log_key} call-+ <br/>.*{log_key} print line\n?<br/>"
)
assert extra_log_div_regex.search(html)

if not html_log:
extra_log_div_regex = re.compile(
" -+Captured \\w+ call-+ <br/>.* print line\n<br/>"
)
assert extra_log_div_regex.search(html) is None

def test_environment_table_redact_list(self, testdir):
Expand Down