1. 程式人生 > 實用技巧 >uinttest斷言assert

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