搜尋

首頁  >  問答  >  主體

单元测试 - Python unittest如何收集测试结果

PHP中文网PHP中文网2810 天前1080

全部回覆(1)我來回復

  • PHP中文网

    PHP中文网2017-04-18 10:03:33

    使用unittest模組本身的方法即可:

    import unittest
    
    
    class TestStringMethods(unittest.TestCase):
    
        def test_upper(self):
            self.assertEqual('foo'.upper(), 'F0O')
            self.assertEqual('foo'.upper(), 'F0O')
    
        def test_isupper(self):
            self.assertTrue('FOO'.isupper())
            self.assertFalse('Foo'.isupper())
    
        def test_split(self):
            s = 'hello world'
            self.assertEqual(s.split(), ['hello', 'world'])
            # check that s.split fails when the separator is not a string
            with self.assertRaises(TypeError):
                s.split(2)
    
    
    def main():
        suite = unittest.TestLoader().loadTestsFromTestCase(TestStringMethods)
        test_result = unittest.TextTestRunner(verbosity=2).run(suite)
        print('All case number')
        print(test_result.testsRun)
        print('Failed case number')
        print(len(test_result.failures))
        print('Failed case and reason')
        print(test_result.failures)
        for case, reason in test_result.failures:
            print case.id()
            print reason
    
    
    if __name__ == '__main__':
        main()

    回覆
    0
  • 取消回覆