您的位置:首页技术文章
文章详情页

python接口自动化框架实战

【字号: 日期:2022-07-01 14:04:27浏览:5作者:猪猪

python接口测试的原理,就不解释了,百度一大堆。

先看目录,可能这个框架比较简单,但是麻雀虽小五脏俱全。

python接口自动化框架实战

各个文件夹下的文件如下:

python接口自动化框架实战

一.理清思路

我这个自动化框架要实现什么

1.从excel里面提取测试用例

2.测试报告的输出,并且测试报告得包括执行的测试用例的数量、成功的数量、失败的数量以及哪条成功了,失败的是哪一个,失败的原因是什么;测试结果的总体情况通过图表来表示。

3.测试报告用什么形式输出,excel,还是html,还是其他的,这里我选择了excel

4.配置文件需要配置什么东西

5.哪些东西可以放入公共函数直接调用。

好的这些思路理清楚之后就可以动手了。

二.首先是配置文件和excel测试用例的设计

数据与代码分离,也就是数据性的需要作为配置文件可以随时修改。如:接口url,网站登录权限验证信息,数据库信息。全部存入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

我的测试用例的设计如下:

notes是测试用例摘要。

python接口自动化框架实战

三.公共函数存在common文件夹下

get_authorization.py

#从配置文件获取访问权限信息def get_Authorization():fp = open(’D:personlearnpyHDapiconfigAuthorization.txt’)info = fp.read()fp.close()return info

public.py

import os,xlrd,xlwt,time #通过配置文件里的接口名称来获取接口url的函数def get_url(api_name):fp = open(’D:personlearnpyHDapiconfigAPI_url.txt’)#按行读取接口url配置文件api_infos = fp.readlines()fp.close()#通过for循环来遍历配置文件里的每一个url,并且返回传入的接口名称相应的urlfor api in api_infos:#去除因为读取产生的换行空格等api_f = api.strip(’ rnt’)api_c = api_f.split(’=’)if api_name == api_c[0]:return api_c[1] #通过传入用例名称的文件和excel页面来读取测试用例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 #通过xlwt库来设计测试报告并写入excel里面def write_report():workbook = xlwt.Workbook(encoding=’utf-8’)#在excel测试报告表格中创建名叫housemanage的页面worksheet = workbook.add_sheet(’housemanage’)#设置字体格式为居中对齐alignment = xlwt.Alignment()alignment.horz = alignment.HORZ_CENTERalignment.vert = alignment.VERT_CENTERstyle = xlwt.XFStyle()style.alignment = alignment#具体的合并哪些单元格并且写入相应的信息worksheet.write_merge(0,0,0,7,’测试报告(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两个参数,因为在测试测试用例和运行文件中需要用到的两个参数return worksheet,workbook四.测试用例的编写

test_inserthouse.py

import requests,unittest,os,time,jsonfrom common import public,get_authorization #房屋添加用例,通过传入public里wirte_sheet函数返回的参数wooksheet,将用例的执行结果写入到测试报告中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循环来实现遍历一个excel页面的所有测试用例for i in range(1,nor):#获取excel表格里面需要给接口传入的参数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需要传入的参数data = {’houseNum’:houseNum,’houseUseFor’:houseUseFor,’orgUuid’:orgUuid,’residentNum’:residentNum,’floor’:floor,’emergencyPhone’:emergencyPhone,’payment’:payment} #请求头,网站加了登陆验证之后需要在请求头传入Authorization参数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)#将字符串格式转换为字典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 += 1else:worksheet.write(2,a,’faild:%s’%k)yu += 1#测试用例执行完后,返回用例成功与失败的数量return xu,yu

test_updatehouse.py

import requests,unittest,os,time,jsonfrom 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 = 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五.通过对公共函数、测试用例的设计联合的思考应该在执行文件里面做什么,实现什么。

本来我想将执行文件单独放置于HDapi-auto-test的根文件下的,可是将测试通过与不通过的数量写入到测试报告里面,就必须要调用公共函数的方法,由于放置在根文件夹下与公共函数隔了一个文件夹无法调用( 本鸟不会调用),所以不得不将执行文件放置于测试用例文件夹下了,好在文件名还是比较好区分也比较好寻找,另外我还想加上自动发送邮件的功能,这里不写了,其实发送邮件很简单随便找几个例子就OK了,ps:代码比较low,都没有封装,直接暴力简单执行。代码如下:

from common import publicimport test_inserthouse,test_updatehouseimport timefrom pychartdir import *#从公共函数调用excel的写入方法worksheet,workbook = public.write_report() #测试用例的执行,并且返回x:成功的数量,y:失败的数量xu,yu = test_inserthouse.test_inserthouses(worksheet,workbook)x,y = test_updatehouse.test_updatehouses(worksheet,workbook)#得到成功与失败的总数量xr = x+xuyr = y+yu#将成功与失败的数量写入的excel的固定表格中worksheet.write(2,12,xr)worksheet.write(2,13,yr)#获取当前的时间并以制定的格式返回now = time.strftime(’%Y-%m-%d %H_%M_%S’)#测试报告输出的地址report_dir = ’D:personlearnpyHDapireport’#拼接出测试报告名filename =report_dir + now + ’apiresult.xlsx’workbook.save(filename) #通过pychart库实现图形处理,生成测试报告总览图----具体的参数设计可以参考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')六.奉上测试报告输出

本来想将生成的图片放进excel测试报告里面的,奈何能力有限,没办法将图片放进去,智能单独存为一个png文件了

图表总览:

python接口自动化框架实战

excel测试报告情况:

python接口自动化框架实战

到此这篇关于python接口自动化框架实战 的文章就介绍到这了,更多相关python接口自动化内容请搜索好吧啦网以前的文章或继续浏览下面的相关文章希望大家以后多多支持好吧啦网!

标签: Python 编程
相关文章: