python unittest学习2---简单的例子

python unittest简单的例子如下:

class TestString(unittest.TestCase):
    def test_upper(self):
        self.assertEqual("foo".upper(), "FOO")
     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"])
        with self.assertRaises(TypeError):
        s.split(2)

if __name__=="__main__":
unittest.main()

备注:

1.创建该类时,需要继承unittest.TestCase类

2.测试用例的方法都需要用小写的“test”开头,因为testrunner通过这个规则识别哪些是testcase

3.testrunner会根据定义的test开头的用例执行完后将结果汇聚,并生产报告,并非里面的assert语句

原文地址:https://www.cnblogs.com/dmtz/p/10967460.html