def main(): # 连接数据库,获取连接实例对象 db = DB() # 从数据库中获取需要执行的api集合 api_list = db.get_api_list() for id, api in enumerate(api_list,1): #下标从1开始 api_id = api[0] api_name = api[1] req_url = api[2] req_method = api[3] parm_type = api[4] # print(api_id,api_name,req_url,req_method,parm_type) # 通过api_id获取它对应的测试用例 api_case_list = db.get_api_case_list(api_id) for idx,case in enumerate(api_case_list,1): case_id = case[0] request_data = eval(case[2]) if case[2] else {} # str转为dict为后续转json使用 rely_data = case[3] protocol_code = case[4] data_store = eval(case[6]) if case[6] else {} check_point = eval(case[7]) if case[7] else {} # print(case_id,request_data,rely_data,protocol_code,data_store,check_point) info("第[%s]个接口,接口名是[%s],请求url是[%s],请求方式是[%s],参数类型是[%s]" % (id, api_name, req_url, req_method, parm_type)) info("接口[%s]的第[%s]条用例,请求参数是%s, 依赖数据是%s" %(api_name,idx,request_data,rely_data)) # 接下来进行数据依赖的处理 if rely_data: request_data = GetRely.get(request_data,eval(rely_data)) info("接口[%s]的第[%s]条用例,依赖数据处理后的请求参数是%s" % (api_name, idx, request_data)) else: info("接口[%s]的第[%s]条用例不需要处理依赖数据!" % (api_name,idx)) #接下来进行接口请求,并获取响应body hc = HttpClient() responseObj = hc.request(req_url,req_method,parm_type,request_data) # 接下来进行数据依赖存储 code = responseObj.status_code info("接口[%s]的第[%s]条用例,实际响应结果是%s" %(api_name,idx,responseObj.json())) if code == int(protocol_code): # 进行数据依赖存储 if data_store: RelyDataStore.do(data_store,api_name,case_id,request_data,responseObj.json()) info("接口[%s]的第[%s]条用例的依赖数据存储成功!" %(api_name,idx)) else: info("接口[%s]的第[%s]条用例的不需要依赖数据存储!" %(api_name,idx)) # 接下来进行结果校验 error_info = CheckResult.check(responseObj.json(),check_point) if error_info: debug("接口[%s]的第[%s]条用例校验结果失败,错误信息:%s" % (api_name,idx,error_info)) else: info("接口[%s]的第[%s]条用例运行成功!" %(api_name,idx)) else: debug("接口[%s]的第[%s]条用例响应协议code=%s,不是期望值code=%s!" %(api_name,idx,code,protocol_code))
def main(): # 连接数据库,获取连接实例对象 db = DB() # 从数据库中获取需要执行的api集合 api_list = db.get_api_list() for id, api in enumerate(api_list, 1): api_id = api[0] api_name = api[1] req_url = api[2] req_method = api[3] parm_type = api[4] # 通过api_id获取它对应的测试用例 api_case_list = db.get_api_case(api_id) for idx, case in enumerate(api_case_list, 1): case_id = case[0] request_data = eval(case[2]) if case[2] else {} rely_data = case[3] protocol_code = case[4] data_store = eval(case[6]) if case[6] else {} check_point = eval(case[7]) if case[7] else {} # 接下来进行数据依赖的处理 if rely_data: rely_data = eval(rely_data) request_data = GetRely.get(request_data, rely_data) else: info("接口[%s]的第%s用例不需要依赖数据!" % (api_name, idx)) # 接下来进行接口请求,并获取响应body hc = HttpClient() responseObj = hc.request(req_url, req_method, parm_type, request_data) code = responseObj.status_code info("接口[%s]的第%s用例的实际响应结果为:%s" % (api_name, idx, responseObj.json())) if code == int(protocol_code): # 进行数据依赖存储 RelyDataStore.do(data_store, api_name, case_id, request_data, responseObj.json()) info("接口[%s]的第%s用例的依赖数据存储成功!" % (api_name, idx)) # 接下来进行结果校验 error_info = CheckResult.check(responseObj.json(), check_point) if error_info: debug("接口[%s]的第%s用例校验结果失败,错误信息:%s" % (api_name, idx, error_info)) else: info("接口[%s]的第%s用例运行成功!" % (api_name, idx)) else: info("接口[%s]的第%s用例响应协议code=%s,不是期望值code=%s" % (api_name, idx, code, protocol_code))
def main(): # 新建一个解析excel工具类的实例对象 parseE = ParseExcel() parseE.loadWorkBook(FILEPATH) sheetObj = parseE.getSheetByName("API") #print(sheetObj) # 获取active列的列对象 activeList = parseE.getColumn(sheetObj, API_active) #下面第一层遍历是为了读取excel主表api信息,读取测试用例 for idx, cell in enumerate(activeList[1:], 2): if cell.value == "y": # 需要执行的接口所在的行的对象 rowObj = parseE.getRow(sheetObj, idx) apiName = rowObj[API_apiName - 1].value requestUrl = rowObj[API_requestUrl - 1].value requestMethed = rowObj[API_requestMothod - 1].value paramsType = rowObj[API_paramsType - 1].value apiTestCaseFileName = rowObj[API_apiTestCaseFileName - 1].value # 下一步需要读取接口用例表,获取接口的测试用例 caseSheetObj = parseE.getSheetByName(apiTestCaseFileName) # obj类型 caseActiveObj = parseE.getColumn(caseSheetObj, CASE_active) # tuple类型 print(caseActiveObj) #第二层for循环是读取每个测试用例(注册接口用例,登录接口用例, 查询博文用例)里面的信息 for c_idx, c_cell in enumerate(caseActiveObj[1:], 2): if c_cell.value == "y": # 说明此case需要被执行 caseRowObj = parseE.getRow(caseSheetObj, c_idx) requestData = caseRowObj[CASE_requestData - 1].value requestData = eval(requestData) if requestData else {} relyData = caseRowObj[CASE_relyData - 1].value responseCode = caseRowObj[CASE_responseCode - 1].value dataStore = caseRowObj[CASE_dataStore - 1].value checkPoint = caseRowObj[CASE_checkPoint - 1].value print(requestData, relyData, responseCode, dataStore, checkPoint) #接口发送请求之前需要先做数据依赖处理 if relyData: requestData = GetRely.get(requestData,eval(relyData)) #构造接口欧请求需要的数据,并调用HttpClient类中的方法发送接口,获取响应结果 hc = HttpClient() response= hc.request(requestUrl,requestMethed,paramsType,requestData) if response.status_code == responseCode: responseBody = response.json() #print(responseBody) #获取到接口响应的body后,接下来处理依赖数据的存储 if dataStore: RelyDataStore.do(eval(dataStore),apiName,c_idx-1,requestData,responseBody) else: print("第%s个接口的第%s条用例不需要存储数据依赖" %(idx - 1, c_idx - 1)) #接下来进行接口响应结果的校验 if checkPoint: errorInfo = CheckResult.Check(responseBody,eval(checkPoint)) WriteTestResult.write(parseE,caseSheetObj,responseBody,errorInfo,c_idx) # if errorInfo: # print("第%s个接口的第%s条校验失败,错误与信息:%s" %(idx - 1, c_idx - 1,errorInfo)) else: print("第%s个接口的第%s条用例未设置校验点" %(idx - 1, c_idx - 1)) else: print("第%s个接口的第%s条用例所调用的接口响应失败,响应code=%s,而预期响应code=%s" %(idx - 1, c_idx - 1,response.status_code,\ responseCode)) else: print("第%s个接口的第%s条用例被忽略执行!" %(idx - 1, c_idx - 1)) else: print("第%s个接口被设置忽略执行!" %(idx - 1))
def main(): #file_path=r"D:\20170721exercise\interfaceFrame201807\TestData\inter_test_data.xlsx" parseE = ParseExcel() parseE.loadWorkBook(file_path) sheetObj = parseE.getSheetByName(u"API") #print sheetObj activeList = parseE.getColumn(sheetObj, 7) #print activeList #for i in activeList: #print i.value #外层for循环,遍历的api的sheet表 for idx, cell in enumerate(activeList[1:],2): #print idx if cell.value == "y": # 需要执行的接口所在行的行对象 rowObj = parseE.getRow(sheetObj, idx) #print type(rowObj) apiName = rowObj[API_apiName-1].value requestUrl = rowObj[API_requestUrl-1].value requestMethod = rowObj[API_requestMothod-1].value paramsType = rowObj[API_paramsType-1].value apiTestCaseFileName = rowObj[API_apiTestCaseFileName-1].value #print apiName, requestUrl, requestMothod, paramsType, apiTestCaseFileName # 下一步读用例sheet表,准备执行测试用例 caseSheetObj = parseE.getSheetByName(apiTestCaseFileName) caseActiveObj = parseE.getColumn(caseSheetObj, CASE_active) # for i in caseActiveObj: # print i.value #内层for循环,遍历的测试用例的sheet for c_idx, col in enumerate(caseActiveObj[1:], 2): if col.value == "y": # 说明此case行需要执行 caseRowObj = parseE.getRow(caseSheetObj, c_idx) requestData = caseRowObj[CASE_requestData - 1].value relyData = caseRowObj[CASE_relyData - 1].value # print requestData # print relyData dataStore=caseRowObj[CASE_dataStore-1].value checkPoint=caseRowObj[CASE_checkPoint-1].value if relyData: #发送接口请求之前,先做依赖数据的处理 requestData = "%s" %GetKey.get(eval(requestData),eval(relyData)) #print type(requestData) #print requestData # 拼接接口请求参数,发送接口请求 #print type(requestData) httpC = HttpClient() #print requestMethod, requestUrl, paramsType, requestData response = httpC.request(requestMethod=requestMethod,requestUrl=requestUrl,paramsType=paramsType,requestData=requestData) if response.status_code==200: responseData = response.json() #存储依赖数据 if dataStore: RelyDataStore.do(eval(dataStore),apiName,c_idx-1,eval(requestData),responseData) #比对结果 errorKey = CheckResult.check(responseData,eval(checkPoint)) write_result(parseE,caseSheetObj,responseData,errorKey,c_idx) # if not errorKey: # print "校验结果通过" # else: # print "校验失败",errorKey else: print response.status_code else: print "用例被忽略执行" else: print "接口被设置忽略执行"
def main(): # 新建一个解析excel工具类的实例对象 parseE = ParseExcel() parseE.loadWorkBook(FILEPATH) sheetObj = parseE.getSheetByName("API") # 获取active列的列对象 activeList = parseE.getColumn(sheetObj, API_active) for idx, cell in enumerate(activeList[1:], 2): if cell.value == "y": # 需要执行的接口所在行的行对象 rowObj = parseE.getRow(sheetObj, idx) apiName = rowObj[API_apiName - 1].value requestUrl = rowObj[API_requestUrl - 1].value requestMethed = rowObj[API_requestMothod - 1].value paramsType = rowObj[API_paramsType - 1].value apiTestCaseFileName = rowObj[API_apiTestCaseFileName - 1].value # 下一步需要读取接口用例表,获取接口的测试用例 caseSheelObj = parseE.getSheetByName( apiTestCaseFileName) # obj数据类型 caseActiveObj = parseE.getColumn(caseSheelObj, CASE_active) # tuple 数据类型 for c_idx, c_cell in enumerate(caseActiveObj[1:], 2): if c_cell.value == "y": # 说明此case行需要被执行 caseRowObj = parseE.getRow(caseSheelObj, c_idx) requestData = caseRowObj[CASE_requestData - 1].value requestData = eval(requestData) if requestData else {} relyData = caseRowObj[CASE_relyData - 1].value responseCode = caseRowObj[CASE_responseCode - 1].value dataStore = caseRowObj[CASE_dataStore - 1].value checkPoint = caseRowObj[CASE_checkPoint - 1].value # 接口发送请求之前需要先做依赖数据的处理 if relyData: requestData = GetRely.get(requestData, eval(relyData)) # 构造接口请求需要的数据 httpC = HttpClient() response = httpC.request(requestUrl, requestMethed, paramsType, requestData) http_code = response.status_code print(response.json()) #下面是处理依赖数据存储 if http_code == responseCode: if dataStore: RelyDataStore.do(eval(dataStore), apiName, c_idx - 1, requestData, response.json()) else: print("第%s个接口的第%s条用例不需要依赖数据存储!") else: print("第%s个接口的第%s条用例的协议code码【%s】不符合预期值【%s】" % (idx - 1, c_idx - 1, http_code, responseCode)) # 下面进行校验点检测功能的实现 if checkPoint: errorInfo = CheckResult.check(response.json(), eval(checkPoint)) write_result(parseE, caseSheelObj, response.json(), errorInfo, c_idx) else: print("第%s个接口的第%s条用例没有设置校验点!" % (idx - 1, c_idx - 1)) else: print("第%s个接口的第%s条用例被忽略执行!" % (idx - 1, c_idx - 1)) else: print("第%s个接口被设置忽略执行!" % (idx - 1))
# 获取响应 response = HttpClient.request(requestUrl, requestMethod, paramType, requestData) if response.status_code == 200: # 获取接口响应的body responseBody = response.json() # 做数据依赖存储 if dataStore: RelyDataStore.do(APIName, cell.row - 1, requestData, responseBody, eval(dataStore)) print(REQUEST_DATA) print(RESPONSE_DATA) # 接下来进行接口响应结果检测 if checkPoint: errorKey = CheckResult.check(response.json(), eval(checkPoint)) # 写响应结果,包括状态码和响应body testExcel.write_cell(cell.row, CASE_responseCode_col_no, response.status_code) testExcel.write_cell(cell.row, CASE_responseData_col_no, str(response.json())) # 写校验结果,包括校验结果以及错误信息 if errorKey: testExcel.write_cell(cell.row, CASE_status_col_no, "fail") testExcel.write_cell(cell.row, CASE_errorInfo_col_no, str(errorKey)) else: testExcel.write_cell(cell.row, CASE_status_col_no, "pass") else:
def main(): parseE = ParseExcel() parseE.loadWorkBook(file_path) sheetObj = parseE.getSheetByName(u"API") activeList = parseE.getColumn(sheetObj, API_active) print activeList for idx, cell in enumerate(activeList[1:], 2): if cell.value == "y": # 需要执行的接口所在行的行对象 rowObj = parseE.getRow(sheetObj, idx) apiName = rowObj[API_apiName - 1].value requestUrl = rowObj[API_requestUrl - 1].value requestMethod = rowObj[API_requestMothod - 1].value paramsType = rowObj[API_paramsType - 1].value apiTestCaseFileName = rowObj[API_apiTestCaseFileName - 1].value # 下一步读用例sheet表,准备执行测试用例 caseSheetObj = parseE.getSheetByName(apiTestCaseFileName) caseActiveObj = parseE.getColumn(caseSheetObj, CASE_active) for c_idx, col in enumerate(caseActiveObj[1:], 2): if col.value == "y": # 说明此case行需要执行 caseRowObj = parseE.getRow(caseSheetObj, c_idx) requestData = caseRowObj[CASE_requestData - 1].value relyData = caseRowObj[CASE_relyData - 1].value dataStore = caseRowObj[CASE_dataStore - 1].value checkPoint = caseRowObj[CASE_checkPoint - 1].value headers = caseRowObj[CASE_headers - 1].value print headers if relyData: # 发送接口请求之前,先做依赖数据的处理 requestData = "%s" %GetKey.get(eval(requestData), eval(relyData)) # 拼接接口请求参数,发送接口请求 httpC = HttpClient() # print requestMethod, requestUrl, paramsType, requestData response = httpC.request(requestMethod = requestMethod, requestUrl = requestUrl, paramsType = paramsType, requestData = requestData, headers=headers ) print response.text if response.status_code == 200: responseData = response.json()#对比json responseText = response.text # 存储依赖数据 if dataStore: RelyDataStore.do(eval(dataStore),apiName, c_idx - 1, eval(requestData),responseData) # 比对结果 errorKey = CheckResult.check(responseData, eval(checkPoint)) print type(responseData) write_result(parseE, caseSheetObj,responseText, errorKey, c_idx)#讲返回的报文体填入结果 else: responseData = response.json() responseText = response.text print "错误json%s"%responseData errorKey = CheckResult.check(responseData, eval(checkPoint)) print responseData,type(eval(checkPoint)) write_result(parseE, caseSheetObj, responseText,errorKey,c_idx) print errorKey print responseData else: print "用例被忽略执行" else: print "接口被设置忽略执行"
def main(): # 实现从获取接口测试数据到发送情况,再到获取返回结果,并处理结果 # 创建ParseExcel类的实例对象 parseE = ParseExcel() # 调用封装好的加载excel到内存的方法将需要解析的excel加载到内存 parseE.loadWorkBook(excelPath) # 获取“API”表的表对象 sheetObj = parseE.getSheetByName(apiExcelName) #print(sheetObj) # 获取API表中是否需要执行api自动化case的列对象 activeList = parseE.getColumn(sheetObj, API_active) #print(activeList) # 遍历是否需要执行标记列,只执行标记为y的api自动化测试 for idx, cell in enumerate(activeList[1:], 2): #print(idx, cell.value) if cell.value == "y": # 需要执行的api rowObj = parseE.getRow(sheetObj, idx) apiName = rowObj[API_apiName - 1].value requestUrl = rowObj[API_requestUrl - 1].value requestMethod = rowObj[API_requestMothod - 1].value paramsType = rowObj[API_paramsType - 1].value apiTestCaseSheetName = rowObj[API_apiTestCaseFileName - 1].value #print(apiName, requestUrl, requestMethod, paramsType, apiTestCaseSheetName) # 下一步就是获取api的测试case,然后准备执行用例 caseSheetObj = parseE.getSheetByName(apiTestCaseSheetName) caseActiveObj = parseE.getColumn(caseSheetObj, CASE_active) print(caseActiveObj) for c_idx, col in enumerate(caseActiveObj[1:], 2): if col.value == 'y': # 说明当前case是需要被执行的 caseRowObj = parseE.getRow(caseSheetObj, c_idx) requestsData = caseRowObj[CASE_requestData - 1].value #print(requestsData) relyData = caseRowObj[CASE_relyData - 1].value dataStore = caseRowObj[CASE_dataStore - 1].value checkPoint = caseRowObj[CASE_checkPoint - 1].value # 下一步,在发送接口请求之前,需要处理数据依赖 if relyData: # 需要进行数据依赖处理 RequestData = GetRelyValue.get(requests, relyData) print(RequestData) else: print("第%s个API的第%s条不需要做数据依赖处理!" % ((idx - 1), (c_idx - 1))) if requestData[0] == "{" and requestData[-1] == "}": # 说明请求参数是一个json串格式数据 requestData = eval(requestData) # 处理完接口请求参数的依赖数据后,接下来就是发送请求并获取响应结果 response = HttpClient.request(requestUrl, requestMethod, paramsType, requestData) print(response.status_code) print(response.json()) # 下一步,根据接口响应结果,做数据依赖存储以及结果检测 if response.status_code == "200": # 获取接口响应body responseBody = response.json() # 接下来做数据依赖存储 if dataStore: RelyDataStore.do(apiName, c_idx - 1, requestData, responseBody, eval(dataStore)) # 接下来进行接口响应结果检测 if checkPoint: errorKey = CheckResult.check(response.json(), eval(checkPoint)) # 将测试结果写回excel write_result(parseE, caseSheetObj, response.json(), errorKey, c_idx) else: print("第%s个API的第%s条case被忽略执行!" % ((idx - 1), (c_idx - 1))) else: print("第%s个API不需要执行" % (idx - 1))