Home  >  Q&A  >  body text

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

PHP中文网PHP中文网2762 days ago1057

reply all(1)I'll reply

  • PHP中文网

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

    Just use the method of the unittest module itself:

    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()

    reply
    0
  • Cancelreply