使用excel進行用例的管理,如圖:json
如下爲這次根據公司全部接口以及返回的參數進行判斷來實現的:api
# coding:utf-8import requestsimport jsonimport xlrdimport osfrom xlutils.copy import copyimport loggingimport sys# import Cookie'''簡易版接口自動化測試'''defaultencoding = 'utf-8'if sys.getdefaultencoding() != defaultencoding: reload(sys) sys.setdefaultencoding(defaultencoding) # 在eclipse裏該報錯不須要理會# 獲取token值par1 = {"userName": "xxxxxxxxxx", "password": "xxxxxxxx"}url1 = "http://xxxxxxxxxxxxxxx"r1 = requests.get(url1, params=par1)LOGIN_TOKEN = r1.cookies["authToken"]# print LOGIN_TOKEN# 建立日記logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(filename) s[line:%(lineno)d] %(levelname)s %(message)s', datefmt='%a, %d %b %Y %H:%M:%S', filename=r'C:\Users\admin\Desktop\TestLog.log', filemode='w' )# 定義一個StreamHandler,將INFO級別或更高的日誌信息打印到標準錯誤,並將其添加到當前的日誌處理對象#console = logging.StreamHandler()console.setLevel(logging.INFO)formatter = logging.Formatter('%(name)-12s:%(levelname)-8s %(message)s')console.setFormatter(formatter)logging.getLogger('').addHandler(console)excel = r'C:\Users\admin\Desktop\test_case_yang1.xls' # 定義存放用例的excel路徑,當前路徑下data = xlrd.open_workbook(excel)logging.info("打開%s excel表格成功 " % data)result = [] # 用來存放驗證結果responseValue = [] # 存放返回的數據bidui = [] # 用來比對預期狀態碼和實際狀態碼remark = [] # 用來存儲outparam內嵌部分出現的異常狀況test_result = [] # 用來存放測試結果table = data.sheet_by_index(0)nrow = table.nrows # 獲取行數headers = {"Content-Type": "application/json;charset=utf-8"}for i in range(1, nrow): # 循環獲取每行中的數據 requestMethod = table.cell(i, 6).value url = table.cell(i, 5).value # 拼接url logging.info(url) payload = eval(table.cell(i, 7).value) # 參數格式處理 logging.info('\n' + table.cell(i, 2).value + "請求報文:\n" + table.cell(i, 7).value + '\n') ex = table.cell(i, 8).value # 獲取指望的返回值 # 能夠在這裏作一個異常處理 if requestMethod == 'get': # 不一樣的方法發不一樣的請求 payload["authToken"] = LOGIN_TOKEN r = requests.get(url, payload) elif requestMethod == 'post': payload["LOGIN_TOKEN"] = LOGIN_TOKEN payload = json.dumps(payload) r = requests.post(url, payload, headers=headers) apicontent = r.json() apicontent = json.dumps(apicontent, ensure_ascii=False) logging.info('\n' + table.cell(i, 2).value + "接口返回結果:\n" + apicontent + '\n') apicontent = json.loads(apicontent) if apicontent["result"] == "SUCCESS" or apicontent["result"] == 0: result.append('true') # 獲取接口返回內嵌outparam字段內容 # result_out=apicontent["outparam"] if "outparam" in r.json(): result_out = r.json()["outparam"] result_out1 = json.dumps(result_out, ensure_ascii=False) result_outparam = json.loads(result_out1) if "BUSI_RESULT" in result_outparam.keys(): if result_outparam["BUSI_RESULT"] == "ERROR": remark.append(result_outparam["BUSI_RESULT_DESC"]) test_result.append(u'測試不經過') else: remark.append(u'——') test_result.append(u'測試經過') elif "RESULT" in result_outparam.keys(): # 針對outparam裏有相同的RESULT字段,不一樣的DESC作判斷 if result_outparam["RESULT"] == "ERROR": if "BUSI_RESULT_DESC" in result_outparam.keys(): remark.append(result_outparam["BUSI_RESULT_DESC"]) test_result.append(u'測試不經過') elif "RESULT_DESC" in result_outparam.keys(): remark.append(result_outparam["RESULT_DESC"]) test_result.append(u'測試不經過') else: pass else: remark.append(u'——') test_result.append(u'測試經過') else: remark.append(u'——') test_result.append(u'測試經過') else: remark.append(u'——') test_result.append(u'測試經過') else: result.append('false') logging.info(r) if 'result_desc' in r.json().keys(): responseValue.append(r.json()['result_desc']) elif 'errorMessage' in r.json().keys(): # 有些錯誤的信息描述是這個字段:針對於有些接口的get請求 if r.json()["result"] == 0: responseValue.append(u'接口正常') else: responseValue.append(u'接口不正常') # responseValue.append(r.json()['errorMessage']) # 在列表上寫入不能留有空,不然結果輸出會報:IndexError: list index out of range # remark.append(u'測試不經過') remark.append(r.json()['errorMessage']) test_result.append(u'測試不經過') elif 'resultDesc' in r.json().keys(): responseValue.append(r.json()['resultDesc']) else: responseValue.append(u'接口正常') # 預期結果和接口實際返回的結果比對 if r.json()["result"] == ex: bidui.append(u"預期結果一致") else: bidui.append(u"預期結果不一致") r.close() print('共有%d個url,當第%d個執行完畢' % (nrow - 1, i))book = copy(data)sheet1 = book.get_sheet(0) # copy原來的excelfor j in range(1, nrow): # 將結果寫入到對應的表格中 #將結果和response都寫入到複製的工做表中 sheet1.write(j, 9, result[j - 1]) sheet1.write(j, 10, bidui[j - 1]) sheet1.write(j, 11, responseValue[j - 1]) sheet1.write(j, 12, remark[j - 1]) sheet1.write(j, 13, test_result[j - 1])os.remove(excel)logging.info(sheet1)book.save(excel) # 移除原來的excel,保存新的excel最後實現的結果如上圖展現聲明:此代碼是借鑑網上某位大神的代碼,再根據現階段公司的接口狀況進行調試實現的