接口测试 (三)功能分类及配置文件引入

#data_test.py
from openpyxl import load_workbook
import json
from project_data.file_path import filepath
from project_data.cfg_read import Read_Url
class Date_test():

    def __init__(self):
        self.case_id = None
        self.url = None
        self.data = None
        self.Method = None
        self.expected = None
        self.sheet_name = None
        self.url = eval(Read_Url().Read_url())

    # 输出字典
    def Date_test_list(self,sheetname):
        """
        配置文件读取模块
        :return:
        """
        wb = load_workbook(filepath + "\project_data\cases.xlsx")
        list = []
        for j in wb.get_sheet_names():
            ws = wb[j]
            for i in range(2,ws.max_row + 1):
                if j == sheetname:
                    result = {}
                    result["case_id"] = ws.cell(i, 1).value
                    result["url"] = self.url + ws.cell(i,3).value
                    result["data"] = ws.cell(i,4).value
                    result["Method"] = ws.cell(i,5).value
                    result["expected"] = ws.cell(i,6).value
                    result["sheet_name"] = j
                    list.append(result)
        wb.close()
        return list
    #输出对象
    def Date_test_xlsx(self,sheetname):
        wb = load_workbook(filepath + "\project_data\cases.xlsx")
        list = []
        for j in wb.get_sheet_names():
            ws = wb[j]
            for i in range(2,ws.max_row + 1):
                if j == sheetname:
                    case = Date_test()
                    case.case_id = ws.cell(i, 1).value
                    case.url = self.url + ws.cell(i,3).value
                    case.data = ws.cell(i,4).value
                    case.Method = ws.cell(i,5).value
                    case.expected = ws.cell(i,6).value
                    case.sheet_name = j
                    list.append(case)
        wb.close()
        return list


    def Date_test_write(self, test_function, x,sheet_name):
        """
        运行结果写入模块
        :param test_function:
        :param test_text:
        :param x:
        :return:
        """
        Dict = {}
        wb = load_workbook(filepath+"\project_data\cases.xlsx")
        ws = wb[sheet_name]
        test_function = json.loads(test_function)
        Dict["status"] = test_function["status"]
        Dict["code"] = test_function["code"]
        Dict["msg"] = test_function["msg"]
        ws.cell(x+1, 7).value = str(Dict)
        if ws.cell(x+1,6).value == ws.cell(x+1, 7).value:
            ws.cell(x+1, 8).value = "PASS"
        else:
            ws.cell(x+1, 8).value = "FAIL"
        ws.cell(x+1, 9).value = str(test_function["data"])
        wb.save(filepath+"\project_data\cases.xlsx")
        wb.close()

    @staticmethod
    def Email_Date():
        username = "username"
        password = "password"
        To_Email = "To_Email_1,To_Email_2"
        return username, password, To_Email

#cfg_read.py
from configparser import ConfigParser
from project_data.file_path import filepath
class Read_Url():
    def Read_url(self):
        cf = ConfigParser()
        cf.read(filepath + "\project_data\http_console.cfg", encoding="utf-8")
        if cf.get(section="data_console", option="console"):
            hu = ConfigParser()
            hu.read(filepath + "\project_data\http_test.cfg", encoding="utf-8")
            return hu.get(section="Url_data", option="url")
        else:
            hu = ConfigParser()
            hu.read(filepath + "\project_data\http_www.cfg", encoding="utf-8")
            return hu.get(section="Url_data", option="url")
#file_path.py
import os

filepath = os.path.dirname(os.path.dirname(__file__))

注意:

   1.优化的模块均属于配置目录下,顺便引入了配置文件,方便域名的切换

   2.引入sheetname参数,方便用例功能分类

   3.博客园抽风了无法贴图,所以cfg配置文件我就不放了

   4.用例未做任何改动,只引入了sheetname参数,在data妆饰符中使用就行

   5.用例新增了投标模块,但是这方面灵活度很大,所以就不放上来了

 

posted @ 2019-04-18 21:44  桂木  阅读(360)  评论(0编辑  收藏  举报