學習python的第一個小目標:經過requests+xlrd實現簡單接口測試,將測試用例維護在表格中,與腳本分開。

點點小白的學習方式:經過肯定一個小目標來想辦法實現它,再經過筆記來加深印象。python

面對標題中的小目標我陷入了思考。。。。嗯,首先實現利用xlrd庫來取出想要的用例json

首先用表格準備好用例,如圖下api

先試下取number爲1的一行用例試試:post

 1 #!/usr/bin/env python
 2 # -*- coding: utf-8 -*-
 3 import xlrd
 4 
 5 xlsurl = r"D:\RequestAPI.xlsx"  # 表格文件路徑
 6 rqapi = xlrd.open_workbook(xlsurl)   # 得到文件對象
 7 sheet_name = rqapi.sheet_names()[0]  # 獲取表格第一個sheet名稱
 8 sheet1 = rqapi.sheet_by_name(sheet_name)  # 獲取表格第一個sheet對象
 9 nrow = sheet1.nrows   # 獲取表格行總數
10 ncols = sheet1.ncols  # 獲取表格列總數
11 col_data = sheet1.col_values(0)  # 獲取表格第一列的數據
12 row_data = sheet1.row_values(1)  # 獲取表格第二行的數據
13 print "======================================================================="
14 print row_data
15 print type(row_data)

執行結果如圖:學習

能夠看到,表格中number爲1的一行數據已經打印出來了,而且是list類型的,這樣經過遍歷list能夠將數據都當個取出來,不過我沒這麼作,由於表格中paramete是保存的請求參數,在requests庫中,請求參數是dict格式的,因此不須要把每一個參數都遍歷出來(requests庫的使用在筆記後面別急)。url

搞定取單行數據,那接下來再取每一行的數據,並把每行中須要的列也取出來,上代碼:spa

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import xlrd

class ApiTest:
    def xlsee(self, xlsFile):
        rqapi = xlrd.open_workbook(xlsFile)   # 得到文件對象
        sheet_name = rqapi.sheet_names()[0]  # 獲取第一個sheet名稱
        sheet = rqapi.sheet_by_name(sheet_name)  # 獲取第一個sheet對象
        return sheet


if __name__ == "__main__":
        apitest = ApiTest()
        xlsFile = r"D:\RequestAPI.xlsx"  # 文件路徑
        sheet1 = apitest.xlsee(xlsFile)
        nrow = sheet1.nrows   # 獲取行總數
        ncols = sheet1.ncols  # 獲取列總數
        col_data = sheet1.col_values(0)  # 獲取第一列的數據

        for i in range(1, nrow):  # 循環每行,並獲取每行每列的值,第0行是標題,因此從第一行開始取
             row_data = sheet1.row_values(i)  # 獲取第i行的數據
             nums = int(row_data[0])  # 用例編號
             rqtypes = str(row_data[2])  # 請求類型
             rqurls = str(row_data[1])  # 請求地址
             a = row_data[5:]  # 請求參數,而且一次取出這一行全部的參數
             parametes = dict(zip(a[0::2], a[1::2]))    # 將list類型的參數轉換爲dict字典類型的
             code = row_data[3]  # 校驗結果用的code狀態碼
             coderesult = row_data[4]  # 用於顯示校驗結果的參數pass或error
             print "=================================================================================="
             print "用例編號:", nums
             print "請求類型", rqtypes
             print "請求地址", rqurls
             print "請求參數parametes:", parametes
             print "校驗結果用的code狀態碼", code
             print "用於顯示校驗結果的參數", coderesult

對於上面代碼,簡單封裝了一下,注意code

a = row_data[5:]  # 請求參數,而且一次取出這一行全部的參數

這個利用list[a:]來取值,可取第a位後面全部的數據,這就代表,我可在表格中設置N個參數,而不會限制於不一樣接口參數個數不同的限制了
上面代碼運行結果以下:

從表格中讀取用例的問題已經搞定了,接下來就是經過requests來執行這些用例,上代碼:對象

class ApiTest:
    # 請求主方法
    def request(self, rqtype, rqurl, paramete, headers):
        self.rqurl = rqurl  # API地址
        self.rqtype = rqtype  # 請求類型get or post
        self.paramete = paramete  # 請求參數
        self.headers = headers  # 請求頭

        if rqtype == "get":
            apirqhttp = requests.get(url=rqurl, params=paramete, headers=headers)  # 發送請求
            code = apirqhttp.status_code  # 保存返回狀態
            pam = apirqhttp.text  # 保存返回數據並將json轉爲dict
            return code, pam
        if rqtype == "post":
            apirqhttp = requests.post(url=rqurl, data=paramete, headers=headers)
            code = apirqhttp.status_code
            pam = apirqhttp.text
            return code, pam
        else:
            print "請求參數錯誤,請求類型只支持get+post,請求地址支持string,參數支持dict"

 

