Python 基于http接口自动化测试
Posted 旭旭杂货店
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python 基于http接口自动化测试相关的知识,希望对你有一定的参考价值。
设计原理 基于http协议接口的测试设计,莫过于Python的requests库,简单粗暴易理解。
设计模式 采用python的flask框架,搭建一套接口自动化测试平台。 测试用例维护:采用Excel 测试结果保存:采用MongoDb存储,HTML页面展示
相关核心代码介绍:
- Excel模板如下: 看Excel的定义命名,基本理解,每个参数的含义 介绍: B1:要测试的接口地址 B2:该测试接口的请求参数,以“#”分隔【便于分割】 B3:登录的URL,若测试不需要登录 B4:登录的json串,以字典的形式展现 注:CaseNo、Except——result、Commit,是必填的
- 从Excel读取及处理数据,代码如下:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
import
xlrd
import
os
# ****************************************************************
# Excel模版设置
# self.interFace = 0 #Excel中测试用例接口对应为第1列
# self.interFaceArgList = 1 #Excel中测试用例接口参数列表对应为第2列
# self.loginChoice = 2 #Excel中测试用例接口是否需要登录为第3列
# self.loginJson = 3 #Excel中测试用例接口是否需要登录为第4列
# self.titleIndex = 4 #Excel中测试用例标题行索引为第5列
# self.caseBegin = 5 #Excel中测试用例开始行索引为第6列
# ****************************************************************
class
ExcelSheet:
def
__init__(
self
, sFile, interFace
=
0
, interFaceArgList
=
1
, loginInterFace
=
2
, loginJson
=
3
, titleIndex
=
4
, caseBegin
=
5
):
try
:
excel
=
xlrd.open_workbook(sFile)
except
Exception as e:
print
e
exit()
self
.sheet
=
excel.sheet_by_index(
0
)
# 查询Excel的第一个sheet
self
.interFace
=
interFace
self
.interFaceArgList
=
interFaceArgList
self
.loginInterFace
=
loginInterFace
self
.titleIndex
=
titleIndex
self
.caseBegin
=
caseBegin
self
.loginJson
=
loginJson
def
sheet_name(
self
):
return
self
.sheets.name
def
nrows(
self
):
return
self
.sheet.nrows
def
ncols(
self
):
return
self
.sheet.ncols
def
cellxy(
self
, rowx, colx):
# type: (object, object) -> object
cell_value
=
self
.sheet.cell(rowx, colx).value
# 对数字的处理
if
self
.sheet.cell(rowx, colx).ctype
in
(
2
,
3
)
and
int
(cell_value)
=
=
cell_value:
cell_value
=
int
(cell_value)
return
cell_value
# interFace 测试接口URL
def
get_interFace(
self
):
return
self
.cellxy(
self
.interFace,
1
)
# interFace接口的参数List
def
get_interFaceArgList(
self
):
return
self
.cellxy(
self
.interFaceArgList,
1
).split(
"#"
)
# 测试用例的参数项
def
get_titleIndex(
self
):
return
self
.sheet.row_values(
self
.titleIndex)
# 登录的接口地址
def
get_loginInterFace(
self
):
return
self
.cellxy(
self
.loginInterFace,
1
)
# 获取登录接口参数的json
def
get_loginJson(
self
):
return
str
(
self
.cellxy(
self
.loginJson,
1
))
# 返回单行用例的数据字典
def
get_by_line(
self
, line):
tempdict
=
dict
()
data
=
dict
()
if
line <
self
.caseBegin:
return
False
else
:
for
col
in
range
(
self
.ncols()):
if
self
.cellxy(
self
.titleIndex, col)
in
self
.get_interFaceArgList():
if
self
.cellxy(line, col) !
=
\'X\'
:
data[
self
.cellxy(
self
.titleIndex, col)]
=
self
.cellxy(line, col)
else
:
tempdict[
self
.cellxy(
self
.titleIndex, col)]
=
self
.cellxy(line, col)
tempdict[
"data"
]
=
data
return
tempdict
-
requests的post和get请求代码:
123456789101112131415import
requests
def
postRequest(url, data, cookie):
header
=
{
"Content-Type"
:
"application/json"
}
if
cookie:
return
requests.post(url, data
=
data, cookies
=
cookie, headers
=
header)
else
:
return
requests.post(url, data
=
data, headers
=
header)
def
postRequest(url, cookie):
header
=
{
"Content-Type"
:
"application/json"
}
if
cookie:
return
requests.get(url, cookies
=
cookie, headers
=
header)
else
:
return
requests.get(url, headers
=
header)
- 检查返回为页面还是json串:
1234567
def
checkReturnResult(req_result):
try
:
realResult
=
eval
(req_result)
except
:
return
False
else
:
return
req_result
- 测试结果存储MongoDB:
1234567891011121314151617181920
# -*- coding: utf-8 -*-
import
pymongo
class
ConMongoDb:
#读取配置文件
def
__init__(
self
):
MongoIP
=
"192.168.X.XXX"
MongoPort
=
27017
#链接MongoDb
self
.conn
=
pymongo.Connection(MongoIP, MongoPort)
#选择数据库
self
.db
=
self
.conn.test
self
.collection
=
self
.db.SocketTest
\'\'\'
# **************************************************
# InsertMongo:往MongoDb插入数据
# **************************************************
\'\'\'
def
InsertMongo(
self
, Lst):
self
.collection.insert(Lst)
def close(self): return self.conn.disconnect()
- 测试用例执行代码:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
import
time
ExcelSheet
=
ExcelSheet(
"104137PC.xlsx"
)
interFace
=
ExcelSheet.get_interFace()
interFaceArgList
=
ExcelSheet.get_interFaceArgList()
titleIndex
=
ExcelSheet.get_titleIndex()
loginInterFace
=
ExcelSheet.get_loginInterFace()
# 判断是否需要登录
if
loginInterFace:
if
"username"
not
in
titleIndex
or
"password"
not
in
titleIndex:
print
"Test Case File not include username or password"
exit()
else
:
# 获取登录接口参数的json
loginJson
=
ExcelSheet.get_loginJson()
caseList
=
list
()
for
line
in
range
(
5
, ExcelSheet.nrows()):
lineContent
=
ExcelSheet.get_by_line(line)
# 获取登录后的cookie
if
loginInterFace:
# 需要登录,用户名密码,替换
loginJson
=
loginJson.replace(
"#username#"
, lineContent[
"username"
])
loginJson
=
loginJson.replace(
"#password#"
,
str
(lineContent[
"password"
]))
result
=
postRequest(loginInterFace,
eval
(loginJson),
False
)
print
result.text
cookie
=
result.cookies
else
:
cookie
=
False
# reqtype 不填默认post
if
lineContent.has_key(
"reqtype"
):
if
lineContent[
"reqtype"
].upper()
=
=
"POST"
:
interFaceResult
=
postRequest(interFace, lineContent[
"data"
], cookie)
else
:
interFaceResult
=
postRequest(interFace, cookie)
else
:
interFaceResult
=
postRequest(interFace, lineContent[
"data"
], cookie)
req_result
=
interFaceResult.text
# 非页面,可直接比较,也可以转换成字典进行某个value进行比较
if
checkReturnResult(req_result):
if
checkReturnResult(req_result)
=
=
lineContent[
"Except_result"
]:
TestResult
=
"PASS"
else
:
TestResult
=
"FAIL"
#如果返回是页面的话,就查找特殊标志词
else
:
if
ineContent[
"Except_result"
]
in
req_result:
TestResult
=
"PASS"
else
:
TestResult
=
"FAIL"
lineContent[
"result"
]
=
TestResult
caseList.append(lineContent)
TestDate
=
time.strftime(
"%Y-%m-%d %H:%M:%S"
, time.localtime(time.time()))
content
=
{
"interFace"
: interFace,
"caseList"
: caseList,
"testdate"
: TestDate}
MyngoCls
=
ConMongoDb()
MyngoCls.InsertMongo(content)
MyngoCls.close()
整个流程梳理:
- 用例Excel的读取解析
- python的requests模块运用
- 返回值的判断处理
- 用例执行结果的存储
- 用例测试的入口
以上是关于Python 基于http接口自动化测试的主要内容,如果未能解决你的问题,请参考以下文章