1、生成(多个)对测试函数的参数化调用。实际上与装饰器pytest.mark.parametrize()作用一样(代码很挫,垃圾回收很滑稽)
def pytest_generate_tests(metafunc): """ 根据测试配置或定义测试函数的类或模块中指定的值生成测试用例, 在测试用例参数化收集前调用此钩子函数 :param metafunc:共有五个属性值 metafunc.fixturenames:参数化收集时的参数名称 metafunc.module:使用参数名称进行参数化的测试用例所在的模块d对象 metafunc.config:测试用例会话 metafunc.function:测试用例对象,即函数或方法对象 metafunc.cls: 测试用例所属的类的类对象 :return: none """ #print(metafunc.fixturenames) # 测试用例中有casename、caserequest、caseexpect这三个fixture时,开始为测试用例设置参数化 if __GetRegionCoutry_testdata: return if all(list(filter(is_in_fixturename, metafunc.fixturenames))) and (list(filter(is_in_fixturename, metafunc.fixturenames))): # 所有测试函数的参数化的测试数据(测试用例名字、测试用例的测试数据、测试用例的预期结果) all_case_parametrize = [] case_parametrize = [] # 正在运行的测试函数的函数名或方法名 current_function_name = metafunc.function.__qualname__ # 正在检测的测试函数所在模块的名字 current_function_in_module_name = metafunc.module # 从yaml中取出测试用例的测试数据 testdata_dict = __GetRegionCoutry_testdata #print(testdata_dict.keys()) # 从模块中提取不需要参数化的测试函数名字 testcase = current_function_in_module_name.__dict__ del testcase[current_function_name] if not testcase: raise NameError("testcase字典为空") not_parametrize_testcase = [key for key in testcase.keys() if "Rightrequest" in key] # 不需要参数化的测试函数所需的测试数据 #__GetRegionCoutry_testdata = [dict((i, testdata_dict[i])) for i in not_parametrize_testcase if i in testdata_dict.keys()] if not not_parametrize_testcase: raise NameError("not_parametrize_testcase字典为空") # print(not_parametrize_testcase) for i in not_parametrize_testcase: if i in testdata_dict.keys(): # 不需要参数化的测试数据 #print(i) __GetRegionCoutry_testdata[i] = copy.deepcopy(testdata_dict[i]) # 需要参数化的测试数据 del testdata_dict[i] #print(__GetRegionCoutry_testdata.keys()) #print("----------") #print(testdata_dict.keys()) for key, value in testdata_dict.items(): case_parametrize.clear() case_parametrize.append(key) case_parametrize.append(value["request"]) case_parametrize.append(value["expect"]) all_case_parametrize.append(tuple(case_parametrize)) else: if not all_case_parametrize: raise NameError("all_casez_parametrize列表为空") # 在测试用例运行前,对测试函数进行参数化设置 metafunc.parametrize("casename, caserequest, caseexpect", all_case_parametrize) print("---") # 垃圾回收 del testdata_dict del testcase del current_function_name del not_parametrize_testcase del case_parametrize del all_case_parametrize