Accessing test status in pyunit tearDown

亡梦爱人 提交于 2019-12-06 09:36:53

Use TestResult.

import unittest

class TestFoo(unittest.TestCase):
    def test_ok(self):
        self.assertEqual(1+2, 3)
    def test_fail(self):
        self.assertEqual(1+2, 4)
    def test_error(self):
        1/0
    @unittest.skip('blah')
    def test_skip(self):
        self.assertEqual(42, 42)

class MyResult(unittest.TextTestResult):
    def addError(self, test, err):
        self.call_web_api(test, err)
        return super(MyResult, self).addError(test, err)
    def addFailure(self, test, err):
        self.call_web_api(test, err)
        return super(MyResult, self).addFailure(test, err)
    def call_web_api(self, test, err):
        print('***', test, err)

if __name__ == '__main__':
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(TestFoo))

    runner = unittest.TextTestRunner(resultclass=MyResult)
    runner.run(suite)

You can also use as follow:

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