In actual testing work, when doing interface automated testing, we often encounter data dependency problems between interfaces, that is, the request parameters of API_03 come from the response data of API_02, and the request parameters of API_02 come from the response data of API_01.
Therefore, when testing the API_03 interface through automated methods, you need to request the API_02 interface in advance to obtain the request parameters of API_03, and to obtain the response of API_02, you need to initiate a request to API_01 in advance and extract the request parameters of API_02 from the response.
The following automation framework design solves this problem:
For data-driven Excel table design
Framework code implementation logic
When testing the interface whose ID is shop-03, first determine whether it is running. If it is 'yes', determine the request type of the interface, and 'POST' OR 'GET' will take different logical branches.
When making a POST request, determine whether there is a [case dependency]. If [case dependency] is empty, there is no dependency. If it is not empty, the case id of the dependency is obtained.
In the above table, the case dependency of shop-03 is shop-02. After obtaining the dependency ID, the code logic actively determines the relevant field parameters of the shop-02 interface ([whether to run]/[request type]/[case dependency] ), if shop-02 also depends on the interface shop-01, the code continues to determine the corresponding field parameters of the shop-01 interface.
When the [case dependency] field of the shop-01 interface is empty, obtain [request data] according to the request data field to obtain the complete request parameters stored separately in the dictionary, initiate a request, obtain the response from shop-01, and then correspond according to shop-01 The [return data of the dependent interface] extracts the downstream interface.
That is, the dependency parameters required by shop-02 are stored in a dependency parameter list. The shop-02 interface substitutes the obtained parameters into the request parameters to make a request. After obtaining the response from shop-02, it also uses the [Dependent Interface] Return data] Extract the dependency parameters required by the downstream interface shop-03 and store them in the dependency parameter list. The shop-03 interface obtains this parameter from the list and substitutes it into the request parameter, enters the request, and obtains the response. According to the [actual result value field] Obtain the field to be asserted and complete the test of the interface compared with the expected results.
# 主逻辑模块部分代码示例
'''判断是否有case依赖,如果有case依赖则执行depend_data.py中的depend_response_data(self,caseid)方法,该方法返回该接口发起请求的依赖参数'''
elif method == 'Post':
if depend_caseid:
params_data_list = self.depend_result.depend_response_data(depend_caseid)
log_info('main_logic.requests_api::根据依赖的caseid={},'
'调用depend_data.depend_response_data获取到的依赖数据为:{}'.format(depend_caseid, params_data_list))
for j in range(len(depend_key_list)):
params[depend_key_list[j]] = params_data_list[j][0]
header = rquests_headers()
log_info('main_logic.requests_api::执行的用例{}请求url为:{},入参为:{},请求头为:{}'.format(caseid,url,params,header))
response = self.post_requests(url, params, header)
# print(response)
self.results_list.append(response)
else:
header = rquests_headers()
response =self.post_requests(url, params, header)
# print(response)
self.results_list.append(response)
#数据依赖模块部分代码示例
'''通过excel表中【case依赖】字段一层层向上游接口请求,结果储存在一个结果列表中供下游接口调用,最后返回测试接口所需要的依赖数据'''
def depend_response_data(self,caseid):
depend_caseid_t = self.excel_case_data.get_depend_caseid(caseid)
depend_caseid_list = []
#如果case依赖字段不为空
if depend_caseid_t:
#获取接口串联依赖caseid列表
while depend_caseid_t:
depend_caseid_list.append(depend_caseid_t)
depend_caseid_t = self.excel_case_data.get_depend_caseid(depend_caseid_t)
depend_caseid_list.reverse()
depend_caseid_list.append(caseid)
march_result_list = []
num_caseid = len(depend_caseid_list)
#迭代获取列表中的caseid
for i in range(num_caseid):
depend_caseid_s = self.excel_case_data.get_depend_caseid(depend_caseid_list[i])
depend_key_list = self.excel_case_data.get_depend_key(depend_caseid_list[i]).split(",")
params_key = self.excel_case_data.get_request_key(depend_caseid_list[i])
params = self.requests_data[params_key]
url = self.excel_case_data.get_url(depend_caseid_list[i])
#判断该caseid的测试用例中case依赖是否为空
if depend_caseid_s:
#获取上一个依赖接口返回的依赖参数
params_data_list = march_result_list[i-1]
#根据该caseid数据依赖字段构造该接口的传参
for k in range(len(depend_key_list)):
params[depend_key_list[k]] = params_data_list[k][0]
#对该接口发起请求
header = rquests_headers()
result = post_requests(url, params, header)
getdata = GetExcelDate()
is_depend_response = getdata.get_depend_response(depend_caseid_list[i])
#判断该接口是否需要为下一接口返回依赖参数
if is_depend_response:
depend_response_list = is_depend_response.split(",")
results = result
matchlist = depend_response_list
matchparams = match_params(results, matchlist)
march_result_list.append(matchparams)
else:
print("caseid={}不需要返回被依赖的字段".format(depend_caseid_list[i]))
#当该caseid测试用例中case依赖为空时直接请求该接口,返回匹配到的下一关联接口的请求参数
else:
result = self.depend_response(depend_caseid_list[i])
getdata = GetExcelDate()
is_depend_response = getdata.get_depend_response(depend_caseid_list[i])
if is_depend_response:
depend_response_list = is_depend_response.split(",")
results = result
matchlist = depend_response_list
matchparams = match_params(results, matchlist)
march_result_list.append(matchparams)
else:
print("caseid={}不需要返回被依赖的字段".format(depend_caseid_list[i]))
return matchparams
Finally, I would like to thank everyone who reads my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, if you can use it, you can take it directly:
This information should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey. I hope it can also help you!