Merge pull request #2880 from samueldg/capture-result-namedtuple
Capture result namedtuple
This commit is contained in:
commit
cb30848e5a
1
AUTHORS
1
AUTHORS
|
@ -156,6 +156,7 @@ Ronny Pfannschmidt
|
||||||
Ross Lawley
|
Ross Lawley
|
||||||
Russel Winder
|
Russel Winder
|
||||||
Ryan Wooden
|
Ryan Wooden
|
||||||
|
Samuel Dion-Girardeau
|
||||||
Samuele Pedroni
|
Samuele Pedroni
|
||||||
Segev Finer
|
Segev Finer
|
||||||
Simon Gomizelj
|
Simon Gomizelj
|
||||||
|
|
|
@ -4,6 +4,7 @@ per-test stdout/stderr capturing mechanism.
|
||||||
"""
|
"""
|
||||||
from __future__ import absolute_import, division, print_function
|
from __future__ import absolute_import, division, print_function
|
||||||
|
|
||||||
|
import collections
|
||||||
import contextlib
|
import contextlib
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
|
@ -306,6 +307,9 @@ class EncodedFile(object):
|
||||||
return getattr(object.__getattribute__(self, "buffer"), name)
|
return getattr(object.__getattribute__(self, "buffer"), name)
|
||||||
|
|
||||||
|
|
||||||
|
CaptureResult = collections.namedtuple("CaptureResult", ["out", "err"])
|
||||||
|
|
||||||
|
|
||||||
class MultiCapture(object):
|
class MultiCapture(object):
|
||||||
out = err = in_ = None
|
out = err = in_ = None
|
||||||
|
|
||||||
|
@ -366,8 +370,8 @@ class MultiCapture(object):
|
||||||
|
|
||||||
def readouterr(self):
|
def readouterr(self):
|
||||||
""" return snapshot unicode value of stdout/stderr capturings. """
|
""" return snapshot unicode value of stdout/stderr capturings. """
|
||||||
return (self.out.snap() if self.out is not None else "",
|
return CaptureResult(self.out.snap() if self.out is not None else "",
|
||||||
self.err.snap() if self.err is not None else "")
|
self.err.snap() if self.err is not None else "")
|
||||||
|
|
||||||
|
|
||||||
class NoCapture:
|
class NoCapture:
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Return stdout/stderr capture results as a ``namedtuple``, so ``out`` and ``err`` can be accessed by attribute.
|
|
@ -922,6 +922,14 @@ class TestStdCapture(object):
|
||||||
out, err = cap.readouterr()
|
out, err = cap.readouterr()
|
||||||
assert err == "error2"
|
assert err == "error2"
|
||||||
|
|
||||||
|
def test_capture_results_accessible_by_attribute(self):
|
||||||
|
with self.getcapture() as cap:
|
||||||
|
sys.stdout.write("hello")
|
||||||
|
sys.stderr.write("world")
|
||||||
|
capture_result = cap.readouterr()
|
||||||
|
assert capture_result.out == "hello"
|
||||||
|
assert capture_result.err == "world"
|
||||||
|
|
||||||
def test_capturing_readouterr_unicode(self):
|
def test_capturing_readouterr_unicode(self):
|
||||||
with self.getcapture() as cap:
|
with self.getcapture() as cap:
|
||||||
print("hx\xc4\x85\xc4\x87")
|
print("hx\xc4\x85\xc4\x87")
|
||||||
|
@ -1083,6 +1091,14 @@ def test_using_capsys_fixture_works_with_sys_stdout_encoding(capsys):
|
||||||
assert err == ''
|
assert err == ''
|
||||||
|
|
||||||
|
|
||||||
|
def test_capsys_results_accessible_by_attribute(capsys):
|
||||||
|
sys.stdout.write("spam")
|
||||||
|
sys.stderr.write("eggs")
|
||||||
|
capture_result = capsys.readouterr()
|
||||||
|
assert capture_result.out == "spam"
|
||||||
|
assert capture_result.err == "eggs"
|
||||||
|
|
||||||
|
|
||||||
@needsosdup
|
@needsosdup
|
||||||
@pytest.mark.parametrize('use', [True, False])
|
@pytest.mark.parametrize('use', [True, False])
|
||||||
def test_fdcapture_tmpfile_remains_the_same(tmpfile, use):
|
def test_fdcapture_tmpfile_remains_the_same(tmpfile, use):
|
||||||
|
|
Loading…
Reference in New Issue