python接口自動(dòng)化框架實(shí)戰(zhàn)
python接口測(cè)試的原理,就不解釋了,百度一大堆。
先看目錄,可能這個(gè)框架比較簡(jiǎn)單,但是麻雀雖小五臟俱全。
各個(gè)文件夾下的文件如下:
我這個(gè)自動(dòng)化框架要實(shí)現(xiàn)什么
1.從excel里面提取測(cè)試用例
2.測(cè)試報(bào)告的輸出,并且測(cè)試報(bào)告得包括執(zhí)行的測(cè)試用例的數(shù)量、成功的數(shù)量、失敗的數(shù)量以及哪條成功了,失敗的是哪一個(gè),失敗的原因是什么;測(cè)試結(jié)果的總體情況通過(guò)圖表來(lái)表示。
3.測(cè)試報(bào)告用什么形式輸出,excel,還是html,還是其他的,這里我選擇了excel
4.配置文件需要配置什么東西
5.哪些東西可以放入公共函數(shù)直接調(diào)用。
好的這些思路理清楚之后就可以動(dòng)手了。
二.首先是配置文件和excel測(cè)試用例的設(shè)計(jì)數(shù)據(jù)與代碼分離,也就是數(shù)據(jù)性的需要作為配置文件可以隨時(shí)修改。如:接口url,網(wǎng)站登錄權(quán)限驗(yàn)證信息,數(shù)據(jù)庫(kù)信息。全部存入config文件夾下
下面是具體的配置文件信息:
API_url.txt
inserthouse=http://IP:port/scp-mdmapp/house/insertHousedeletehouse=http://IP:port/scp-mdmapp/house/deleteHousebatchdeletehouse=http://IP:port/scp-mdmapp/house/batchdeleteHousegethouse=http://IP:port/scp-mdmapp/house/getHouseupdatehouse=http://IP:port/scp-mdmapp/house/updateHouse
Authorization.txt
joxNTIxMTg3MTA3fQ.JyeCMMsM0tOr7exORUNpkZ-FtprjpNBhMtFjUAdnYDnhRfaR6qi3fqVkybyb245zONiTxLOw8jBR60oNUVEbKx9_cut6uDIZMUFYOx6hyyBkY9IXJlutYdo4sSMAKF_MjKsZY7bZNXLHzN0juiezn6iN0hbnbhS-Kv2LYLLZLTs
我的測(cè)試用例的設(shè)計(jì)如下:
notes是測(cè)試用例摘要。
get_authorization.py
#從配置文件獲取訪問(wèn)權(quán)限信息def get_Authorization():fp = open(’D:personlearnpyHDapiconfigAuthorization.txt’)info = fp.read()fp.close()return info
public.py
import os,xlrd,xlwt,time #通過(guò)配置文件里的接口名稱(chēng)來(lái)獲取接口url的函數(shù)def get_url(api_name):fp = open(’D:personlearnpyHDapiconfigAPI_url.txt’)#按行讀取接口url配置文件api_infos = fp.readlines()fp.close()#通過(guò)for循環(huán)來(lái)遍歷配置文件里的每一個(gè)url,并且返回傳入的接口名稱(chēng)相應(yīng)的urlfor api in api_infos:#去除因?yàn)樽x取產(chǎn)生的換行空格等api_f = api.strip(’ rnt’)api_c = api_f.split(’=’)if api_name == api_c[0]:return api_c[1] #通過(guò)傳入用例名稱(chēng)的文件和excel頁(yè)面來(lái)讀取測(cè)試用例def get_case(filename,sheetnum):case_dir=’D:personlearnpyHDapitestcase_excel’ + ’’ + filename + ’.xlsx’ datas = xlrd.open_workbook(case_dir)table = datas.sheets()[sheetnum]nor = table.nrowsnol = table.ncolsreturn nor,table #通過(guò)xlwt庫(kù)來(lái)設(shè)計(jì)測(cè)試報(bào)告并寫(xiě)入excel里面def write_report():workbook = xlwt.Workbook(encoding=’utf-8’)#在excel測(cè)試報(bào)告表格中創(chuàng)建名叫housemanage的頁(yè)面worksheet = workbook.add_sheet(’housemanage’)#設(shè)置字體格式為居中對(duì)齊alignment = xlwt.Alignment()alignment.horz = alignment.HORZ_CENTERalignment.vert = alignment.VERT_CENTERstyle = xlwt.XFStyle()style.alignment = alignment#具體的合并哪些單元格并且寫(xiě)入相應(yīng)的信息worksheet.write_merge(0,0,0,7,’測(cè)試報(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(cè)試測(cè)試用例和運(yùn)行文件中需要用到的兩個(gè)參數(shù)return worksheet,workbook四.測(cè)試用例的編寫(xiě)
test_inserthouse.py
import requests,unittest,os,time,jsonfrom common import public,get_authorization #房屋添加用例,通過(guò)傳入public里wirte_sheet函數(shù)返回的參數(shù)wooksheet,將用例的執(zhí)行結(jié)果寫(xiě)入到測(cè)試報(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 = 2xu = 0yu = 0#用for循環(huán)來(lái)實(shí)現(xiàn)遍歷一個(gè)excel頁(yè)面的所有測(cè)試用例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} #請(qǐng)求頭,網(wǎng)站加了登陸驗(yàn)證之后需要在請(qǐng)求頭傳入Authorization參數(shù)headers={’Accept’:’application/json’,’Content-Type’:’application/json’,’Authorization’:Authorization}a+=1worksheet.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’)#判斷接口測(cè)試通過(guò)與否if m==expect_code and n==expect_message:worksheet.write(2,a,’pass’)xu += 1else:worksheet.write(2,a,’faild:%s’%k)yu += 1#測(cè)試用例執(zhí)行完后,返回用例成功與失敗的數(shù)量return xu,yu
test_updatehouse.py
import requests,unittest,os,time,jsonfrom common import public,get_authorization #房屋編輯測(cè)試用例def test_updatehouses(worksheet,workbook):nor,table = public.get_case(’house’,4)Authorization = get_authorization.get_Authorization()url = public.get_url(’updatehouse’)a = 2x = 0y = 0for 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+=1worksheet.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 += 1else:worksheet.write(4,a,’faild:%s’%k)y += 1return x,y五.通過(guò)對(duì)公共函數(shù)、測(cè)試用例的設(shè)計(jì)聯(lián)合的思考應(yīng)該在執(zhí)行文件里面做什么,實(shí)現(xiàn)什么。
本來(lái)我想將執(zhí)行文件單獨(dú)放置于HDapi-auto-test的根文件下的,可是將測(cè)試通過(guò)與不通過(guò)的數(shù)量寫(xiě)入到測(cè)試報(bào)告里面,就必須要調(diào)用公共函數(shù)的方法,由于放置在根文件夾下與公共函數(shù)隔了一個(gè)文件夾無(wú)法調(diào)用( 本鳥(niǎo)不會(huì)調(diào)用),所以不得不將執(zhí)行文件放置于測(cè)試用例文件夾下了,好在文件名還是比較好區(qū)分也比較好尋找,另外我還想加上自動(dòng)發(fā)送郵件的功能,這里不寫(xiě)了,其實(shí)發(fā)送郵件很簡(jiǎn)單隨便找?guī)讉€(gè)例子就OK了,ps:代碼比較low,都沒(méi)有封裝,直接暴力簡(jiǎn)單執(zhí)行。代碼如下:
from common import publicimport test_inserthouse,test_updatehouseimport timefrom pychartdir import *#從公共函數(shù)調(diào)用excel的寫(xiě)入方法worksheet,workbook = public.write_report() #測(cè)試用例的執(zhí)行,并且返回x:成功的數(shù)量,y:失敗的數(shù)量xu,yu = test_inserthouse.test_inserthouses(worksheet,workbook)x,y = test_updatehouse.test_updatehouses(worksheet,workbook)#得到成功與失敗的總數(shù)量xr = x+xuyr = y+yu#將成功與失敗的數(shù)量寫(xiě)入的excel的固定表格中worksheet.write(2,12,xr)worksheet.write(2,13,yr)#獲取當(dāng)前的時(shí)間并以制定的格式返回now = time.strftime(’%Y-%m-%d %H_%M_%S’)#測(cè)試報(bào)告輸出的地址report_dir = ’D:personlearnpyHDapireport’#拼接出測(cè)試報(bào)告名filename =report_dir + now + ’apiresult.xlsx’workbook.save(filename) #通過(guò)pychart庫(kù)實(shí)現(xiàn)圖形處理,生成測(cè)試報(bào)告總覽圖----具體的參數(shù)設(shè)計(jì)可以參考pychart庫(kù)的文檔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')六.奉上測(cè)試報(bào)告輸出
本來(lái)想將生成的圖片放進(jìn)excel測(cè)試報(bào)告里面的,奈何能力有限,沒(méi)辦法將圖片放進(jìn)去,智能單獨(dú)存為一個(gè)png文件了
圖表總覽:
excel測(cè)試報(bào)告情況:
到此這篇關(guān)于python接口自動(dòng)化框架實(shí)戰(zhàn) 的文章就介紹到這了,更多相關(guān)python接口自動(dòng)化內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. XML入門(mén)的常見(jiàn)問(wèn)題(一)2. 使用css實(shí)現(xiàn)全兼容tooltip提示框3. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)4. 詳解CSS偽元素的妙用單標(biāo)簽之美5. 詳解PHP實(shí)現(xiàn)HTTP服務(wù)器過(guò)程6. 低版本IE正常運(yùn)行HTML5+CSS3網(wǎng)站的3種解決方案7. html小技巧之td,div標(biāo)簽里內(nèi)容不換行8. ASP中格式化時(shí)間短日期補(bǔ)0變兩位長(zhǎng)日期的方法9. HTML DOM setInterval和clearInterval方法案例詳解10. ASP基礎(chǔ)入門(mén)第八篇(ASP內(nèi)建對(duì)象Application和Session)
