uinttest断言assert

1、简单案例

import unittest
class Test(unittest.TestCase):
    def test01(self):
        #  判断 a = b
        a = "100"
        b = "100"
        self.assertEqual(a, b)
    def test02(self):
        #  判断a是否是b的子集
        a = "1"
        b = "123456789"
        self.assertIn(a, b)
    def test03(self):
        #  判断a为True
        a = True
        self.assertTrue(a)
    def test04(self):
        a = "jiale"
        b = 123
        self.assertEqual(a, b,msg = "失败原因:%s != %d"%(a,b))
        #  msg参数,遇到异常后自定义输出信息
if __name__ == "__main__":
    unittest.main()

2、unittest常用的断言方法

1.assertEqual(self, first, second, msg=None)

--判断两个参数相等:first == second

2.assertNotEqual(self, first, second, msg=None)

--判断两个参数不相等:first != second

3.assertIn(self, member, container, msg=None)

--判断是字符串是否包含:member in container

4.assertNotIn(self, member, container, msg=None)

--判断是字符串是否不包含:member not in container

5.assertTrue(self, expr, msg=None)

--判断是否为真:expr is True

6.assertFalse(self, expr, msg=None)

--判断是否为假:expr is False

7.assertIsNone(self, obj, msg=None)

--判断是否为None:obj is None

8.assertIsNotNone(self, obj, msg=None)
--判断是否不为None:obj is not None

参考自:https://www.cnblogs.com/yoyoketang/p/6691270.html

posted on 2020-11-18 14:40  Tester十点半  阅读(142)  评论(0编辑  收藏  举报

导航