tests: Add tests for stdout and sterr capture (#604)

This commit is contained in:
Jim Brännlund 2023-04-01 11:23:25 +02:00 committed by GitHub
parent 34ff60fbd4
commit f381d082bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 4 deletions

View File

@ -111,7 +111,7 @@ class BaseReport:
for section in report.sections:
header, content = section
if "teardown" in header:
log.append(f" \n{header:-^80} ")
log.append(f"{' ' + header + ' ':-^80}")
log.append(content)
test["log"] += _handle_ansi("\n".join(log))
@ -415,11 +415,17 @@ def _is_error(report):
def _process_logs(report):
log = []
if report.longreprtext:
log.append(report.longreprtext)
log.append(report.longreprtext + "\n")
for section in report.sections:
header, content = section
log.append(f" \n{header:-^80} ")
log.append(f"{' ' + header + ' ':-^80}")
log.append(content)
# weird formatting related to logs
if "log" in header:
log.append("")
if "call" in header:
log.append("")
if not log:
log.append("No log output captured.")
return "\n".join(log)

View File

@ -28,7 +28,7 @@ OUTCOMES = {
def run(pytester, path="report.html", *args):
path = pytester.path.joinpath(path)
pytester.runpytest("-s", "--html", path, *args)
pytester.runpytest("--html", path, *args)
chrome_options = webdriver.ChromeOptions()
if os.environ.get("CI", False):
@ -532,6 +532,39 @@ class TestHTML:
page = run(pytester)
assert_results(page, passed=1)
@pytest.mark.parametrize("no_capture", ["", "-s"])
def test_standard_streams(self, pytester, no_capture):
pytester.makepyfile(
"""
import pytest
import sys
@pytest.fixture
def setup():
print("this is setup stdout")
print("this is setup stderr", file=sys.stderr)
yield
print("this is teardown stdout")
print("this is teardown stderr", file=sys.stderr)
def test_streams(setup):
print("this is call stdout")
print("this is call stderr", file=sys.stderr)
assert True
"""
)
page = run(pytester, "report.html", no_capture)
assert_results(page, passed=1)
log = get_log(page)
for when in ["setup", "call", "teardown"]:
for stream in ["stdout", "stderr"]:
if no_capture:
assert_that(log).does_not_match(f"- Captured {stream} {when} -")
assert_that(log).does_not_match(f"this is {when} {stream}")
else:
assert_that(log).matches(f"- Captured {stream} {when} -")
assert_that(log).matches(f"this is {when} {stream}")
class TestLogCapturing:
LOG_LINE_REGEX = r"\s+this is {}"