1. 程式人生 > 其它 >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)
測試結果