标签:多个 elf proc 测试 case desc ssi pytho tin
Unitest是python里面的单元测试框架,方便组建测试用例,执行用例,并生成测试报告
1 import unittest 2 3 class TestCase(unittest.TestCase): 4 5 def testPlus(self): 6 a = 2 7 b = 3 8 c = a + b 9 self.assertEqual(c,5) 10 11 def testReduce(self): 12 a = 5 13 b = 3 14 c = a - b 15 self.assertEqual(c,2) 16 17 if __name__ == ‘__main__‘: 18 unittest.main()
前置条件setUp(self):每个用例执行前,执行一次
后置条件teardown(self):每个用例执行后,执行一次
1 import unittest 2 3 class TestCase(unittest.TestCase): 4 5 def setUp(self): 6 print("每个用例执行前执行一次") 7 8 def tearDown(self): 9 print("每个用例执行后执行一次") 10 def test001(self): 11 print("测试用例001") 12 def test002(self): 13 print("测试用例002") 14 15 if __name__ == ‘__main__‘: 16 unittest.main()
结果:
1 Testing started at 13:08 ... 2 每个用例执行前执行一次 3 测试用例001 4 每个用例执行后执行一次 5 每个用例执行前执行一次 6 测试用例002 7 每个用例执行后执行一次 8 9 Process finished with exit code 0
前置setUpClass:只执行一次前置,必须加@classmethod
后置tearDownClass:只执行一次结束,必须加@classmethod
例:
1 import unittest 2 3 class TestCase(unittest.TestCase): 4 5 @classmethod 6 def setUpClass(cls): 7 print("只开始执行一次") 8 @classmethod 9 def tearDownClass(cls): 10 print("只结束执行一次") 11 12 def setUp(self): 13 print("每个用例执行前执行一次") 14 15 def tearDown(self): 16 print("每个用例执行后执行一次") 17 def test001(self): 18 print("测试用例001") 19 def test002(self): 20 print("测试用例002") 21 22 if __name__ == ‘__main__‘: 23 unittest.main()
结果:
1 Testing started at 13:12 ... 2 只开始执行一次 3 每个用例执行前执行一次 4 测试用例001 5 每个用例执行后执行一次 6 每个用例执行前执行一次 7 测试用例002 8 每个用例执行后执行一次 9 只结束执行一次 10 11 Process finished with exit code 0
项目框架
一级目录:项目名称
二级文件夹:report
二级文件夹:test_cases
可以建三级子文件夹
(待执行的脚本用test开头)
二级文件夹:common
存放一些公共模块,如HTMLTestRunner.py
主函数:run_all.py
测试用例
1 #test_case\test_qqtest.py 2 3 import unittest 4 import requests 5 6 class QQtest(unittest.TestCase): 7 8 def setUp(self): 9 ‘‘‘测试前置‘‘‘ 10 self.url = "http://japi.juhe.cn/qqevaluate/qq" 11 self.headers={ 12 "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0" 13 } 14 self.s = requests.session() 15 16 def tearDown(self): 17 ‘‘‘清除数据‘‘‘ 18 self.s.close() 19 def test001(self): 20 ‘‘‘测试用例001‘‘‘ 21 self.params ={ 22 "qq":"741841851", 23 "key":"49c47ae3d1e8e3e49578f03cee1e7a7a" 24 } 25 r = self.s.get(self.url,params=self.params,headers=self.headers,verify=False) 26 result = r.json() 27 # print(result) 28 reason = result[‘reason‘]#获取接口状态 29 data = result[‘result‘][‘data‘][‘conclusion‘]#获取吉凶数据 30 self.assertEqual(reason,‘success‘) 31 32 def test002(self): 33 ‘‘‘测试用例002‘‘‘ 34 self.params ={ 35 "qq":"", 36 "key":"49c47ae3d1e8e3e49578f03cee1e7a7a" 37 } 38 r = self.s.get(self.url,params=self.params,headers=self.headers,verify=False) 39 result = r.json() 40 print(result) 41 reason = result[‘reason‘]#获取接口状态 42 # data = result[‘result‘][‘data‘][‘conclusion‘]#获取吉凶数据 43 self.assertEqual(reason,‘错误的请求参数‘) 44 45 if __name__ == ‘__main__‘: 46 unittest.main()
主函数:
1 run_all.py 2 3 import unittest 4 from common.HTMLTestRunner import HTMLTestRunner 5 import os 6 7 #获取当前运行的目录 8 dirpath = os.path.dirname(os.path.realpath(__file__)) 9 print("当前运行的目录是:%s " %dirpath) 10 11 #获取用例目录用join拼接 12 casepath = os.path.join(dirpath,"test_case") 13 14 print("用例目录是:%s" %casepath) 15 16 #获取报告的目录 17 reportpath=os.path.join(dirpath,‘report‘,‘report.html‘) 18 print("报告的目录是:%s" %reportpath) 19 20 #查找用例 21 all_cases=unittest.defaultTestLoader.discover(casepath,pattern=‘test*.py‘) 22 print(all_cases) 23 24 fp = open(reportpath,‘wb‘) 25 26 runner = HTMLTestRunner(stream=fp, 27 title=‘qq号测试吉凶自动化测试报告‘, 28 description=‘qq号测试吉凶‘, 29 ) 30 31 32 runner.run(all_cases)
报告
python接口自动化--unittest框架+HTMLTestRunner
标签:多个 elf proc 测试 case desc ssi pytho tin
原文地址:https://www.cnblogs.com/xiaoyujuan/p/11302474.html