Skip to content
Merged
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
1 change: 1 addition & 0 deletions changelog/7517.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Preserve line endings when captured via ``capfd``.
1 change: 1 addition & 0 deletions src/_pytest/capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ def __init__(self, targetfd: int) -> None:
TemporaryFile(buffering=0), # type: ignore[arg-type]
encoding="utf-8",
errors="replace",
newline="",
write_through=True,
)
if targetfd in patchsysdict:
Expand Down
6 changes: 6 additions & 0 deletions testing/test_capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,12 @@ def test_hello(capfd):
)
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(
"""\
Expand Down