Skip to content

Instantly share code, notes, and snippets.

@edgabaldi
Last active August 29, 2015 14:00
Show Gist options
  • Save edgabaldi/11407358 to your computer and use it in GitHub Desktop.
Save edgabaldi/11407358 to your computer and use it in GitHub Desktop.
testing stdout in python
import sys
from StringIO import StringIO
from unittest import TestCase
class Foo(object):
def hello(self):
print 'world'
class StdoutTest(TestCase):
def setUp(self):
self.output = StringIO()
self.saved_stdout = sys.stdout
sys.stdout = self.output
def test_hello(self):
obj = Foo()
obj.hello()
self.assertEqual(self.output.getvalue(), "world\n")
def tearDown(self):
self.output.close()
sys.stdout = self.saved_stdout
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment