I am trying to mock.patch sys.stdout to StringIO as a decorator to record the output for testing. As a 'with' statement it works this way: with mock.patch('sys.stdout', new_callable = StringIO) as recorded_output: print('OUTPUT') r = recorded_output.getvalue() print(r) gets 'OUTPUT', but 'getvalue' doesn't work when used as a decorator: @mock.patch('sys.stdout', new_callable = StringIO) def test_stdout(self, recorded_output): print('OUTPUT') r = recorded_output.getvalue() print(r) gets error: Mock object has no attribute 'getvalue' Does anyone know how to write this as a decorator? Continue reading...