Skip to content

Commit cb30848

Browse files
Merge pull request #2880 from samueldg/capture-result-namedtuple
Capture result namedtuple
2 parents def471b + 8e178e9 commit cb30848

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ Ronny Pfannschmidt
156156
Ross Lawley
157157
Russel Winder
158158
Ryan Wooden
159+
Samuel Dion-Girardeau
159160
Samuele Pedroni
160161
Segev Finer
161162
Simon Gomizelj

_pytest/capture.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"""
55
from __future__ import absolute_import, division, print_function
66

7+
import collections
78
import contextlib
89
import sys
910
import os
@@ -306,6 +307,9 @@ def __getattr__(self, name):
306307
return getattr(object.__getattribute__(self, "buffer"), name)
307308

308309

310+
CaptureResult = collections.namedtuple("CaptureResult", ["out", "err"])
311+
312+
309313
class MultiCapture(object):
310314
out = err = in_ = None
311315

@@ -366,8 +370,8 @@ def stop_capturing(self):
366370

367371
def readouterr(self):
368372
""" return snapshot unicode value of stdout/stderr capturings. """
369-
return (self.out.snap() if self.out is not None else "",
370-
self.err.snap() if self.err is not None else "")
373+
return CaptureResult(self.out.snap() if self.out is not None else "",
374+
self.err.snap() if self.err is not None else "")
371375

372376

373377
class NoCapture:

changelog/2879.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Return stdout/stderr capture results as a ``namedtuple``, so ``out`` and ``err`` can be accessed by attribute.

testing/test_capture.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,14 @@ def test_capturing_readouterr(self):
922922
out, err = cap.readouterr()
923923
assert err == "error2"
924924

925+
def test_capture_results_accessible_by_attribute(self):
926+
with self.getcapture() as cap:
927+
sys.stdout.write("hello")
928+
sys.stderr.write("world")
929+
capture_result = cap.readouterr()
930+
assert capture_result.out == "hello"
931+
assert capture_result.err == "world"
932+
925933
def test_capturing_readouterr_unicode(self):
926934
with self.getcapture() as cap:
927935
print("hx\xc4\x85\xc4\x87")
@@ -1083,6 +1091,14 @@ def test_using_capsys_fixture_works_with_sys_stdout_encoding(capsys):
10831091
assert err == ''
10841092

10851093

1094+
def test_capsys_results_accessible_by_attribute(capsys):
1095+
sys.stdout.write("spam")
1096+
sys.stderr.write("eggs")
1097+
capture_result = capsys.readouterr()
1098+
assert capture_result.out == "spam"
1099+
assert capture_result.err == "eggs"
1100+
1101+
10861102
@needsosdup
10871103
@pytest.mark.parametrize('use', [True, False])
10881104
def test_fdcapture_tmpfile_remains_the_same(tmpfile, use):

0 commit comments

Comments
 (0)