@@ -32,6 +32,10 @@ def StdCapture(out=True, err=True, in_=True):
3232 return capture .MultiCapture (out , err , in_ , Capture = capture .SysCapture )
3333
3434
35+ def TeeStdCapture (out = True , err = True , in_ = True ):
36+ return capture .MultiCapture (out , err , in_ , Capture = capture .TeeSysCapture )
37+
38+
3539class TestCaptureManager :
3640 def test_getmethod_default_no_fd (self , monkeypatch ):
3741 from _pytest .capture import pytest_addoption
@@ -816,6 +820,25 @@ def test_write_bytes_to_buffer(self):
816820 assert f .getvalue () == "foo\r \n "
817821
818822
823+ class TestCaptureAndPassthroughIO (TestCaptureIO ):
824+ def test_text (self ):
825+ sio = io .StringIO ()
826+ f = capture .CaptureAndPassthroughIO (sio )
827+ f .write ("hello" )
828+ s1 = f .getvalue ()
829+ assert s1 == "hello"
830+ s2 = sio .getvalue ()
831+ assert s2 == s1
832+ f .close ()
833+ sio .close ()
834+
835+ def test_unicode_and_str_mixture (self ):
836+ sio = io .StringIO ()
837+ f = capture .CaptureAndPassthroughIO (sio )
838+ f .write ("\u00f6 " )
839+ pytest .raises (TypeError , f .write , b"hello" )
840+
841+
819842def test_dontreadfrominput ():
820843 from _pytest .capture import DontReadFromInput
821844
@@ -1112,6 +1135,23 @@ def test_stdin_nulled_by_default(self):
11121135 pytest .raises (IOError , sys .stdin .read )
11131136
11141137
1138+ class TestTeeStdCapture (TestStdCapture ):
1139+ captureclass = staticmethod (TeeStdCapture )
1140+
1141+ def test_capturing_error_recursive (self ):
1142+ """ for TeeStdCapture since we passthrough stderr/stdout, cap1
1143+ should get all output, while cap2 should only get "cap2\n " """
1144+
1145+ with self .getcapture () as cap1 :
1146+ print ("cap1" )
1147+ with self .getcapture () as cap2 :
1148+ print ("cap2" )
1149+ out2 , err2 = cap2 .readouterr ()
1150+ out1 , err1 = cap1 .readouterr ()
1151+ assert out1 == "cap1\n cap2\n "
1152+ assert out2 == "cap2\n "
1153+
1154+
11151155class TestStdCaptureFD (TestStdCapture ):
11161156 pytestmark = needsosdup
11171157 captureclass = staticmethod (StdCaptureFD )
@@ -1252,7 +1292,7 @@ def test_capture_again():
12521292 )
12531293
12541294
1255- @pytest .mark .parametrize ("method" , ["SysCapture" , "FDCapture" ])
1295+ @pytest .mark .parametrize ("method" , ["SysCapture" , "FDCapture" , "TeeSysCapture" ])
12561296def test_capturing_and_logging_fundamentals (testdir , method ):
12571297 if method == "StdCaptureFD" and not hasattr (os , "dup" ):
12581298 pytest .skip ("need os.dup" )
0 commit comments