断言
断言
断言内容是自动化脚本的重要内容,正确设置断言以后才能帮助我们判断测试用例执行结果。
断言方法
- assertEqual(a, b) 判断a==b
- assertNotEqual(a, b) 判断a!=b
- assertTrue(x) bool(x) is True
- assertFalse(x) bool(x) is False
- assertIs(a, b) a is b
- assertIsNot(a, b) a is not b
- assertIsNone(x) x is None
- assertIsNotNone(x) x is not None
- assertIn(a, b) a in b
- assertNotIn(a, b) a not in b
- assertIsInstance(a, b) isinstance(a, b)
- assertNotIsInstance(a, b) not isinstance(a, b)
测试案例脚本的编写:
#/usr/bin python #-*- coding:UTF-8 -*- from calculator import Math import unittest #测试用例继承TestCase类 class TestMath(unittest.TestCase): def setUp(self): print("test start") def test_add(self): j = Math.add(1,2) self.assertEqual(j,3) #self.assertNotEqual(j,4) print("######test_add######") def test_add1(self): j = Math.add(1,2) self.assertTrue(j>1) print("######test_add1######") def assertIs_test(self): self.assertIs("welcome","welcome to beijing") print("######assertIs_test######") def assertIn_test(self): self.assertIn("welcome","welcome to beijing") print("######assertIs_test######") def tearDown(self): print("test end") if __name__ == '__main__': ##构造测试集 suite = unittest.TestSuite() suite.addTest(TestMath("test_add")) suite.addTest(TestMath("test_add1")) suite.addTest(TestMath("assertIn_test")) suite.addTest(TestMath("assertIs_test")) ##执行测试 runner = unittest.TextTestRunner() runner.run(suite)
测试的python源码
#/usr/bin python #-*- coding:UTF-8 -*- ##计算模块## class Math: #用于存放初始化数据 def __int__(self,a,b): self.a = a self.b = b #待测的方法 def add(a,b): return a+b
 
                    
                
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号