python接口自動(dòng)化框架實(shí)戰(zhàn)
python接口測試的原理,就不解釋了,百度一大堆。
先看目錄,可能這個(gè)框架比較簡單,但是麻雀雖小五臟俱全。
各個(gè)文件夾下的文件如下:
一.理清思路
我這個(gè)自動(dòng)化框架要實(shí)現(xiàn)什么
1.從excel里面提取測試用例
2.測試報(bào)告的輸出,并且測試報(bào)告得包括執(zhí)行的測試用例的數(shù)量、成功的數(shù)量、失敗的數(shù)量以及哪條成功了,失敗的是哪一個(gè),失敗的原因是什么;測試結(jié)果的總體情況通過圖表來表示。
3.測試報(bào)告用什么形式輸出,excel,還是html,還是其他的,這里我選擇了excel
4.配置文件需要配置什么東西
5.哪些東西可以放入公共函數(shù)直接調(diào)用。
好的這些思路理清楚之后就可以動(dòng)手了。
二.首先是配置文件和excel測試用例的設(shè)計(jì)
數(shù)據(jù)與代碼分離,也就是數(shù)據(jù)性的需要作為配置文件可以隨時(shí)修改。如:接口url,網(wǎng)站登錄權(quán)限驗(yàn)證信息,數(shù)據(jù)庫信息。全部存入config文件夾下
下面是具體的配置文件信息:
API_url.txt
inserthouse=http://IP:port/scp-mdmapp/house/insertHouse deletehouse=http://IP:port/scp-mdmapp/house/deleteHouse batchdeletehouse=http://IP:port/scp-mdmapp/house/batchdeleteHouse gethouse=http://IP:port/scp-mdmapp/house/getHouse updatehouse=http://IP:port/scp-mdmapp/house/updateHouse
Authorization.txt
joxNTIxMTg3MTA3fQ.JyeCMMsM0tOr7exORUNpkZ-FtprjpNBhMtFjUAdnYDnhRfaR6qi3fqVkybyb245zONiTxLOw8jBR60oNUVEbKx9_cut6uDIZMUFYOx6hyyBkY9IXJlutYdo4sSMAKF_MjKsZY7bZNXLHzN0juiezn6iN0hbnbhS-Kv2LYLLZLTs
我的測試用例的設(shè)計(jì)如下:
notes是測試用例摘要。
三.公共函數(shù)存在common文件夾下
get_authorization.py
#從配置文件獲取訪問權(quán)限信息 def get_Authorization(): fp = open('D:\person\learn\py\HDapi\config\Authorization.txt') info = fp.read() fp.close() return info
public.py
import os,xlrd,xlwt,time #通過配置文件里的接口名稱來獲取接口url的函數(shù) def get_url(api_name): fp = open('D:\person\learn\py\HDapi\config\API_url.txt') #按行讀取接口url配置文件 api_infos = fp.readlines() fp.close() #通過for循環(huán)來遍歷配置文件里的每一個(gè)url,并且返回傳入的接口名稱相應(yīng)的url for api in api_infos: #去除因?yàn)樽x取產(chǎn)生的換行空格等 api_f = api.strip(' \r\n\t') api_c = api_f.split('=') if api_name == api_c[0]: return api_c[1] #通過傳入用例名稱的文件和excel頁面來讀取測試用例 def get_case(filename,sheetnum): case_dir='D:\\person\\learn\\py\\HDapi\\testcase_excel' + '\\' + filename + '.xlsx' datas = xlrd.open_workbook(case_dir) table = datas.sheets()[sheetnum] nor = table.nrows nol = table.ncols return nor,table #通過xlwt庫來設(shè)計(jì)測試報(bào)告并寫入excel里面 def write_report(): workbook = xlwt.Workbook(encoding='utf-8') #在excel測試報(bào)告表格中創(chuàng)建名叫housemanage的頁面 worksheet = workbook.add_sheet('housemanage') #設(shè)置字體格式為居中對齊 alignment = xlwt.Alignment() alignment.horz = alignment.HORZ_CENTER alignment.vert = alignment.VERT_CENTER style = xlwt.XFStyle() style.alignment = alignment #具體的合并哪些單元格并且寫入相應(yīng)的信息 worksheet.write_merge(0,0,0,7,'測試報(bào)告(housemanage)',style) worksheet.write_merge(1,10,0,0,'house_manage',style) worksheet.write_merge(1,2,1,1,'insethouse',style) worksheet.write_merge(3,4,1,1,'updatehouse',style) worksheet.write_merge(5,6,1,1,'deletehouse',style) worksheet.write_merge(7,8,1,1,'gethouse',style) worksheet.write_merge(9,10,1,1,'updatehouse',style) worksheet.write_merge(1,2,11,11,'total_result',style) worksheet.write(1,2,'notes') worksheet.write(2,2,'detail') worksheet.write(3,2,'notes') worksheet.write(4,2,'detail') worksheet.write(5,2,'notes') worksheet.write(6,2,'detail') worksheet.write(7,2,'notes') worksheet.write(8,2,'detail') worksheet.write(9,2,'notes') worksheet.write(10,2,'detail') worksheet.write(1,12,'pass') worksheet.write(1,13,'faild') #最后返回worksheet,workbook兩個(gè)參數(shù),因?yàn)樵跍y試測試用例和運(yùn)行文件中需要用到的兩個(gè)參數(shù) return worksheet,workbook
四.測試用例的編寫
test_inserthouse.py
import requests,unittest,os,time,json from common import public,get_authorization #房屋添加用例,通過傳入public里wirte_sheet函數(shù)返回的參數(shù)wooksheet,將用例的執(zhí)行結(jié)果寫入到測試報(bào)告中 def test_inserthouses(worksheet,workbook): url = public.get_url('inserthouse') nor,table = public.get_case('house',0) Authorization = get_authorization.get_Authorization() a = 2 xu = 0 yu = 0 #用for循環(huán)來實(shí)現(xiàn)遍歷一個(gè)excel頁面的所有測試用例 for i in range(1,nor): #獲取excel表格里面需要給接口傳入的參數(shù) houseNum = table.cell_value(i,0) orgUuid = table.cell_value(i,1) floor = table.cell_value(i,2) houseUseFor = table.cell_value(i,3) residentNum = table.cell_value(i,4) emergencyPhone = table.cell_value(i,5) expect_code = table.cell_value(i,6) expect_message = table.cell_value(i,7) notes = table.cell_value(i,8) payment = table.cell_value(i,11) #接口body需要傳入的參數(shù) data = { 'houseNum':houseNum, 'houseUseFor':houseUseFor, 'orgUuid':orgUuid, 'residentNum':residentNum, 'floor':floor, 'emergencyPhone':emergencyPhone, 'payment':payment } #請求頭,網(wǎng)站加了登陸驗(yàn)證之后需要在請求頭傳入Authorization參數(shù) headers={ 'Accept':'application/json', 'Content-Type':'application/json', 'Authorization':Authorization } a+=1 worksheet.write(1,a,notes) data = json.dumps(data) r = requests.post(url,data=data,headers=headers) #將字符串格式轉(zhuǎn)換為字典 b = eval(r.text) m = b.get('code') n = b.get('message') k = b.get('data') #判斷接口測試通過與否 if m==expect_code and n==expect_message: worksheet.write(2,a,'pass') xu += 1 else: worksheet.write(2,a,'faild:%s'%k) yu += 1 #測試用例執(zhí)行完后,返回用例成功與失敗的數(shù)量 return xu,yu
test_updatehouse.py
import requests,unittest,os,time,json from common import public,get_authorization #房屋編輯測試用例 def test_updatehouses(worksheet,workbook): nor,table = public.get_case('house',4) Authorization = get_authorization.get_Authorization() url = public.get_url('updatehouse') a = 2 x = 0 y = 0 for i in range(1,nor): houseNum = table.cell_value(i,0) orgUuid = table.cell_value(i,1) uuid = table.cell_value(i,2) houseUseFor = table.cell_value(i,3) residentNum = table.cell_value(i,4) emergencyPhone = table.cell_value(i,5) expect_code = table.cell_value(i,6) expect_message = table.cell_value(i,7) notes = table.cell_value(i,8) floor = table.cell_value(i,9) payment = table.cell_value(i,11) data = { 'houseNum':houseNum, 'houseUseFor':houseUseFor, 'orgUuid':orgUuid, 'floor':floor, 'residentNum':residentNum, 'uuid':uuid, 'emergencyPhone':emergencyPhone, 'payment':payment } headers={ 'Accept':'application/json', 'Content-Type':'application/json', 'Authorization':Authorization } a+=1 worksheet.write(3,a,notes) data = json.dumps(data) r = requests.post(url,data=data,headers=headers) b = eval(r.text) m = b.get('code') n = b.get('message') k = b.get('data') if m==expect_code and n==expect_message: worksheet.write(4,a,'pass') x += 1 else: worksheet.write(4,a,'faild:%s'%k) y += 1 return x,y
五.通過對公共函數(shù)、測試用例的設(shè)計(jì)聯(lián)合的思考應(yīng)該在執(zhí)行文件里面做什么,實(shí)現(xiàn)什么。
本來我想將執(zhí)行文件單獨(dú)放置于HDapi-auto-test的根文件下的,可是將測試通過與不通過的數(shù)量寫入到測試報(bào)告里面,就必須要調(diào)用公共函數(shù)的方法,由于放置在根文件夾下與公共函數(shù)隔了一個(gè)文件夾無法調(diào)用( 本鳥不會調(diào)用),所以不得不將執(zhí)行文件放置于測試用例文件夾下了,好在文件名還是比較好區(qū)分也比較好尋找,另外我還想加上自動(dòng)發(fā)送郵件的功能,這里不寫了,其實(shí)發(fā)送郵件很簡單隨便找?guī)讉€(gè)例子就OK了,ps:代碼比較low,都沒有封裝,直接暴力簡單執(zhí)行。代碼如下:
from common import public import test_inserthouse,test_updatehouse import time from pychartdir import * #從公共函數(shù)調(diào)用excel的寫入方法 worksheet,workbook = public.write_report() #測試用例的執(zhí)行,并且返回x:成功的數(shù)量,y:失敗的數(shù)量 xu,yu = test_inserthouse.test_inserthouses(worksheet,workbook) x,y = test_updatehouse.test_updatehouses(worksheet,workbook) #得到成功與失敗的總數(shù)量 xr = x+xu yr = y+yu #將成功與失敗的數(shù)量寫入的excel的固定表格中 worksheet.write(2,12,xr) worksheet.write(2,13,yr) #獲取當(dāng)前的時(shí)間并以制定的格式返回 now = time.strftime('%Y-%m-%d %H_%M_%S') #測試報(bào)告輸出的地址 report_dir = 'D:\\person\\learn\\py\\HDapi\\report\\' #拼接出測試報(bào)告名 filename =report_dir + now + 'apiresult.xlsx' workbook.save(filename) #通過pychart庫實(shí)現(xiàn)圖形處理,生成測試報(bào)告總覽圖----具體的參數(shù)設(shè)計(jì)可以參考pychart庫的文檔 data = [yr, xr] labels = ["faild", "pass"] c = PieChart(280, 240) c.setPieSize(140, 130, 80) c.addTitle("api_result") c.set3D() c.setData(data, labels) c.setExplode(0) c.makeChart(report_dir+now+"apiresult.png")
六.奉上測試報(bào)告輸出
本來想將生成的圖片放進(jìn)excel測試報(bào)告里面的,奈何能力有限,沒辦法將圖片放進(jìn)去,智能單獨(dú)存為一個(gè)png文件了
圖表總覽:
excel測試報(bào)告情況:
到此這篇關(guān)于python接口自動(dòng)化框架實(shí)戰(zhàn) 的文章就介紹到這了,更多相關(guān)python接口自動(dòng)化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python協(xié)程方式的實(shí)現(xiàn)及意義筆記分享
協(xié)程也被稱為微線程,是一種用戶態(tài)的上下文切換技術(shù),簡而言之,就是通過一個(gè)線程實(shí)現(xiàn)代碼互相切換執(zhí)行,本文主要給大家介紹實(shí)現(xiàn)協(xié)程的幾種方法2021-09-09selenium鼠標(biāo)操作實(shí)戰(zhàn)案例詳解
在實(shí)際場景中,會有單擊、長時(shí)間單擊、雙擊、右鍵、拖拽等鼠標(biāo)操作,selenium提供了名為ActionChains的類來處理這些操作,下面這篇文章主要給大家介紹了關(guān)于selenium鼠標(biāo)操作實(shí)戰(zhàn)案例的相關(guān)資料,需要的朋友可以參考下2023-05-05python簡單線程和協(xié)程學(xué)習(xí)心得(分享)
下面小編就為大家?guī)硪黄猵ython簡單線程和協(xié)程學(xué)習(xí)心得(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06