diff --git a/AUTHORS b/AUTHORS index 33ee644ea68..ca0f329466f 100644 --- a/AUTHORS +++ b/AUTHORS @@ -204,6 +204,7 @@ Hamza Mobeen Harald Armin Massa Harshna Henk-Jaap Wagenaar +Henry Schreiner Holger Kohr Hugo van Kemenade Hui Wang (coldnight) diff --git a/changelog/14687.improvement.rst b/changelog/14687.improvement.rst new file mode 100644 index 00000000000..04bef6173c9 --- /dev/null +++ b/changelog/14687.improvement.rst @@ -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. diff --git a/src/_pytest/capture.py b/src/_pytest/capture.py index f9f8f9e4a28..b914bc2831c 100644 --- a/src/_pytest/capture.py +++ b/src/_pytest/capture.py @@ -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) @@ -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)