接口测试框架——第六篇-读Excel封装方法

谢谢@小麦苹果的提醒,才发现我借口测试少写了一个文件,今天给大家补上:

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文件的方法。

微信公众号搜索“自动化测试实战”或扫描下方二维码添加关注~~~

posted @ 2017-11-16 18:13  孟船长  阅读(331)  评论(0编辑  收藏  举报