Last active
August 29, 2015 14:00
-
-
Save edgabaldi/11407358 to your computer and use it in GitHub Desktop.
testing stdout in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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