接口自動化框架(Python)之 三,base.py的配置

主要以讀取excel表的信息:

# -*- coding:utf-8 -*-
# @Time   : 2019-10-23
# @Author : carl_dj

import json
import requests
from openpyxl.styles import colors
from Interface_python3.public.http_service import HTTP
from Interface_python3.public import config,read_excel,write_excel



# 拼接url,path參數是域名後面的虛擬目錄部分
def get_url(path):
    return ''.join([config.base_url, path])


# 封裝requests請求方法,方法參數爲:請求方式,接口url,請求參數
def get_response(method, url, **DataALL):
    if method == 'get':
        resp = HTTP().get(url, **DataALL)
    elif method == 'put':
        resp = HTTP().put(url, **DataALL)
    elif method == 'post':
        resp = HTTP().post(url, **DataALL)
    elif method == 'delete':
        resp = HTTP().delete(url, **DataALL)
    else:
        return "no the method"
    resp.encoding = 'UTF-8'
    return resp


# 封裝requests請求方法,請求參數testdata數據是從Excel表讀取的
def get_excel_response(testdata):
    method = testdata["method"]  # 請求方式
    url = testdata["url"]  # 請求url

    # url後面的params參數
    try:
        params = eval(testdata["params"])
    except:
        params = None

    # 請求頭部headers
    try:
        headers = eval(testdata["headers"])
    except:
        headers = None

    # post請求body內容
    try:
        bodydata = eval(testdata["body"])
        # 可在這裏實現excel的body裏面某個字段動態賦值,實現接口參數的關聯,如token
        if 'accessToken' in testdata["body"]:
            bodydata['accessToken'] = config.accessToken
    except:
        bodydata = {}

    # post請求body類型,判斷傳data數據還是json
    type = testdata["type"]
    if type == "data":
        body = bodydata
    elif type == "json":
        body = json.dumps(bodydata)
    else:
        body = json.dumps(bodydata)

    # 發起網絡請求,並返回數據
    try:
        r = requests.request(method=method,
                             url=url,
                             params=params,
                             headers=headers,
                             data=body)
        r.encoding = 'UTF-8'
        return r
    except Exception as msg:
        return msg


# 這個是二次封裝讀取Excel表數據,返回的data是列表類型,列表中子元素是字典類型
def get_excel_data(file_name, sheet_name):
    # fileName是文件名(要帶後綴),sheetName是表名
    sheet = read_excel.ReadExcel(config.test_data_path + file_name, sheet_name)
    data = sheet.get_dict_data()
    return data


# 這個是二次封裝寫入Excel表數據,fileName是文件名,sheetName是表名,r是網絡請求結果
def write_to_excel(file_name, sheet_name, test_data, r):
    # 這裏的文件夾路徑要修改爲你的
    write_excel.copy_excel(config.test_data_path + file_name)  # 複製備份一份測試數據
    wt = write_excel.WriteExcel(config.test_data_path + file_name, sheet_name)
    row = test_data.get('rowNum')
    color = colors.BLACK
    try:
        if test_data.get('isCheckStatusCode'):
            if str(r.status_code) == test_data.get('checkpoint'):
                wt.write(row, 12, "pass", color)  # 測試結果 pass
            else:
                color = colors.RED
                wt.write(row, 12, "fail", color)  # 測試結果 fail
        else:
            if test_data.get("checkpoint") == '':
                wt.write(row, 12, "checkpoint爲空", colors.RED)  # 沒有設置檢查點的值
            elif test_data.get("checkpoint") in r.text:
                wt.write(row, 12, "pass", color)  # 測試結果 pass
            else:
                color = colors.RED
                wt.write(row, 12, "fail", color)  # 測試結果 fail

        wt.write(row, 10, str(r.status_code), color)  # 寫入返回狀態碼statuscode,第8列
        wt.write(row, 11, str(r.elapsed.total_seconds()), color)  # 耗時
        wt.write(row, 13, r.text, color)  # 響應內容
        wt.write(row, 14, "")  # 異常置空
        wt.wb.close()
    except Exception as msg:
        color = colors.RED
        wt.write(row, 10, "")
        wt.write(row, 11, "")
        wt.write(row, 12, "fail", color)
        wt.write(row, 13, "")
        wt.write(row, 14, str(r), color)
        wt.wb.close()
    return wt
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章