def main(): parseE = ParseExcel() parseE.loadWorkBook(file_path) sheetObj = parseE.getSheetByName(u"API") 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 requestMethod = rowObj[API_requestMethod - 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 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 ) responseData = response.json() # 存储依赖数据 if dataStore: RelyDataStore.do(eval(dataStore),apiName, c_idx - 1, eval (requestData),responseData) # 比对结果 else: print "用例被忽略执行" else: print "接口被设置忽略执行"
class HandleTestCase(): def __init__(self,testFilePath): self.wb=ParseExcel() self.wb.loadWorkBook(testFilePath) def getTestAPIInfo(self,rowNum=None): #rowNum指定API的具体行 #wb=ParseExcel() #wb.loadWorkBook(testFilePath) API_SheetObj=self.wb.getSheetByName("API") #print(API_SheetObj) testAPIAllInfo=self.wb.getRow(API_SheetObj,rowNum) print("testAPIAllInfo--->",testAPIAllInfo) APIName=testAPIAllInfo[1].value RequestUrl=testAPIAllInfo[2].value RequestMethod=testAPIAllInfo[3].value ParamsType=testAPIAllInfo[4].value CaseSheetName=testAPIAllInfo[5].value Active=testAPIAllInfo[6].value #print(APIName,RequestUrl,RequestMethod,ParamsType,CaseSheetName,Active) APIsheet_rows=self.wb.getRowsNumber(API_SheetObj) if Active=="y": testCaseSheetObj=self.wb.getSheetByName(CaseSheetName) print("testCaseSheetObj--->",testCaseSheetObj) testsheet_rows=self.wb.getRowsNumber(testCaseSheetObj) return testCaseSheetObj,APIName,RequestUrl, RequestMethod,ParamsType,CaseSheetName,Active,testsheet_rows,APIsheet_rows else: print("该接口%s,%s不用执行"%(APIName,CaseSheetName)) def getTestCaseInfo(self,testCaseSheet,rowNum): #rowNum指定testcasesheet的具体行 testCaseAllInfo=self.wb.getRow(testCaseSheet,rowNum) print("testCaseAllInfo--->",testCaseAllInfo) RequestData=testCaseAllInfo[0].value RelyData = testCaseAllInfo[1].value ResponseCode=testCaseAllInfo[2].value DataStore=testCaseAllInfo[4].value CheckPoint=testCaseAllInfo[5].value Active=testCaseAllInfo[6].value if Active=="y": #print(RequestData,RelyData,ResponseCode,DataStore,Active) return eval(RequestData),RelyData,ResponseCode,DataStore,eval(CheckPoint),Active else: print("[%s]第%s条用例不用执行"%(testCaseSheet.title,(rowNum-1))) return None
from Utils.ParseExcel import ParseExcel def writeResult(wb, sheetObj, errorinfo, response_data, result, rowNo): wb.writeCell(sheetObj, content=response_data, rowNo=rowNo, colsNo=4) print("4444444444444444444444444444") wb.writeCell(sheetObj, content=result, rowNo=rowNo, colsNo=8) print("555555555555555555555555555") if eval(errorinfo): wb.writeCell(sheetObj, content=errorinfo, rowNo=rowNo, colsNo=9) print("666666666666666666666666666") print("write down") if __name__ == "__main__": wb = ParseExcel() wb.loadWorkBook( r"E:\我的坚果云\framework\practice\excel_interface\TestData\testData.xlsx") ob = wb.getSheetByName("注册接口用例") writeResult(wb, ob, "{}", "{}", "pass", 2)
def main(): wb = ParseExcel() wb.loadWorkBook( r"E:\framework\practice\excel_interface\TestData\testData.xlsx") hc = HandleTestCase(os.path.join(ProjectPath, "TestData\\testData.xlsx")) for i in range(2, 7): #需要算出api总表的行数 allinfo = hc.getTestAPIInfo(i) if allinfo: testsheet = allinfo[0] #print(hc.getTestCaseInfo(testsheet, 2)) APIName = allinfo[1] requestUrl = allinfo[2] requestMethod = allinfo[3] ParamsType = allinfo[4] testsheet_rows = allinfo[7] for j in range(2, testsheet_rows + 1): #需要算出每个casesheet的行数 sub = hc.getTestCaseInfo(testsheet, j) if sub: #eval(RequestData),RelyData,ResponseCode,DataStore,CheckPoint,Active print("sub", sub) hr = HttpRequest() RequestData = sub[0] if "username" in RequestData: RequestData["username"] = RequestData["username"] + str( getUniqnum()) RelyData = sub[1] ResponseCode = sub[2] DataStore = sub[3] CheckPoint = sub[4] if RelyData: RequestData = getRelyData(REQUEST_DATA, RESPONSE_DATA, RequestData, eval(RelyData)) if APIName == "querycontent": RequestData = str(RequestData[list( RequestData.keys())[0]]) print(requestUrl, requestMethod, RequestData, ParamsType) response = hr.httpRequest(requestUrl, requestMethod, RequestData, ParamsType, cookies=None, headere=None) print("response", response.text) if DataStore: print("dddddddddddddddddddddddddssssssss", RequestData) dataStore(APIName, j - 1, eval(DataStore), RequestData, json.loads(response.text)) if response.status_code == ResponseCode: assert_result = assertResult(json.loads(response.text), CheckPoint) print("assert_reuslt", assert_result) else: print("response_code不是200") print("assert_result[1]", str(assert_result[1])) #writeResult(hc.wb, testsheet, "{}", "{}", "pass", 2) writeResult(hc.wb, testsheet, str(assert_result[1]), str(response.text), "pass" if assert_result[0] else "fail", j) print("-" * 100) print("REQUEST_DATA", REQUEST_DATA) print("RESPONSE_DATA", RESPONSE_DATA)