开源接口自动化测试工具 HttpRunner

#开源接口自动化测试工具 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

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值