Skip to content

Commit 8616a5f

Browse files
committed
Preserve newlines when captured with capfd
1 parent 41d211c commit 8616a5f

File tree

3 files changed

+8
-0
lines changed

3 files changed

+8
-0
lines changed

changelog/7517.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Preserve line endings when captured via ``capfd``.

src/_pytest/capture.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ def __init__(self, targetfd: int) -> None:
388388
TemporaryFile(buffering=0), # type: ignore[arg-type]
389389
encoding="utf-8",
390390
errors="replace",
391+
newline="",
391392
write_through=True,
392393
)
393394
if targetfd in patchsysdict:

testing/test_capture.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,12 @@ def test_hello(capfd):
514514
)
515515
reprec.assertoutcome(passed=1)
516516

517+
@pytest.mark.parametrize("nl", ("\n", "\r\n", "\r"))
518+
def test_cafd_preserves_newlines(self, capfd, nl):
519+
print("test", end=nl)
520+
out, err = capfd.readouterr()
521+
assert out.endswith(nl)
522+
517523
def test_capfdbinary(self, testdir):
518524
reprec = testdir.inline_runsource(
519525
"""\

0 commit comments

Comments
 (0)