2 Star 2 Fork 0

tuboyou / requestQ

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

request

介绍

接口测试

软件架构

使用python的requests进行封装了一个简单框架requestQ,正在试用 #安装

pip install requestQ

#使用

1.基本组成

  • dorequest #基本发起http请求的单元
  • item #对dorequest单元进行校验取值的单元,可视为一个接口测试的用例
  • Case/Cases #具体用例的概念,可以由多个item整合,公用变量

1.1 样例

from requestQ import item,Case,Cases
item1=item("name1",data1).expect('data.code').toBe(1).expect('data.status').toBeTruthy().save('data.token','token')
item2=item("name1",{"aaa":"asdada${token}"}).expect('data.code').toBe(1).expect('data.status').toBeTruthy()
...
Cases(Case("casename",item1,item2,...),Case(xxxx))

2.dorequest使用

from requestQ.package.dorequest import DoRequest
req= DoRequest()
# DoRequest 主要有三个方法
# 1. add_session_headers(header:dict) 添加公共的请求头,类似cookie
# 2. run(method: str, url: str, data=None, is_auto=True, **kwargs) 进行请求校验
# 3. commit() 对请求进行提交

# DoRequest 主要有三个常量
# req.status 查看执行状态
# req.e 查看执行错误
# req.is_commit 查看是否提交

2.1 add_session_headers的使用

#填写dict格式的请求头即可
req. add_session_headers({"cookie":"asdadad12123123"})

2.2 run的使用

#样例对百度进行访问
req.run("get","http://www.baidu.com")
#参数说明
# method 请求方法 get post 等,必填
# url 请求链接,必须要添加http或者https,必填
# data 请求体内容,支持dict格式,str格式,"xxx=yyy&sss=www"的格式,选填
# is_auto 是否自动提交,默认自动,即run方法后不需要再添加commit方法
# kwargs dict格式,选填,支持的名字暂时有:
#  files 格式str,上传文件的路径 
#  file_name 格式str,用于设置上传文件名称,不填默认文件名称
#  headers 格式dict,{xxx:yyy,sss:www} 设置请求头
#  session_headers 格式dict,{xxx:yyy,sss:www} 设置通用请求头
#  proxies 设置请求代理 格式 ip:port

2.3 commit的使用

req.commit()
#返回提交的结果或者报错

3 item使用

from requestQ import item
item(name: str,  data: dict, des='', func: str = 'http', priority='中')
#参数说明
# name 此条请求的名称,标识,唯一值,必填
# data dict格式,内容是func对应的参数,为了方便以后扩展使用,当前输入请求参数
# des  此条请求的描述,无相关作用,只是描述而已
# func str格式,默认是http请求,可扩展成mysql等
# priority str格式,没有作用,只是标注优先级,待报告中显示使用

# item主要有八个方法
# run(func) 此处是调试时使用,类似使用 item(xxx).run(Dorequest())
# debug(func) 此处是调试时使用,类似使用 item(xxx).debug(Dorequest()),中间有停顿,方便查看数据数据
# addLocal(local: dict, glo: dict) 此处添加本地变量和总变量,格式必须是加在run和debug之前
# save(catch: str, name: str, method: str = 'json', is_global=False)保存值到变量数据中
# expect(catch: str, method: str = 'json') 提取结果的值,用户后续处理,主要方法!
# print_log() 打印出运行日志
# do_req(func)  func(请求值,本地变量,公共变量),在run之前定义方法,提前处理请求的值
# do_res(func) func(结果值,本地变量,公共变量),在run之前定义方法,在校验前处理结果值


#item还有协助的方法,不参与用例,只是方便编写data的值
# fetch(url, data, replace=None)
# 使用方法,chrome浏览器中,在network中找到需要的请求,右击-复制-复制为fetch,
#然后直接黏贴,删除分号就可以使用,返回值为请求的data值,缺陷是无法获取到cookie的值
#raw(str) 
#使用方法,类似fiddler抓包,在raw页面,全选-复制,使用raw('''xxxx'''),黏贴即可解析过data的值

