Python, unittest: Can one make the TestRunner completely quiet?

对着背影说爱祢 提交于 2020-06-27 07:45:10

问题


Is there a way to make unittest.TextTestRunner completely quiet, meaning it never prints to output on its own? Even at verbosity=0 it prints results when done.

I want to process the TestResult object returned by the runner before anything is printed.


回答1:


TextTestRunner has a stream=sys.stderr in its constructor:

def __init__(self, stream=sys.stderr, descriptions=1, verbosity=1)

Change it to a null stream.

result = unittest.TextTestRunner(stream = open(os.devnull, 'w')).run(alltests)
if len(result.failures) or len(result.errors):
    print "Sorry."


来源:https://stackoverflow.com/questions/3157456/python-unittest-can-one-make-the-testrunner-completely-quiet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!