您可以捐助,支持我们的公益事业。

1元 10元 50元





认证码:  验证码,看不清楚?请点击刷新验证码 必填



  求知 文章 文库 Lib 视频 iPerson 课程 认证 咨询 工具 讲座 Model Center   Code  
会员   
   
 
     
   
 订阅
  捐助
python接口自动化实战(框架)
 
  7936  次浏览      16
 2019-3-15 
 
编辑推荐:
本文来自cnblogs,文庄主要讲解了框架的思路,提取测试用例,测试报告,配置文件,用例设计,用例编写等内容。

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

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

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

一.理清思路

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

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

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

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

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

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

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

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

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

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

notes是测试用例摘要。

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

get_authorization.py


#从配置文件获取访问权限信息
def get_Authorization():
fp = ope n('D:\person\learn\py\HDapi \config\Authorization.txt')
info = fp.read()
fp.close()
return info

 

public.py

import os,xlrd,xlwt,time

#通过配置文件里的接口名称来获取接口url的函数
def get_url(api_name):
fp = open('D:\person\learn\py\HDapi\config\API_url.txt')
#按行读取接口url配置文件
api_infos = fp.readlines()
fp.close()
#通过for循环来遍历配置文件里的每一个url,并且返回传入的接口名称相应的url
for api in api_infos:
#去除因为读取产生的换行空格等
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库来设计测试报告并写入excel里面
def write_report():
workbook = xlwt.Workbook(encoding='utf-8')
#在excel测试报告表格中创建名叫housemanage的页面
worksheet = workbook.add_sheet('housemanage')
#设置字体格式为居中对齐
alignment = xlwt.Alignment()
alignment.horz = alignment.HORZ_CENTER
alignment.vert = alignment.VERT_CENTER
style = 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,json
from 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 = 2
xu = 0
yu = 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+=1
worksheet.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 += 1
else:
worksheet.write(2,a,'faild:%s'%k)
yu += 1
#测试用例执行完后,返回用例成功与失败的数量
return xu,yu
# now = time.strftime('%Y-%m-%d %H_%M_%S')
# report_dir = 'D:\\person\\learn\\py\\HDapi\\report\\'
# filename =report_dir + now + 'apiresult.xlsx'
# workbook.save(filename)

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---------------------

五.通过对公共函数、测试用例的设计联合的思考应该在执行文件里面做什么,实现什么。

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


# utf-8
from common import public
import test_inserthouse,test_updatehouse
import time
from 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+xu
yr = 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:\\person\\learn\\py\\HDapi\\report\\'
#拼接出测试报告名
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)---------------------

六.奉上测试报告输出

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

图表总览:

excel测试报告情况:

   
7936 次浏览       16
相关文章

微服务测试之单元测试
一篇图文带你了解白盒测试用例设计方法
全面的质量保障体系之回归测试策略
人工智能自动化测试探索
相关文档

自动化接口测试实践之路
jenkins持续集成测试
性能测试诊断分析与优化
性能测试实例
相关课程

持续集成测试最佳实践
自动化测试体系建设与最佳实践
测试架构的构建与应用实践
DevOps时代的测试技术与最佳实践