Skip to content
Open
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 AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ Hamza Mobeen
Harald Armin Massa
Harshna
Henk-Jaap Wagenaar
Henry Schreiner
Holger Kohr
Hugo van Kemenade
Hui Wang (coldnight)
Expand Down
1 change: 1 addition & 0 deletions changelog/14687.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The default fd-level output capturing (``--capture=fd``) now skips reading the capture buffer for tests which produce no output, reducing per-test overhead in large test suites.
6 changes: 6 additions & 0 deletions src/_pytest/capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,9 @@ class FDCaptureBinary(FDCaptureBase[bytes]):

def snap(self) -> bytes:
self._assert_state("snap", ("started", "suspended"))
# Avoid seek/read/truncate in the common case of no output.
if os.fstat(self.tmpfile.fileno()).st_size == 0:
return self.EMPTY_BUFFER
self.tmpfile.seek(0)
res = self.tmpfile.buffer.read()
self.tmpfile.seek(0)
Expand All @@ -588,6 +591,9 @@ class FDCapture(FDCaptureBase[str]):

def snap(self) -> str:
self._assert_state("snap", ("started", "suspended"))
# Avoid seek/read/truncate in the common case of no output.
if os.fstat(self.tmpfile.fileno()).st_size == 0:
return self.EMPTY_BUFFER
self.tmpfile.seek(0)
res = self.tmpfile.read()
self.tmpfile.seek(0)
Expand Down
Loading