【第八章】参数化测试引入 paramunittest(类似ddt),优化脚本是否执行用例
-
参数化测试引入 paramunittest
引言:
上一章节我们讲到的是断言跟添加异常模块,这一章节我们讲的是如何进行参数化实现
- 新建demo
unitest_paramtest_demo_12
# -*- coding: utf-8 -*- # @Time : 2021/12/22 13:22 # @Author : Limusen # @File : unitest_paramtest_demo_12 """ 安装 pip install paramunittest """ import unittest import paramunittest # # 元祖举例 # @paramunittest.parametrized( # (10, 20, 30), (30, 40, 70), (70, 80, 150) # ) # class ParamUnittestDemo12(paramunittest.ParametrizedTestCase): # # def setParameters(self, numa, numb, numc): # self.a = numa # self.b = numb # self.c = numc # # def test_add_case(self): # print("%s + %s ?= %s" % (self.a, self.b, self.c)) # self.assertEqual(self.a + self.b, self.c) # 字典举例 @paramunittest.parametrized( {"numa": 20, "numb": 30, "numc": 50}, {"numa": 40, "numb": 60, "numc": 100}, {"numa": 80, "numb": 110, "numc": 190} ) class PUnitTestDemo(paramunittest.ParametrizedTestCase): # unittest.TestCase def setParameters(self, numa, numb, numc): self.a = numa self.b = numb self.c = numc def test_add_case(self): print("%d + %d ?= %d" % (self.a, self.b, self.c)) self.assertEqual(self.a + self.b, self.c) if __name__ == '__main__': unittest.main(verbosity=2)
![](https://img2020.cnblogs.com/blog/1242217/202112/1242217-20211222133801434-365463356.png)
- 优化读取excel文件格式
# -*- coding: utf-8 -*- # @Time : 2021/12/22 13:22 # @Author : Limusen # @File : unitest_paramtest_demo_12 """ 安装 pip install paramunittest """ import unittest import paramunittest # # 元祖举例 # @paramunittest.parametrized( # (10, 20, 30), (30, 40, 70), (70, 80, 150) # ) # class ParamUnittestDemo12(paramunittest.ParametrizedTestCase): # # def setParameters(self, numa, numb, numc): # self.a = numa # self.b = numb # self.c = numc # # def test_add_case(self): # print("%s + %s ?= %s" % (self.a, self.b, self.c)) # self.assertEqual(self.a + self.b, self.c) # # 字典举例 # @paramunittest.parametrized( # {"numa": 20, "numb": 30, "numc": 50}, # {"numa": 40, "numb": 60, "numc": 100}, # {"numa": 80, "numb": 110, "numc": 190} # ) # class PUnitTestDemo(paramunittest.ParametrizedTestCase): # unittest.TestCase # # def setParameters(self, numa, numb, numc): # self.a = numa # self.b = numb # self.c = numc # # def test_add_case(self): # print("%d + %d ?= %d" % (self.a, self.b, self.c)) # self.assertEqual(self.a + self.b, self.c) cast_info = [ {"case_id": "api_case_01", "case_step": ".....1"}, {"case_id": "api_case_02", "case_step": ".....2"}, {"case_id": "api_case_03", "case_step": ".....3"} ] # 整合测试用例 @paramunittest.parametrized( *cast_info ) class PUnitTestDemo(paramunittest.ParametrizedTestCase): # unittest.TestCase def setParameters(self, case_id, case_step): self.case_id = case_id self.case_step = case_step def test_add_case(self): print("case_id: %s ,case_step: %s " % (self.case_id, self.case_step)) assert True if __name__ == '__main__': unittest.main(verbosity=2)
![](https://img2020.cnblogs.com/blog/1242217/202112/1242217-20211222134628454-1526122382.png)
-
添加测试类,新增运行用例方法
- 新增testcase包
- 新建test_api_case.py
# -*- coding: utf-8 -*- # @Time : 2021/12/22 13:47 # @Author : Limusen # @File : test_api_case import unittest import paramunittest from common.testcase_data_utils import TestCaseDataUtils from common.request_utils import RequestsUtils testcase_data_lists = TestCaseDataUtils().convert_data_info_list() @paramunittest.parametrized( *testcase_data_lists ) class TestApiCase(paramunittest.ParametrizedTestCase): def setParameters(self, case_id, case_step): self.case_id = case_id self.case_step = case_step def test_api_case(self): test_result = RequestsUtils().request_steps(self.case_step) self.assertTrue(test_result["check_result"]) if __name__ == '__main__': unittest.main(verbosity=2)
![](https://img2020.cnblogs.com/blog/1242217/202112/1242217-20211222135825518-1816332926.png)
![](https://img2020.cnblogs.com/blog/1242217/202112/1242217-20211222135536034-1294857476.png)
-
优化excel是否执行用例
- 将excel是否执行用例改成否
- 在数据转换的时候判断如果为是的话执行,为否的话不执行
- 修改testcase_data_utils.py
# -*- coding: utf-8 -*- # @Time : 2021/12/9 13:42 # @Author : Limusen # @File : testcase_data_utils import os from common.excel_utils import ExcelUtils current = os.path.dirname(os.path.abspath(__file__)) excel_file_path = os.path.join(current, '..', 'testcase_data', 'testcase_infos.xlsx') excel_sheet_name = "Sheet1" class TestCaseDataUtils: def __init__(self): self.ex = ExcelUtils(excel_file_path=excel_file_path, sheet_name=excel_sheet_name) def convert_data_info_dict(self): """ 将测试数据转换成字典 :param testcase_info: :return: """ test_case_dict = {} for testcase in self.ex.get_all_excel_data(): if testcase["用例执行"] == "是": # 新增代码 test_case_dict.setdefault(testcase['测试用例编号'], []).append(testcase) return test_case_dict def convert_data_info_list(self): test_case_list = [] for key, value in self.convert_data_info_dict().items(): type_dict = {} type_dict["case_id"] = key type_dict["case_step"] = value test_case_list.append(type_dict) return test_case_list if __name__ == '__main__': print(TestCaseDataUtils().convert_data_info_list())
![](https://img2020.cnblogs.com/blog/1242217/202112/1242217-20211222140453179-57064107.png)
- 总结
这一章节主要引入了paramunittest参数化模块,跟优化的是否执行用例的代码部分,下一章讲解引入测试报告模块