From 8616a5f1d989eec5e2c5f2129040149fe4cf4347 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Mon, 20 Jul 2020 08:54:20 -0700 Subject: [PATCH] Preserve newlines when captured with capfd --- changelog/7517.bugfix.rst | 1 + src/_pytest/capture.py | 1 + testing/test_capture.py | 6 ++++++ 3 files changed, 8 insertions(+) create mode 100644 changelog/7517.bugfix.rst diff --git a/changelog/7517.bugfix.rst b/changelog/7517.bugfix.rst new file mode 100644 index 000000000..2d062dc1e --- /dev/null +++ b/changelog/7517.bugfix.rst @@ -0,0 +1 @@ +Preserve line endings when captured via ``capfd``. diff --git a/src/_pytest/capture.py b/src/_pytest/capture.py index 3f9c60fb9..f538b67ec 100644 --- a/src/_pytest/capture.py +++ b/src/_pytest/capture.py @@ -388,6 +388,7 @@ class FDCaptureBinary: TemporaryFile(buffering=0), # type: ignore[arg-type] encoding="utf-8", errors="replace", + newline="", write_through=True, ) if targetfd in patchsysdict: diff --git a/testing/test_capture.py b/testing/test_capture.py index a3bd4b623..bc89501c7 100644 --- a/testing/test_capture.py +++ b/testing/test_capture.py @@ -514,6 +514,12 @@ class TestCaptureFixture: ) reprec.assertoutcome(passed=1) + @pytest.mark.parametrize("nl", ("\n", "\r\n", "\r")) + def test_cafd_preserves_newlines(self, capfd, nl): + print("test", end=nl) + out, err = capfd.readouterr() + assert out.endswith(nl) + def test_capfdbinary(self, testdir): reprec = testdir.inline_runsource( """\