#开源接口自动化测试工具 HttpRunner
##优点
1、只要熟悉http协议,有一定的python基础,就可以很简单上手使用。
2、支持js、yaml等格式,结构清晰。
- test:
name: 用户登录(新) #用例名称
request:
headers: #头部信息
json:
loginType: '1'
password: $password
userName: $name
method: POST #请求方法
params: #路径参数
_api: login.userLogin
_mock: 'false'
_stamp: ''
url: xxxxxxxxxxxxxxxx #url
validate:
- eq: [content.success,true] #断言
extract:
- sessionId: content.content.sessionId #将接口返回数据提取出来
- userId: content.content.userId
3、测试用例与python完美结合,使代码和数据分离,可复用性和可维护性较高,适应不同的测试环境。
import requests
import datetime
import json
from time import sleep
import random
import os
from requests_toolbelt import MultipartEncoder
import sys
import base64
# reload(sys)
# sys.setdefaultencoding('utf-8')
from requests_toolbelt import MultipartEncoder
connfig_headers = {
'Connection': 'keep-alive',
'Pragma': 'no-cache',
'Cache-Control': 'no-cache',
'Accept': 'application/json, text/json',
'X-Requested-With': 'XMLHttpRequest',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36',
'Content-Type': 'application/json;charset=utf-8',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
'Referer': 'http://xxxxxxxxxxxxxxxxxxxxxxxxx/login?redirect=/'
}
#测试环境配置
# connfig_url = "xxxxxxxxxxxxxxxxxxx"
# living = "http://xxxxxxxxxxxxx" #测试 \地址
# eop = "http://xxxxxxxxxxxxxxxxxxxx"
# productId = "308"
#线上环境配置
connfig_url = "xxxxxxxx"
living = "http://xxxxxxxxx" #测试 \地址
eop = "xxxxxxxxxxx"
productId = "xxx"
#租户信息http://xxxxxxxxxxx/
# name = "xxxxxx"
# password = "xxxxxx" #密码
name = "xxxx"
password = "xxxxxx" #密码线上
#是否需要检测任务运行情况,当任务没运行时,只检查ddl任务
examine = True
4、开源、源码为python,可扩展性强,可根据自己的需求,更改源码、进行二次开发。
5、断言比较灵活,返回结果清晰。
6、可以对复杂接口进行请求。
例如:
需要统计一些长时间运行任务的运行结果,一些接口测试自动化工具测试比较麻烦,例如jmeter,因为不知道任务什么时候运行完成,只写一个接口很难实现,但是httprunner通过与python接口,可以用python来解决这些复杂的问题,将Python出来过的数据再返还给用例。
def living_task(sessionId,wsId,flowId):
i = 1
a = []
while i > 0:
sleep(60)
url = "xxxxxx"
headers = {
'Connection': 'keep-alive',
'Pragma': 'no-cache',
'Cache-Control': 'no-cache',
'Accept': 'application/json, text/json',
'X-Requested-With': 'XMLHttpRequest',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36',
'Content-Type': 'application/json;charset=utf-8',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
'Referer': 'xxxxxx',
'Cookie': 'xxxxxxxxx
}
payload = {'wsId':wsId,'time':"{} 00:00:00".format(today),'flowId':flowId}
reson = requests.get(url,headers=headers,params=payload)
reson_dict = json.loads(reson.text)
running = reson_dict.get("content").get("status").get("running")
unrun = reson_dict.get("content").get("status").get("unrun")
if running == 0 and unrun == 0:
url_failed = 'xxxxxxxxxxxxxxxxxx'
headers_1 = {
'Connection': 'keep-alive',
'Pragma': 'no-cache',
'Cache-Control': 'no-cache',
'Accept': 'application/json, text/json',
'X-Requested-With': 'XMLHttpRequest',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36',
'Content-Type': 'application/json;charset=utf-8',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
'Referer': 'xxxxxxxx',
'Cookie': 'xxxxxxx'
}
paylad = {'wsId':wsId,'time':"{} 00:00:00".format(today),'flowId':flowId,'pageSize':1000,'instanceStatus':"failed"}
resons = requests.get(url_failed,headers=headers_1,params=paylad)
resons_dict = json.loads(resons.text)
data = resons_dict.get("content").get("data")
for living in data:
task = living.get("taskCode")
a.append("{}任务运行失败".format(task))
return a
##缺点
1、开发时间比较短,技术不够成熟,有好多坑需要踩。
2、版本更新迭代快,有些功能更新版本后却无法使用。
3、有很多不支持的功能,需要使用python代码实现。
##官方快速上手文档
http://cn.httprunner.org/quickstart/#_3