其中有個小插曲,就是接口中常見的身份驗證問題,須要獲取token值,將token值做爲herders請求頭來進行身份校驗blog

因而再經過登陸接口來獲取這個token(注意:token值校驗不一樣項目可能不同,好比咱們公司就在touken值前面加了個brarer ,因此須要再將token值拼接一下)值並參數化,上代碼:

    # 獲取B2B分銷商token值方法
    def seltoken(self):
        rqtypes = "post"
        rqurls = "http://xxxxx.dddd.com//account/authorize"
        parametes = {"username": "Wbfxs001", "password": "111111Qq", "grant_type": "password"}
        headers = None
        cod, pam = ApiTest().request(rqtypes, rqurls, parametes, headers)  # 掉用request方法請求登陸
        pam = json.loads(pam)  # 保存返回數據並將json轉爲dict
        access_token = pam["access_token"]  # 截取dic中access_token鍵的value
        access_token = "bearer " + str(access_token)  # 拼接access_token爲最終須要的token值
        return access_token

 

最後將xlrd+requests方法都封裝好,上代碼(上面代碼都整合在這裏了):

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2017-04-13 18:39
import requests, xlrd, xlrd, json


class ApiTest:
    # 請求主方法
    def request(self, rqtype, rqurl, paramete, headers):
        self.rqurl = rqurl  # API地址
        self.rqtype = rqtype  # 請求類型get or post
        self.paramete = paramete  # 請求參數
        self.headers = headers  # 請求頭

        if rqtype == "get":
            apirqhttp = requests.get(url=rqurl, params=paramete, headers=headers)  # 發送請求
            code = apirqhttp.status_code  # 保存返回狀態
            pam = apirqhttp.text  # 保存返回數據並將json轉爲dict
            return code, pam
        if rqtype == "post":
            apirqhttp = requests.post(url=rqurl, data=paramete, headers=headers)
            code = apirqhttp.status_code
            pam = apirqhttp.text
            return code, pam
        else:
            print "請求參數錯誤,請求類型只支持get+post,請求地址支持string,參數支持dict"

    # 獲取B2B分銷商token值方法
    def seltoken(self):
        rqtypes = "post"
        rqurls = "http://xxxxx.dddd.com//account/authorize"
        parametes = {"username": "Wbfxs001", "password": "111111Qq", "grant_type": "password"}
        headers = None
        cod, pam = ApiTest().request(rqtypes, rqurls, parametes, headers)  # 掉用request方法請求登陸
        pam = json.loads(pam)  # 保存返回數據並將json轉爲dict
        access_token = pam["access_token"]  # 截取dic中access_token鍵的value
        access_token = "bearer " + str(access_token)  # 拼接access_token爲最終須要的token值
        return access_token

    def xlsee(self, xlsFile):
        rqapi = xlrd.open_workbook(xlsFile)   # 得到文件對象
        sheet_name = rqapi.sheet_names()[0]  # 獲取第一個sheet名稱
        sheet = rqapi.sheet_by_name(sheet_name)  # 獲取第一個sheet對象
        return sheet

if __name__ == "__main__":
    apitest = ApiTest()
    xlsFile = r"D:\RequestAPI.xlsx"  # 文件路徑
    sheet1 = apitest.xlsee(xlsFile)
    nrow = sheet1.nrows   # 獲取行總數
    ncols = sheet1.ncols  # 獲取列總數
    col_data = sheet1.col_values(0)  # 獲取第一列的數據

    for i in range(1, nrow):  # 循環每行,並獲取每行每列的值
         row_data = sheet1.row_values(i)  # 獲取第i行的數據
         nums = int(row_data[0])  # 獲取第i行的某個數據
         rqtypes = str(row_data[2])
         rqurls = str(row_data[1])
         a = row_data[5:]
         parametes = dict(zip(a[0::2], a[1::2]))
         code = row_data[3]
         coderesult = row_data[4]

         access_token = apitest.seltoken()  # 獲取token
         headers = {"Authorization": access_token}
         codetest, pamtest = apitest.request(rqtypes, rqurls, parametes, headers)
         print "用例編號:", nums, "code碼:", codetest
         print pamtest

 

感謝學習期間「大師兄」和「飯哥」的幫助~

相關文章
相關標籤/搜索