unittest测试框架_2_断言设置
在执行测试用例的过程中,用例是否执行通过,需要判断测试得到的实际结果与预期结果是否相等决定的。以下是一些常用的断言操作:
assertEqual(a,b,msg=None) 判断a是否等于b,等于则断言通过
assertNotEqual(a,b,msg=None)判断a是否不等于b,等于则断言通过
assertTrue(a,msg=None) 判断a是否为True,等于则断言通过
assertFalse(a,msg=None)判断a是否为False,等于则断言通过
assertIn(a,b,msg=None) 判断 b 是否包含 a,等于则断言通过
assertNotIn(a,b,msg=None)判断 b 是否不包含 a,等于则断言通过
assertIs(a,b,msg=None) 判断 b 是否是 a,等于则断言通过
assertIsNot(a,b,msg=None) 判断 b 是否不是 a,等于则断言通过
assertIsNone(a,msg=None)判断a是否为None,等于则断言通过
assertIsNotNone(a,b,msg=None)判断a是否不为None,等于则断言通过
from cgi_projects.selenum_test.count import Count from cgi_projects.selenum_test.count import Torf import unittest class TestCount(unittest.TestCase): def setUp(self): print("start") def test_add(self): j = Count(2, 3) # assertEqual(a,b):检查a == b self.assertEqual(j.add(), 5, msg="assertEqual: no equal") def test_add2(self): j = Count(44, 90) self.assertNotEqual(j.add(), 135, msg="assertNotEqual:no equal") def test_zero(self): self.assertTrue(Torf(0).is_zero(), msg="assertTrue: result is true") def test_numb(self): self.assertFalse(Torf('a').is_numb(), msg="assertFalse: result is False") def test_case1(self): a = "hello" b = "hello word" self.assertIn(a, b, msg= "assertIn: a in b ") def test_case2(self): a = "python" b = "java" self.assertNotIn(a, b, msg="assertNotIn: a not in b ") def test_case3(self): a = 1 b = 1 self.assertIs(a, b, msg="assertIs: a is b") def test_case4(self): a = 1 b = 3 self.assertIsNot(a, b, msg="assertIsNot: a is not b") def test_case5(self): a = None self.assertIsNone(a, msg="assertIsNone: a is none") def test_case6(self): a = 'b' self.assertIsNotNone(a, msg="assertIsNotNone: a is not None") # def test_case7(self): # a = Count(2, 3) # b = Count(3, 4) # # 判断 a 是否是 b 的一个实例 # self.assertIsInstance(a, b, msg="assertIsInstance: ") # def test_case8(self): # a = 'second' # b = "se" # # 判断 a 是否是 b 的一个实例 # self.assertNotIsInstance(a, b, msg="assertIsInstance: ") def tearDown(self): print("end") if __name__ == '__main__': suite = unittest.TestSuite() suite.addTest(TestCount("test_add")) suite.addTest(TestCount("test_add2")) suite.addTest(TestCount("test_zero")) suite.addTest(TestCount("test_numb")) suite.addTest(TestCount("test_case1")) suite.addTest(TestCount("test_case2")) suite.addTest(TestCount("test_case3")) suite.addTest(TestCount("test_case4")) suite.addTest(TestCount("test_case5")) suite.addTest(TestCount("test_case6")) # suite.addTest(TestCount("test_case7")) suite.addTest(TestCount("test_case8")) runner = unittest.TextTestRunner() runner.run()