1. 程式人生 > >接口測試框架——第六篇-讀Excel封裝方法

接口測試框架——第六篇-讀Excel封裝方法

eight 轉碼 hcm hand .get bmf borde code inpu

謝謝@小麥蘋果的提醒,才發現我借口測試少寫了一個文件,今天給大家補上:

common->service->excel_case_data.py
# coding: utf-8

import json
import sys
import logging
import setting
import requests
from common.module import excel_module
from common.module import requests_module
from common.module import environment_module

sys.path.append("..")

class ExcelData():

    def __init__(self):

        self.url = ‘‘
        self.method = ‘‘
        self.data_send = ‘‘
        self.expect_res = ‘‘
        self.data = ‘‘
        self.case_url = ‘‘
        self.case_input = ‘‘
        self.content_type = ‘‘

    def get_case_data(self, file_name, sheet_index=0, row_id=0, data=None, **kwargs):
        """
        1、獲取對應id的行的內容
        2、獲取url
        3、獲取請求方式
        4、獲取請求參數,並進行轉碼
        5、獲取預期結果
        6、獲取string類型的response
        :param file_name: xlsx文件名
        :param sheet_index: sheet索引
        :param row_id: 行索引
        :param data: 不用Excel表裏的數據,自己傳
        :param kwargs: 替換excel表裏的某個key的value
        :return: expect_res, actual_res
        """
        # 讀取Excel
        excel_handle = excel_module.ReadExcel(file_name)
        # 獲取指定sheet
        sheet = excel_handle.get_sheet_by_index(sheet_index)
        # 讀取指定行
        case_data_list = excel_handle.get_row_values(sheet, row_id)
        # 獲取第row_id行第2列的數據
        path = case_data_list[1]
        print "path: ", path
        self.get_url(path)
        print self.get_url(path)
        # ID、Path、Request、Input、Expect、content-type、Remark
        # 獲取發送方式(Request)
        self.method = case_data_list[2]
        self.data_send = case_data_list[3]
        # 字符串轉字典
        if self.data_send != ‘‘:
            self.data = json.loads(self.data_send, encoding=‘utf-8‘)

        logging.info(self.data_send)
        if kwargs is not None:
            for i in kwargs:
                for j in self.data:
                    # 如果傳參key和發送內容key相同,則替換Excel表中的對應key的value
                    if i == j:
                        self.data[j] = kwargs[i]

        if data is not None:
            self.data = data

        expect_res = self.get_expect_data()
        # 遍歷傳參,如果有cookie字段則將該cookie賦值給access_token
        access_token = ‘‘
        for i in kwargs:
            if i == ‘cookie‘:
                access_token = kwargs[i]
        if (access_token.strip() == ‘‘):
            actual_res = self.get_actual_data()
        else:
            actual_res = self.get_actual_data(access_token=access_token)

        return expect_res, actual_res

    def get_case_input(self, file_name, sheet_index=0, row_id=0):
        """
        獲取輸入數據
        :param file_name: 文件路徑
        :param sheet_index: sheet索引
        :param row_id: 行索引
        :return: Excel表中的傳入數據
        """
        excel_handle = excel_module.ReadExcel(file_name)
        sheet = excel_handle.get_sheet_by_index(sheet_index)
        case_data = excel_handle.get_row_values(sheet, row_id)
        self.data = case_data[3]
        return self.data

    def get_url(self, path):
        pathStr = str(path)
        self.url = environment_module.Environment_module().get_env_url(‘login‘) + path

    def get_expect_data(self):
        logging.debug("=============Expect============" + self.expect_res)
        return self.expect_res.encode(‘utf-8‘)

    def get_actual_data(self, **kwargs):
        # headers = setting.REQUEST_HEADER
        # headers[‘Content-Type‘] = self.content_type[‘Content-Type‘]
        # for i in kwargs:
        #     if i == "access_token":
        #         headers[‘Cookies‘] = kwargs[i]
        actual_res_handle = requests_module.GetResponse(self.url, method=self.method)
        actual_url = actual_res_handle.get_response(data=self.data)
        res_analysis = requests_module.AnalysisResponse(actual_url)
        actual_res = res_analysis.Str_Content
        cookies = requests.utils.dict_from_cookiejar(res_analysis.Cookies)
        # logging.debug(u"===============data==============") + json.dumps(self.data)
        logging.debug((u"===========實際返回的數據為:%s============") % actual_res)
        return actual_res, cookies

讀取excel文件的方法。

微信公眾號搜索“自動化測試實戰”或掃描下方二維碼添加關註~~~

技術分享

?

接口測試框架——第六篇-讀Excel封裝方法