python-request 实现企业微信接口自动化-1(DDT)
环境准备
python+requests
读取企业微信api开发文档,得知调用企业微信接口必须先获取企业微信的accesstoken是通过 ("corpid","") //企业id 和 ("corpsecret","") //企业密钥 调用获取,python代码实现如下
requests_url='https://qyapi.weixin.qq.com/cgi-bin/gettoken?' # 获取token的接口地址
requests_data={"corpid":"ww2371688596076","corpsecret":"okaj3sQZSGneNs4IHkp5RD8j3v_7ZSa8IHF6Y"}
res=requests.get(requests_url,requests_data)
print("text解析的结果",res.text)
print("json解析的结果",res.json)
封装请求方法
class httpRequest:
def http_request(self,url,data,http_method): #定义接口请求方法
#写法1:
if http_method.upper()=='GET':
try:
res=requests.get(url,data)
except Exception as e:
print("get请求报错:{0}".format(e))
raise # 抛出异常
elif http_method.upper()=='POST':
try:
res = requests.get(url, data)
except Exception as e:
print("post请求报错:{0}".format(e))
raise # 抛出异常
else:
print("请求方法错误")
return res # 返回结果
调用封装的方法请求获取token
#主方法入口
if __name__ == '__main__':
res2=httpRequest().http_request(requests_url,requests_data,'post')
print("测试返回的token为:{}".format(res2))
整个方法如下:
#获取企业微信的接口地址,获取对应token
requests_url='https://qyapi.weixin.qq.com/cgi-bin/gettoken?'
requests_data={"corpid":"ww237168859620176","corpsecret":"okaj3sQJmGneNjh4s4IHkp5RD8j3v_7ZSa8IHF6Y"}
res=requests.get(requests_url,requests_data)
class httpRequest:
def http_request(self,url,data,http_method): #定义接口请求方法
#写法1:
if http_method.upper()=='GET':
try:
res=requests.get(url,data)
except Exception as e:
print("get请求报错:{0}".format(e))
raise # 抛出异常
elif http_method.upper()=='POST':
try:
res = requests.get(url, data)
except Exception as e:
print("post请求报错:{0}".format(e))
raise # 抛出异常
else:
print("请求方法错误")
return res # 返回结果
#写法2
# try:
# if http_method.upper() == 'GET':
# res=requests.get(url,data)
# elif http_method.upper() == 'POST':
# res = requests.get(url, data)
# except Exception as e:
# print("请求方法错误.{e}".format(e))
# raise e
# return res
#主方法入口
if __name__ == '__main__':
res2=httpRequest().http_request(requests_url1,requests_data,'post')
print("测试返回的结果:{}".format(res2.text))
print("测试返回的token为:{}".format(json.loads(res2.text)["access_token"])) # loads操作字符串获取对应的key
代码架构
执行类:
#代码启动类
from tools.http_request import httpRequest as testmethod # 导入http_request取别名
from tools.DoExcel import DoExcel
import json
def run(test_data): #列表嵌套的字典数据格式
for item in test_data: # for循环获取 testdata 数据 ,并执行用来
print("正在测试用例为{0}".format(item['title']))
login_res=testmethod().http_request(item["url"],item["data"],item["http_method"])
print("请求的结果是:{0}".format(login_res.json()))
test_data = [{"url": "https://qyapi.weixin.qq.com/cgi-bin/gettoken?",
"data": {"corpid": "ww2371688596201076",
"corpsecret": "okaj3sQZSWJmGneNjh4s4IHkp5R8j3v_7ZSa8IHF6Y"},
"title":"正确获取token","http_method":"get"},
{"url": "https://qyapi.weixin.qq.com/cgi-bin/gettoken?",
"data": {"corpid": "", "corpsecret": "okaj3sQZSWJmGneNjh4s4Hkp5RD8j3v_7ZSa8IHF6Y"},
"title":"参数错误","http_method":"get"}]
run(test_data)
执行结果:
数据驱动
将接口测试用例存放在excel中
获取excel数据的方法
#从xls 读取信息
from openpyxl import load_workbook
class DoExcel:
def get_data(self,file_name,sheet_name):
wb=load_workbook(file_name)
sheet=wb[sheet_name]
# 遍历获取数据
test_data=[] #初始化
for i in range(2, sheet.max_row+1):
row_data={}
row_data['case_id']=sheet.cell(i,1).value #获取1列的值
row_data['url']=sheet.cell(i,2).value #获取2列的值
row_data['data']=sheet.cell(i,3).value #获取3列的值
row_data['title']=sheet.cell(i,4).value #获取4列的值
row_data['http_method'] = sheet.cell(i, 5).value # 获取5列的值
test_data.append(row_data)
return test_data
#回写返回的结果值到excle
def write_back(self,file_name,sheet_name,i,value):
wb=load_workbook(file_name)
sheet=wb[sheet_name]
sheet.cell(i,6).value=value # 写死第6列 返回的结果
wb.save(file_name) #保存结果
if __name__ == '__main__':
data=DoExcel().get_data("../test_data/data1.xlsx","login") # 传入filename 与 sheet名
print(data)
python-request 实现企业微信接口自动化-1(DDT)的更多相关文章
- Python+Pytest+Allure+Git+Jenkins接口自动化框架
Python+Pytest+Allure+Git+Jenkins接口自动化框架 一.接口基础 接口测试是对系统和组件之间的接口进行测试,主要是效验数据的交换,传递和控制管理过程,以及相互逻辑依赖关系. ...
- Python Django对接企业微信第三方服务回调验证的一些坑
今天公司老总,叫我把公司的企业微信,服务商管理后台中的本地应用进行回调验证. 听起来一脸懵逼,没搞过企业微信对接情况.一头雾水,不知道如何下手. 先讲解一下,企业微信情况. 登录到企业微信后,右上角服 ...
- Python+Unittest+Requests+PyMysql+HTMLReport 接口自动化框架
整体框架使用的是:Python+Unittest+Requests+PyMysql+HTMLReport 多线程并发模式 主要依赖模块 Unittest.Requests.PyMysql.HTMLR ...
- python实现通过企业微信发送消息
实现了通过企业微信发送消息,平时用于运维的告警还是不错的,相对于邮件来说,实时性更高,不过就是企业微信比较麻烦,此处不做过多解释. 企业微信api的详细请看:http://work.weixin.qq ...
- Python+excel实现的简单接口自动化 V0.1
好久没写博客了..最近忙着工作以及新工作的事.. 看了下以前写的简单接口自动化,拿出来总结下,也算记录下学习成果 先来贴一下最后的结果,结果是写在原来的excel中 执行完毕后,会将结果写入到“状态” ...
- 接口自动化之ddt
接口自动化会用到数据驱动模式,也就是一个ddt模块 目录 1.环境准备 2.调用时标准格式 3.应用(结合excle来传值) 1.环境准备 首先,需要安装ddt模块 pip install ddt 2 ...
- Python+request 分模块存放接口,多接口共用参数URL、headers的抽离,添加日志打印等《三》
主要介绍内容如下: 1.分模块存放接口 2.多接口共用参数URL.headers的抽离为配置文件 3.添加日志打印 4.一个py文件运行所有所测的接口 如上介绍内容的作用: 1.分模块存放接口:方便多 ...
- python(pytest)+allure+jenkins 实现接口自动化的思路
效果图镇楼: 上述各模块作用: python(pytest): 1:用于读测试用例(本次用例写在csv文件中) 2:环境配置相关 3:提取1中的测试数据,组成请求体 4:发送请求 5:获取结果 6:断 ...
- 【python接口自动化】- DDT数据驱动测试
简单介绍 DDT(Date Driver Test),所谓数据驱动测试,简单来说就是由数据的改变从而驱动自动化测试的执行,最终引起测试结果的改变.通过使用数据驱动测试的方法,可以在需要验证多组数据 ...
随机推荐
- 安装Linux的步骤 包含linux下安装jdk,及mysql
https://mirrors.tuna.tsinghua.edu.cn/centos/7.9.2009/isos/x86_64/ 镜像下载网址,4G 左右. 安装VMware 15版本 一路下一步, ...
- .Net Core+分布式配置中心(AgileConfig)
GitHub上AgileConfig非常详细,以下只是本人学习过程的整理 一.介绍 1.参考地址 https://github.com/dotnetcore/AgileConfig https://w ...
- windows的基本命令和环境配置
刚开始学的时候整理的基本知识 一.windows的基础知识: 1.常用的DOS命令 盘符切换: D: 回车 进入目录: cd xxx 回退目录 返回到上一级: cd .. 返回到根目录: cd / ...
- 接口自动化测试之httprunner初探
❝ 今天来介绍一个python的一个开源项目:httprunner,接口自动化工具.第一次输入,难免有不周到的地方,轻喷~ ❞ 介绍: HttpRunner是一个简单优雅但功能强大的 HTTP(S) ...
- rancher清理主机脚本
#!/bin/bash #From:rancher #date:2019-10-18 #admin:jarno # 停止服务 systemctl disable kubelet.service sys ...
- Linux性能优化-平均负载
Linux性能优化-平均负载 目录 Linux性能优化-平均负载 平均负载的含义 平均负载为多少时合理 平均负载与 CPU 使用率 平均负载案例分析 场景一:CPU 密集型进程 场景二:I/O 密集型 ...
- 线程 Thread类 GIL锁 信号量 Event事件
线程的开启方法 进程是操作系统调度的最小单位,一个进程最少有一个主线程,而一个进程中可以开启多个线程 from threading import Thread def task(): print('A ...
- 见微知著 带你透过内存看 Slice 和 Array的异同
hi, 大家好,我是 hhf. 有这么一个 Go 面试题:请说出 slice 和 array 的区别? 这简直就是送分题.现在思考一下,你咋样回答才能让面试官满意呢? 我这里就不贴这道题的答案了.但是 ...
- wpf 中的DataTemplate 绑定控件
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" x ...
- 【java se】java注解
目录: 1.注解概述 2.常见的 Annotation 示例 3.自定义 Annotation 4.JDK 中的元注解 5.利用 反射获取注解信息(在反射部分 涉及) 6.JDK 8 中注解的新特性 ...