标签:unit nsf template parameter 插件 失败 rbo cas elf
unittest本身不支持参数化,但是可以借助其他方法
1、unittest中使用第3方插件parameterized进行参数化
首先在DOS中键入以下命令安装parameterized
python -m pip install parameterized
导包:
from parameterized import parameterized
例:
1 import unittest 2 from pylibrary.PyLib import * 3 4 class Interface_Report2(unittest.TestCase): 5 6 #开始使用parameterized 7 @parameterized.expand([ #引用装饰器,准备参数,传入下方的函数 8 ("2018-09-26", ‘2018-09-26‘, "测试", "2001",49,‘TotalCallNum‘), 9 ("2018-09-26", ‘2018-09-26‘, "测试", "2001",60,‘TotalCallAnsweredNum‘), 10 ("2018-09-26", ‘2018-09-26‘, "测试", "2001",60,‘RingNum‘), 11 ("2018-09-13", ‘2018-09-13‘, "测试", "2001",4,‘TotalCallNum_Transfer‘), 12 ]) 13 14 15 def test_Agreport1(self,start_time,end_time,template_name,ag_num,testData,reportData): #当执行到此会自动接收上面传入的参数 16 self.num = testData 17 self.report_num1 = PyLib().getAgReport(start_time,end_time,template_name,ag_num) 18 self.report_num=self.report_num1[0][reportData] 19 self.assertEqual(self.num,self.report_num) 20 21 if __name__ == ‘__main__‘: 22 23 unittest.main(verbosity=2) 24 25 #verbosity表示测试结果的信息复杂度,分别使用0,1,2,其具体的含义如下: 26 #0(静默模式):仅仅获取总的测试用例数以及总的结果 27 #1(默认模式):此时在每个成功的用例前面有个’.’,每个失败的用例前面有个’F’ 28 #2(详细模式):测试结果会显示每个测试用例的所有相关信息
2、unittest的ddt库
pip install ddt
首先创建一个数据文件:test_ddt_file.json
1 { 2 "test_Agreport1_01":{ 3 "start_time":"2018-09-26", 4 "end_time":"2018-09-26", 5 "template_name": "测试", 6 "ag_num":"2001", 7 "testData":49, 8 "reportData":"TotalCallNum" 9 }, 10 "test_Agreport1_02":{ 11 "start_time":"2018-09-26", 12 "end_time":"2018-09-26", 13 "template_name": "测试", 14 "ag_num":"2001", 15 "testData":60, 16 "reportData":"TotalCallAnsweredNum" 17 }, 18 "test_Agreport1_03":{ 19 "start_time":"2018-09-26", 20 "end_time":"2018-09-26", 21 "template_name": "测试", 22 "ag_num":"2001", 23 "testData":60, 24 "reportData":"RingNum" 25 }, 26 "test_Agreport1_04":{ 27 "start_time":"2018-09-13", 28 "end_time":"2018-09-13", 29 "template_name": "测试", 30 "ag_num":"2001", 31 "testData":4, 32 "reportData":"TotalCallNum_Transfer" 33 } 34 }
创建测试用例:
1 import unittest 2 from pylibrary.PyLib import * 3 from ddt import ddt, file_data 4 5 6 @ddt 7 class Interface_Report2(unittest.TestCase): 8 9 @file_data("D:\\Python_code\\test_ddt_file.json") 10 def test_Agreport1(self,start_time,end_time,template_name,ag_num,testData,reportData): 11 self.num = testData 12 self.report_num1 = PyLib().getAgReport(start_time,end_time,template_name,ag_num) 13 self.report_num=self.report_num1[0][reportData] 14 self.assertEqual(self.num,self.report_num) 15 16 17 18 if __name__ == ‘__main__‘: 19 20 unittest.main(verbosity=2)
标签:unit nsf template parameter 插件 失败 rbo cas elf
原文地址:https://www.cnblogs.com/Mollylin/p/9765430.html