开源接口自动化测试工具 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: '1528790980362'
url: /user/center/v1/uic/login/secure #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://ent-test.dtwave-inc.com:8081/login?redirect=/'
}
#测试环境配置
# connfig_url = "http://ent-test.dtwave-inc.com:8081"
# living = "http://47.97.38.124:8701" #测试 serverd地址
# eop = "http://eop-test.dtwave-inc.com:8081"
# productId = "308"
#线上环境配置
connfig_url = "http://ent.dtwave-inc.com"
living = "http://118.31.174.193:8701" #测试 serverd地址
eop = "http://eop.dtwave-inc.com"
productId = "308"
#租户信息http://ent.dtwave-inc.com/
# name = "test0713"
# password = "b319bc32f956f5b880fa54717130d3a5" #密码
name = "dtwave_test_0714"
password = "e1d9ed80f11b892a70b9a408553dad89" #密码线上
#是否需要检测任务运行情况,当任务没运行时,只检查ddl任务
examine = True
#新建数据同步任务是否运行
task_run = True
#是否需要重新创建项目
project_creat = False #需要重新创建项目填写Flase 不需要填写True
#是否需要创建数据源
bool_db = False #例如想在同一项目下创建工作流,任务进行多次测试,可将bool_db的值修改为true,则会跳过创建数据源的过程,直接在开发中心新建任务执行操作 注:跑多次时,需要更改工作流名称
#是否线上实例化
task_prd = 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 = "http://eop-test.dtwave-inc.com:8081/api/v1/dubhe/instance/count_instance_data"
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://ent-test.dtwave-inc.com:8081',
'Cookie': 'Hm_lvt_522167c58906c0b56bf56855f72052ce=1528288578,1528853855,1529029588,1529044559; Hm_lpvt_522167c58906c0b56bf56855f72052ce=1529044559; sessionId={}'.format(sessionId)
}
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 = 'http://eop-test.dtwave-inc.com:8081/api/v1/dubhe/instance/query_instance_list'
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': 'http://ent-test.dtwave-inc.com:8081',
'Cookie': 'Hm_lvt_522167c58906c0b56bf56855f72052ce=1528288578,1528853855,1529029588,1529044559; Hm_lpvt_522167c58906c0b56bf56855f72052ce=1529044559; sessionId={}'.format(sessionId)
}
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代码实现。