def test_with_statement(self): sys.stdout.write('<text_1>') with capture_stdout() as captured: sys.stdout.write('<text_2>') sys.stdout.write('<text_3>') self.assertEqual(self.stdout.getvalue(), '<text_1><text_3>') self.assertEqual(captured.getvalue(), '<text_2>')
def test_cannot_start_twice(self): original = sys.stdout patcher = capture_stdout() patcher.start() try: self.assertRaisesRegexp(ValueError, 'Capturer is already started', patcher.start) finally: patcher.stop() self.assertEqual(sys.stdout, original)
def test_start_stop(self): sys.stdout.write('<text_1>') patcher = capture_stdout() captured = patcher.start() try: sys.stdout.write('<text_2>') finally: patcher.stop() sys.stdout.write('<text_3>') self.assertEqual(self.stdout.getvalue(), '<text_1><text_3>') self.assertEqual(captured.getvalue(), '<text_2>')
def test_creating_patcher_does_not_patch(self): original = sys.stdout capture_stdout() self.assertEqual(sys.stdout, original)
def test_cannot_stop(self): original = sys.stdout patcher = capture_stdout() self.assertRaisesRegexp(ValueError, 'Stopping non-started Capturer', patcher.stop) self.assertEqual(sys.stdout, original)