@@ -113,15 +113,15 @@ def assertRaisesMessage(self, expected_exception, expected_message, callable_obj
113113 self .assertEqual (msg , expected_message )
114114
115115 @contextmanager
116- def withOutput (self ):
116+ def withOutput (self , buffer_type = StringBuffer ):
117117 """
118118 Capture and suppress stdout and stderr.
119119 example:
120120 with self.withOutput() as (out, err):
121121 (do main logic)
122122 (verify out.getvalue() or err.getvalue())
123123 """
124- new_out , new_err = StringBuffer (), StringBuffer ()
124+ new_out , new_err = buffer_type (), buffer_type ()
125125 old_out , old_err = sys .stdout , sys .stderr
126126
127127 try :
@@ -130,6 +130,18 @@ def withOutput(self):
130130 finally :
131131 sys .stdout , sys .stderr = old_out , old_err
132132
133+ @contextmanager
134+ def withBytesOutput (self ):
135+ """
136+ Capture and suppress stdout and stderr. The value is represented as bytes.
137+ example:
138+ with self.withBytesOutput() as (out, err):
139+ (do main logic)
140+ (verify out.getvalue() or err.getvalue())
141+ """
142+ with self .withOutput (six .BytesIO ) as (out , err ):
143+ yield out , err
144+
133145 @contextmanager
134146 def withAssertOutput (self , expected_stdout , expected_stderr , encoding = 'utf-8' ):
135147 with self .withOutput () as (out , err ):
0 commit comments