Ejemplo n.º 1
0
class AnalyseData(object):
    """
    定义对接口测试数据进行分析的类,包含的方法有:
    1.导出测试数据到Excel中
    """

    def __init__(self):
        self.field = config.filed_excel  # 初始化配置文件
        self.db = OperationDbInterface(link_type=1)  # 初始化数据库操作类,返回元组

    def export2excel(self, name_export):
        """

        :param name_export: 待导出的接口名称,列表形式
        :return:
        """
        counts_export = len(name_export)  # 导出总数
        fail_export = []  # 导出失败接口列表
        try:
            src = open_workbook(config.src_path + '/report/report_module.xls', formatting_info=True)
            destination = copy(src)
            dt = datetime.datetime.now().strftime("%Y%m%d%H%M%S")  # 当前的时间戳
            filepath = config.src_path + '/report/' + str(dt) + '.xls'
            destination.save(filepath)  # 保存模板表格到新的目录下
            for name_interface in name_export:
                cases_interface = self.db.select_all("SELECT * FROM case_interface "
                                                     "WHERE case_status = 1 "
                                                     "and name_interface = '%s'" % name_interface)
                if len(cases_interface.get('data')) != 0 and cases_interface.get('code') == '0000':
                    src = open_workbook(filepath, formatting_info=True)
                    destination = copy(src)
                    sheet = destination.add_sheet(name_interface, cell_overwrite_ok=True)
                    for col in range(0, len(self.field)):
                        sheet.write(0, col, self.field[col])  # 获取并写入数据段信息到Sheet中
                    for row in range(1, len(cases_interface['data']) + 1):
                        for col in range(0, len(self.field)):
                            sheet.write(row, col, '%s' % cases_interface['data'][row - 1][col])  # 写数据到对应Excel表中

                    destination.save(filepath)
                elif len(cases_interface['data']) == 0 and cases_interface['code'] == '0000':
                    fail_export.append(name_interface)
                else:
                    fail_export.append(name_interface)
            result = {'code': '0000', 'message': '导出总数: %s , 失败数: %s' % (counts_export, len(fail_export)),
                      'data': fail_export}

        except Exception as e:
            MyLog.error(e)
            result = {'code': '9999', 'message': '导出过程异常|导出总数: %s , 失败数: %s' % (counts_export, len(fail_export)),
                      'data': fail_export}
        finally:
            return result
Ejemplo n.º 2
0
class Run(object):
    def __init__(self):
        self.db = OperationDbInterface()
        self.cases_to_run = []
        self.cache = Cache(maxsize=256, ttl=0, timer=time.time)

    def get_all_cases(self):
        """
        获取所有测试用例
        :return: []
        """
        sql = 'select * from case_interface'
        print(self.db.select_all(sql))

    def get_one_case(self, condition):
        """
        获取一条测试用例
        :return: []
        """

    def get_cases_for_run(self):
        """
        获取要执行的测试用例
        :return:
        """
        pass

    def execute_test_case(self, *arg):
        """
        执行测试用例
        :param arg:
        :return:
        """
        test_interface = RequestInterface()
        sql = "SELECT * FROM `case_interface` WHERE case_status = 1"
        result = self.db.select_all(sql)
        if result.get('code') == '0000' and result.get('data'):
            MyLog.debug('获取执行接口成功')
            datas = result.get('data')
            for temp_case_interface in datas:
                obj = extraDB(temp_case_interface, self.cache)
                params_interface = obj.replace()  # 执行测试前重构测试数据
                url_interface = params_interface.get('url_interface')
                id_case = temp_case_interface.get('id')
                name_interface = temp_case_interface.get('name_interface')
                name_case = temp_case_interface.get('name_case')
                headdata = ast.literal_eval(params_interface.get('header_interface'))
                type_interface = params_interface.get('exe_mode')
                if url_interface != '' and headdata != '' and type_interface != '':
                    temp_level_check = temp_case_interface.get('check_level')  # 检查级别

                    result_http_respones = test_interface.http_request(url_interface, headdata,
                                                                       params_interface.get('params_interface'),
                                                                       type_interface)
                    print(result_http_respones)
                    MyLog.debug("用例返回消息:{result}".format(result=result_http_respones.get('data')))
                    obj.setvar(result_http_respones.get('data'))
                    self.db.op_sql("UPDATE case_interface  SET result_interface = '%s' where id = %s " %
                                   (result_http_respones.get('data'), id_case))  # 将返回包数据写入用例表

                    if result_http_respones['code'] == '0000' and len(result_http_respones['data']) != 0:
                        base_compare = CompareParam(temp_case_interface)
                        if '0' in list(temp_level_check):  # 执行关键值参数检查
                            result_compare_code = base_compare.compare_code(result_http_respones.get('data'))
                            MyLog.debug('用例编号:%s|检查级别:关键字参数值|接口名称:%s|用例名称:%s|提示信息:%s \n'
                                        % (id_case, name_interface, name_case, result_compare_code['message']))
                        if '1' in list(temp_level_check):  # 执行参数完整性检查
                            result_compare_params_complete = base_compare.compare_params_complete(
                                result_http_respones.get('data'))
                            MyLog.debug('用例编号:%s|检查级别:参数完整性|接口名称:%s|用例名称:%s|提示信息:%s \n'
                                        % (id_case, name_interface, name_case, result_compare_params_complete['message']))
                    elif len(result_http_respones['data']) == 0:
                        MyLog.debug('接口名称: %s|信息错误:获取用例数据为空,请检查用例\n' % name_interface)
                    else:
                        MyLog.debug('接口名称: %s|信息错误:获取用例数据失败' % name_interface)
Ejemplo n.º 3
0
import pytest
import allure
from common.opmysql import OperationDbInterface
# from common.analyse import AnalyseData
from common.request import RequestInterface
from common.compare import CompareParam
# from public import config
from common.Log import MyLog

cases_list = [
]  # 测试用例集,里面放tuple[(name_interface1,case1),(name_interface2,case2)]
base_request = RequestInterface()
base_db = OperationDbInterface()

module_execute = base_db.select_all(
    "SELECT value_config from config_total "
    "WHERE key_config = 'exe_setup' and `status` = 1")  # 获取待执行接口数据
if len(module_execute.get('data')) != 0 and module_execute.get(
        'code') == '0000':
    for module_execute_one in module_execute.get('data'):
        temp_module_execute = eval(module_execute_one.get('value_config'))
        for temp_name_interface, condition in temp_module_execute.items():
            # print("###########开始执行接口:%s############\n" % temp_name_interface)
            temp_level_check = condition.get('level_check')  # 检查级别
            temp_level_exe = tuple(condition.get('level_exe'))  # 执行级别
            data_case_interface = base_db.select_all(
                "SELECT * FROM case_interface WHERE "
                "case_status = 1 AND "
                "name_interface =  '%s' AND exe_level in %s" %
                (temp_name_interface, temp_level_exe))  # 获取接口测试数据