單元測試框架之unittest(五)
阿新 • • 發佈:2018-12-27
一、摘要
單元測試裡很重要的一個部分就是斷言,unittest為我們提供了很多斷言方法
- assertEqual(a, b, msg=None)斷言 a == b
- assertNotEqual(a, b, msg=None)斷言 a != b
- assertTrue(expr, msg=None)斷言 bool(expr) is True
- assertFalse(expr, msg=None)斷言 bool(expr) is False
- assertIs(a, b, msg=None)斷言 a is b
- assertIsNot(a, b, msg=None)斷言 a is not b
- assertIsNone(expr, msg=None)斷言 expr is None
- assertIsNotNone(expr, msg=None)斷言 expr is not None
- assertIn(a, b, msg=None)斷言 a in b
- assertNotIn(a, b, msg=None)斷言 a not in b
- assertIsInstance(obj, cls, msg=None)斷言 obj is cls instance
- assertNotIsInstance(obj, cls, msg=None)斷言 obj is not cls instance
- assertRaises(exc, fun, *args, **kwds)斷言 fun(*args, **kwds) raises exc 否則丟擲斷言異常
- assertRaisesRegex(exc, r, fun, *args, **kwds) 斷言 fun(*args, **kwds) raises exc and the exc message matches regex r 否則丟擲斷言異常
二、程式碼例項
# encoding = utf-8 import unittest import random # 被測試類 classToBeTest(object): @classmethod def sum(cls, a, b): return a + b @classmethod def div(cls, a, b): return a/b @classmethod def return_none(cls): return None # 單元測試類 class TestToBeTest(unittest.TestCase): # assertEqual()方法例項 def test_assertequal(self): try: a, b = 100, 200 sum = 300 # 斷言a+b等於sum self.assertEqual(a + b, sum, '斷言失敗,%s+%s != %s ' %(a, b, sum)) except AssertionError as e: print(e) # assertNotEqual()方法例項 def test_assertnotequal(self): try: a, b = 100, 200 res = -1000 # 斷言a-b不等於res self.assertNotEqual(a - b, res, '斷言失敗,%s-%s != %s ' %(a, b, res)) except AssertionError as e: print(e) # assertTure()方法例項 def test_asserttrue(self): list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] list2 = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] list3 = list1[::-1] print(list3) try: # 斷言表示式為真 self.assertTrue(list3 == list2, "表示式為假") except AssertionError as e: print(e) # assertFalse()方法例項 def test_assertfalse(self): list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] list2 = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] list3 = list1[::-1] try: # 斷言表示式為假 self.assertFalse(list3 == list1, "表示式為真") except AssertionError as e: print(e) # assertIs()方法例項 def test_assertis(self): list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] list2 = list1 try: # 斷言list2和list1屬於同一個物件 self.assertIs(list1, list2, "%s 與 %s 不屬於同一物件" % (list1, list2)) except AssertionError as e: print(e) # assertIsNot()方法例項 def test_assertisnot(self): list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] list2 = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] try: # 斷言list2和list1不屬於同一個物件 self.assertIsNot(list2, list1, "%s 與 %s 屬於同一物件" % (list1, list2)) except AssertionError as e: print(e) # assertIsNone()方法例項 def test_assertisnone(self): try: results = ToBeTest.return_none() # 斷言表示式結果是none self.assertIsNone(results, "is not none") except AssertionError as e: print(e) # assertIsNotNone()方法例項 def test_assertisnotnone(self): try: results = ToBeTest.sum(4, 5) # 斷言表示式結果不是none self.assertIsNotNone(results, "is none") except AssertionError as e: print(e) # assertIn()方法例項 def test_assertin(self): try: str1 = "this is unit test demo" str2 = "demo" # 斷言str2包含在str1中 self.assertIn(str2, str1, "%s 不被包含在 %s中" %(str2, str1)) except AssertionError as e: print(e) # assertNotIn()方法例項 def test_assertnotin(self): try: str1 = "this is unit test demo" str2 = "ABC" # 斷言str2不包含在str1中 self.assertNotIn(str2, str1, "%s 包含在 %s 中" % (str2, str1)) except AssertionError as e: print(e) # assertIsInstance()方法例項 def test_assertisinstance(self): try: o = ToBeTest k = object # 斷言測試物件o是k的型別 self.assertIsInstance(o, k, "%s的型別不是%s" % (o, k)) except AssertionError as e: print(e) # assertNotIsInstance()方法例項 def test_assertnotisinstance(self): try: o = ToBeTest k = int # 斷言測試物件o不是k的型別 self.assertNotIsInstance(o, k, "%s 的型別是%s" % (o, k)) except AssertionError as e: print(e) # assertRaises()方法例項 def test_assertraises(self): # 測試丟擲指定的異常型別 # assertRaises(exception) with self.assertRaises(TypeError) as exc: random.sample([1, 2, 3, 4, 5, 6], "j") # 列印詳細的異常資訊 print(exc.exception) # assertRaises(exception, callable, *args, **kwds) try: self.assertRaises(ZeroDivisionError, ToBeTest.div, 3, 0) except ZeroDivisionError as e: print(e) # assertRaisesRegexp()方法例項 def test_assertraisesregex(self): # 測試丟擲指定的異常型別,並用正則表示式去匹配異常資訊 # assertRaisesRegex(exception, regexp) with self.assertRaisesRegex(ValueError, "literal") as exc: int("abc") # 列印詳細的異常資訊 print(exc.exception) # assertRaisesRegex(exception, regexp, callable, *args, **kwds) try: self.assertRaisesRegex(ValueError, 'invalid literal for.*\'abc\'$', int, 'abc') except AssertionError as e: print(e) if __name__ == '__main__': unittest.main()