环境准备

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)的更多相关文章

  1. Python+Pytest+Allure+Git+Jenkins接口自动化框架

    Python+Pytest+Allure+Git+Jenkins接口自动化框架 一.接口基础 接口测试是对系统和组件之间的接口进行测试,主要是效验数据的交换,传递和控制管理过程,以及相互逻辑依赖关系. ...

  2. Python Django对接企业微信第三方服务回调验证的一些坑

    今天公司老总,叫我把公司的企业微信,服务商管理后台中的本地应用进行回调验证. 听起来一脸懵逼,没搞过企业微信对接情况.一头雾水,不知道如何下手. 先讲解一下,企业微信情况. 登录到企业微信后,右上角服 ...

  3. Python+Unittest+Requests+PyMysql+HTMLReport 接口自动化框架

    整体框架使用的是:Python+Unittest+Requests+PyMysql+HTMLReport  多线程并发模式 主要依赖模块 Unittest.Requests.PyMysql.HTMLR ...

  4. python实现通过企业微信发送消息

    实现了通过企业微信发送消息,平时用于运维的告警还是不错的,相对于邮件来说,实时性更高,不过就是企业微信比较麻烦,此处不做过多解释. 企业微信api的详细请看:http://work.weixin.qq ...

  5. Python+excel实现的简单接口自动化 V0.1

    好久没写博客了..最近忙着工作以及新工作的事.. 看了下以前写的简单接口自动化,拿出来总结下,也算记录下学习成果 先来贴一下最后的结果,结果是写在原来的excel中 执行完毕后,会将结果写入到“状态” ...

  6. 接口自动化之ddt

    接口自动化会用到数据驱动模式,也就是一个ddt模块 目录 1.环境准备 2.调用时标准格式 3.应用(结合excle来传值) 1.环境准备 首先,需要安装ddt模块 pip install ddt 2 ...

  7. Python+request 分模块存放接口,多接口共用参数URL、headers的抽离,添加日志打印等《三》

    主要介绍内容如下: 1.分模块存放接口 2.多接口共用参数URL.headers的抽离为配置文件 3.添加日志打印 4.一个py文件运行所有所测的接口 如上介绍内容的作用: 1.分模块存放接口:方便多 ...

  8. python(pytest)+allure+jenkins 实现接口自动化的思路

    效果图镇楼: 上述各模块作用: python(pytest): 1:用于读测试用例(本次用例写在csv文件中) 2:环境配置相关 3:提取1中的测试数据,组成请求体 4:发送请求 5:获取结果 6:断 ...

  9. 【python接口自动化】- DDT数据驱动测试

    简单介绍 ​ DDT(Date Driver Test),所谓数据驱动测试,简单来说就是由数据的改变从而驱动自动化测试的执行,最终引起测试结果的改变.通过使用数据驱动测试的方法,可以在需要验证多组数据 ...

随机推荐

  1. 【LeetCode】81. 搜索旋转排序数组 II

    81. 搜索旋转排序数组 II 知识点:数组,二分查找: 题目描述 已知存在一个按非降序排列的整数数组 nums ,数组中的值不必互不相同. 在传递给函数之前,nums 在预先未知的某个下标 k(0 ...

  2. InnoDB锁机制-转载

    InnoDB锁机制 1. 锁类型 锁是数据库区别与文件系统的一个关键特性,锁机制用于管理对共享资源的并发访问. InnoDB使用的锁类型,分别有: 共享锁(S)和排他锁(X) 意向锁(IS和IX) 自 ...

  3. 使用Fileupload完成文件的上传下载

    目录 使用Fileupload完成文件的上传下载 为什么需要进行文件上传下载? 引入jar包 文件上传 注意事项 编写一个简单的文件上传jsp页面 编写Servlet Student类用于封装数据,后 ...

  4. Linux下MySQL基础及操作语法

    什么是MySQL? MySQL是一种开源关系数据库管理系统(RDBMS),它使用最常用的数据库管理语言-结构化查询语言(SQL)进行数据库管理.MySQL是开源的,因此任何人都可以根据通用公共许可证下 ...

  5. eslint+prettier+husky+lint-staged 统一前端代码规范

    eslint+prettier+husky+lint-staged 统一前端代码规范 遵循编码规范和使用语法检测,可以很好的提高代码的可读性,可维护性,并有效的减少一些编码错误. 1.终极目标 团队中 ...

  6. 前端 | Nuxt.js axios baseURL,proxy 代理

    平时用 Vue 写前端时,对于 axios 请求的常规操作一般是 统一定义好一个 axios 对象,使用 axios.defaults.baseURL 设置 baseURL 也不是不能直接把服务器地址 ...

  7. WPF 图表控件之曲线绘制与移动

    目的:绘制简单轻量级的曲线视图 二.实现效果: 1,绘制标准基准线 2,可拖动 三.用到控件 1,Canvas 2,Ellipse XAML代码: <Canvas Background=&quo ...

  8. mac 软件意外退出

    大概率的原因是软件签名问题. 先安装 xcode xcode-select --install 然后签名 sudo codesign --force --deep --sign - 文件位置(直接将应 ...

  9. .NET第三方补丁工具(Visual Patch)常用手册

    SetupFactory简介 这是Indigo Rose(蓝玫瑰)公司开发的一套打包-补丁解决方案的补丁工具,相比Setup Factory,他的知名度似乎不太高,网上也很少找到相关资料,但是真的很简 ...

  10. noip18

    T1 来自cf原题 考场直接暴力枚举 \(A,B\),15pts. 正解: 首先时间的表达式,\(T=\frac{A}{a_{i}}+\frac{B}{b_{i}}\),然后以\(\frac{1}{a ...