python - Is there a way to suppress printing that is done within a unit test? -


edit: please notice i'm using python 2.6 (as tagged)

say have following:

class foo:     def bar(self):         print 'bar'         return 7 

and have following unit test:

import unittest class ut_foo(unittest.testcase):     def test_bar(self):         obj = foo()         res = obj.bar()         self.assertequal(res, 7) 

so if run:

unittest.main() 

i get:

bar # <-- don't want this, *do* want rest . ---------------------------------------------------------------------- ran 1 test in 0.002s  ok exit code:  false 

my question is: there way suppress output of object being tested while still getting output of unittest framework?

call unittest option "-b" - buffer stdout , stderr

foo.py

class foo:     def bar(self):         print "bar"         return 7 

test.py

import unittest foo import foo  class test_foo(unittest.testcase):     def test_bar(self):         obj = foo()         res = obj.bar()         self.assertequal(res, 7)  if __name__ == "__main__":     unittest.main() 

run -b option

$ python test.py -b . ---------------------------------------------------------------------- ran 1 test in 0.000s  ok 

alternative: use nose

$ pip install nose 

what installs command nosetests

note, have modified test suite have class , methods prefixed test satisfy nose default test discovery rules.

nosetests default not show output

$ nosetests . ---------------------------------------------------------------------- ran 1 test in 0.002s  ok 

if want see output, use -s switch:

$ nosetests -s bar . ---------------------------------------------------------------------- ran 1 test in 0.002s  ok 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -