1、断言
1)、self.assertEqual(2,res)#期望值qian、结果值hou
2)TextTestRunner 源码 必要的参数
3)测试结果 上下文管理器
with open("test.txt",'w+',encoding='utf-8') as file:
runner= unittest.TextTestRunner(stream=file,verbosity=2)
runner.run(suite)
2、测试报告的另一种:
import unittest import HTMLTestRunner from GYP_test.class_01 import TestMathMethon suite= unittest.TestSuite()#存储用例 loader = unittest.TestLoader()#加载器 suite.addTest(loader.loadTestsFromTestCase(TestMathMethon)) with open('test_report.html','wb') as file: runner= HTMLTestRunner.HTMLTestRunner(stream=file,verbosity=2, title='gyp测试', description='gyp在做练习', tester='卡卡') runner.run(suite)
断言处理:
异常抛出
import unittest from GYP_test.math import MathMethod #测试的目标 class TestMathMethon(unittest.TestCase):#继承了unittest里面的TestCase 专门来写用例 def test_add_two_positive(self): res = MathMethod(1,1).add() print('1+1的结果值是:',res) #加一个断言:判断期望值与实际值的比对结果一直就算通过 不一致 就算失败 try: self.assertEqual(2,res)#期望值qian、结果值hou except AssertionError as e: print("出错了拉%s" %e) raise e def test_add_two_zero(self): res = MathMethod(0, 0).add() print('0+0的结果值是:', res) try: self.assertEqual(1, res) # 期望值、结果值 except AssertionError as e: print("出错了拉%s" %e) raise e def test_add_two_fushu(self): res = MathMethod(-1, -2).add() print('-1+-2的结果值是:',res) try: self.assertEqual(-3, res) # 期望值、结果值 except AssertionError as e: print("出错了拉%s" %e) raise e if __name__ == '__main__': unittest.main()
4、夹心饼干
初始化操作
def setUp(self): print('我要开始执行用例了') def tearDown(self): print('我已经执行完了')