3.1 run/debug 的使用

item(name: str,  data: dict, des='', func: str = 'http', priority='中').run(Dorequest())
item(name: str,  data: dict, des='', func: str = 'http', priority='中').expect('data.code').toBe(1).debug(Dorequest())
#此时就直接运行脚本,查看结果
#此处的data可以支持参数化,格式为:${xxxx},这个xxxx的值为环境变量的key

3.2 addLocal的使用

item(name: str,  data: dict, des='', func: str = 'http', priority='中').addLocal({"a":1},{"b":2}).run(Dorequest())
#此处添加变量中的参数

3.3 save的使用

#样例
item(name: str,  data: dict, des='', func: str = 'http', priority='中').save("data.token","token").run(Dorequest())
#运行后,在localdict中添加了key为token,value为结果提取的值
#参数说明
save(catch: str, name: str, method: str = 'json', is_global=False)
#catch 抓取的方式:例如结果为{“data”:{"status":1,"list":[{"s":1},{"d":2}]}}
#取status的值为:data.status
#取s的值为 data.list.0.s
#name是暂存的值得key
#method是提取的方法,模式使用json模式,就是上述样例中的方法;还有regex和size
#regex是正则提取的方法,类似于xxx(.\+)1313
#size是直接返回值得长度

3.4 print_log的使用

item(name: str,  data: dict, des='', func: str = 'http', priority='中').run(Dorequest()).print_log()
#打印出日志信息

3.5 expect的使用

#样例
item("name1",{"aaa":"asdada${token}"}).expect('data.code')
#参数说明,参考save
#expect就是把值取出来,然后进行比较,比较的方法有:
#toBe(val)、notToBe(val)、notToBeNone()、toBeNone()、toBeTruthy()、toBeFalsy()、
#toBeGreaterThanOrEqual(num)、toBeGreaterThan(num)、toBeLessThan(num)、toBeLessThanOrEqual()
#toMatch(val)、notToMatch(val)、toContain(val)、notToContain(val)
#上述val为值,任何格式的值;num为数字;Match的val是正则

4 Case/Cases的使用

Case("casename",item(xxxx))
#Case 就是完整的一个用例的概念,有一个或者多个item组成
#参数说明:
#name,用例的名称
#items ,任意数量的item
#主要方法有两个:
# add_source(source, name) 用于扩展后续方法使用
# run( allow_print_detail=[], allow_print_res=True) 用于运行item

4.1 add_source的使用

#样例
Case(xxx).add_source(Domysql(),"mysql")
#用于后期扩展

4.2 run的使用

#样例
Case(xxx).run()
#参数说明
# allow_print_detail 格式:list,输入item的名字,显示具体的日志,默认不显示,选填
# allow_print_res 模式True,显示简单的运行结果,选填

Cases(Case(xx)...)
#运行执行的用例,无方法

安装教程

  1. xxxx
  2. xxxx
  3. xxxx

使用说明

  1. xxxx
  2. xxxx
  3. xxxx

参与贡献

  1. Fork 本仓库
  2. 新建 Feat_xxx 分支
  3. 提交代码
  4. 新建 Pull Request

码云特技

  1. 使用 Readme_XXX.md 来支持不同的语言,例如 Readme_en.md, Readme_zh.md
  2. 码云官方博客 blog.gitee.com
  3. 你可以 https://gitee.com/explore 这个地址来了解码云上的优秀开源项目
  4. GVP 全称是码云最有价值开源项目,是码云综合评定出的优秀开源项目
  5. 码云官方提供的使用手册 https://gitee.com/help
  6. 码云封面人物是一档用来展示码云会员风采的栏目 https://gitee.com/gitee-stars/
MIT License Copyright (c) 2020 l454124613 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

接口测试 展开 收起
Python
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Python
1
https://gitee.com/tuboyou/requestQ.git
git@gitee.com:tuboyou/requestQ.git
tuboyou
requestQ
requestQ
master

搜索帮助