noro wrote:
What is the proper way to test (using unit test) a method that print
information?
for example:
def A(s):
print '---'+s+'---'
and i want to check that A("bla") really print out "---bla---"
thanks
amit
For silly module myprog.py:
def A(s):
print '---'+s+'---'
in test_myprog.py:
import unittest
from cStringIO import StringIO # or from StringIO ...
import sys
import myprog
class SomeIOTests(unittest.TestCase):
def setUp(self):
self.held, sys.stdout = sys.stdout, StringIO()
def test_trivialArg(self):
myprog.A('')
self.assertEqual(sys.stdout.getvalue(), '------\n')
def test_simpleArg(self):
myprog.A('simple')
self.assertEqual(sys.stdout.getvalue(), '---simple---\n')
def tearDown(self):
sys.stdout = self.held
if __name__ == '__main__':
unittest.main()
--Scott David Daniels
sc***********@acm.org