diff --git a/src/pytest_html/nextgen.py b/src/pytest_html/nextgen.py index 5c69abed..6c04ffa5 100644 --- a/src/pytest_html/nextgen.py +++ b/src/pytest_html/nextgen.py @@ -111,7 +111,7 @@ def update_test_log(self, report): 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) diff --git a/testing/test_integration.py b/testing/test_integration.py index 5a3e4a70..1321d7ca 100644 --- a/testing/test_integration.py +++ b/testing/test_integration.py @@ -28,7 +28,7 @@ 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 @@ def test_pass(): pass 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 {}"