基於數據驅動思惟進行 Python 接口自動化測試

平時工做中開發在作自測或先後端聯調測試時會使用一些工具來進行,如postman,jmeter等。
但工具都是有自身侷限的,今天小編就以數據驅動的框架思惟來手工編碼進行接口自動化測試(基於python requests模塊)。python

設計思路:
一、 先設計好要測試數據,包括請求體裏的 url,method,headers,params,body_data 等數據。數據庫

二、 測試數據放哪裏?放文件,放excel,仍是放數據庫裏,能夠根據不一樣的業務需求來進行設計,我這裏以excel表格爲例進行說明。json

三、怎麼對測試數據進行讀取和寫入,達到測試接口的目標,要麼只是須要有預期結果和測試結果,須要進行編寫代碼來完成。後端

測試數據設計以下:session

基於數據驅動思惟進行 Python 接口自動化測試

host: 訪問的ip地址或域名
url: 接口的url地址
method: http接口的訪問方法,get仍是post,或是put等
headers: 接口的header信息框架

params: 接口的參數信息
data_tpye: 接口body的數據類型,是data仍是json
body_data: 接口的body數據
assert: 斷言
test result: 測試結果ide

核心代碼以下:工具

一、讀取excel文件,獲取行數和table。post

#!/usr/bin/env python
#-- coding: utf-8 -測試

import xlrd

def ReadExcel(fileName,SheetName="Sheet1"):
data = xlrd.open_workbook(fileName)
table = data.sheet_by_name(SheetName)
nrows = table.nrows

return nrows,table

二、 封裝 http 請求,根據http請求的返回值與斷言進行對比,向excel表中test result字段回寫測試結果

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

import requests
import json
import os.path

import xlrd
from xlutils.copy import copy

from comm.ReadExcel import ReadExcel

def SendRequests():
s = requests.session()
report_path = os.path.abspath(os.path.join(os.getcwd(), ".."))
fileName = report_path + '\testdata' + '\testdata.xls'

for i in range(1, ReadExcel(fileName)[0]):
    host = ReadExcel(fileName)[1].row_values(i)[2]
    url = ReadExcel(fileName)[1].row_values(i)[3]
    method = ReadExcel(fileName)[1].row_values(i)[4]
    headers = ReadExcel(fileName)[1].row_values(i)[5]
    params = ReadExcel(fileName)[1].row_values(i)[6]
    data_type = ReadExcel(fileName)[1].row_values(i)[7]
    body_data = ReadExcel(fileName)[1].row_values(i)[8]
    ex_assert = ReadExcel(fileName)[1].row_values(i)[9]
    testresult = ReadExcel(fileName)[1].row_values(i)[10]
    p_ex_assert = eval(ex_assert)

    try:
        if  headers == "":
            h_headers = None
        else:
            h_headers = eval(headers)

        if  params == "":
            p_params = None
        else:
            p_params = params

        if body_data == "":
            b_body_data = None
        else:
            b_body_data = eval(body_data)

        if data_type == "":
            data_type = None
        elif data_type == "json" :
            body = json.dumps(b_body_data)
        elif data_type == "data":
            body = b_body_data
        else:
            body = b_body_data

        re = s.request(method=method, url=host + url, headers=h_headers,params=p_params,data=body,verify=False)

        if p_ex_assert['success'] == str(re.json()['success']):
            workbook = xlrd.open_workbook(fileName)
            new_workbook = copy(workbook)
            new_worksheet = new_workbook.get_sheet(0)
            new_worksheet.write(i, 10, 'pass')
            new_workbook.save(fileName)
            print("pass")
        else:
            workbook = xlrd.open_workbook(fileName)
            new_workbook = copy(workbook)
            new_worksheet = new_workbook.get_sheet(0)
            new_worksheet.write(i, 10, 'fail')
            new_workbook.save(fileName)
            print("fail")

    except Exception as e:
        print(e)

三、運行腳本

if name == "main":

SendRequests()
相關文章
相關標籤/搜索