Python教程

python-request 实现企业微信接口自动化-1(DDT)

本文主要是介绍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)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!