python - how can I redirect the output of unittest? Obvious solution doesn't work

懵懂的女人 提交于 2019-12-05 04:17:32
Zaur Nasibov

redirect stderr, e.g.:

python my_unit_test_launcher.py 2> log.txt

To solve it within your testcode, you could also do the following:

import sys
import unittest

class DemoTest(unittest.TestCase):
    def test_one(self):
        print "test one"
        self.assertTrue(True)

    def test_two(self):
        print "test two"
        self.assertTrue(False)

if __name__ == "__main__":
    demo_test = unittest.TestLoader().loadTestsFromTestCase(DemoTest)
    unittest.TextTestRunner(stream=sys.stdout).run(demo_test)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!