コード例 #1
0
ファイル: get_data.py プロジェクト: liuzouzou/api_test_report
class GetData(object):

    def __init__(self):
        self.opera_excel = OperationExcel()
        self.sheet = self.opera_excel.get_data()

    '''获取excel的行数,就是我们的case个数'''

    def get_case_lines(self):
        try:
            return self.opera_excel.get_lines()
        except Exception as e:
            logging.info("错误为:", e)

    '''获取是否执行'''

    def get_is_run(self, row):
        try:
            flag = None
            col = get_run()
            run_model = self.opera_excel.get_cell_value(row, col)
            if run_model == 'y':
                flag = True
            else:
                flag = False
            return flag
        except Exception as e:
            logging.info("错误为:", e)

    '''获取接口描述'''

    def get_api_msg(self, row):
        try:
            col = get_request_name()
            api_msg = self.opera_excel.get_cell_value(row, col)
            return api_msg
        except Exception as e:
            logging.info("错误为:", e)

    '''获取接口名称'''

    def get_api_name(self, row):
        try:
            col = get_api_name()
            api_name = self.opera_excel.get_cell_value(row, col)
            return api_name
        except Exception as e:
            logging.info("错误为:", e)

    '''是否携带header'''

    def get_request_headers(self, row):
        try:
            col = get_header()
            header = self.opera_excel.get_cell_value(row, col)
            if header == '':
                return None
            return header
        except Exception as e:
            logging.info("错误为:", e)

    '''是否有header'''

    def is_header(self, row):
        try:
            col = get_header()
            header = self.opera_excel.get_cell_value(row, col)

            if header != 'n':
                header = json.loads(header)
                return header
                # return self.opera_excel.get_cell_value(row,col)
            elif header == 'w':
                return 'w'
            else:
                return None
        except Exception as e:
            logging.info("错误为:", e)

    '''获取请求方式'''

    def get_request_method(self, row):
        try:
            col = get_run_way()
            request_method = self.opera_excel.get_cell_value(row, col)
            return request_method
        except Exception as e:
            logging.info("错误为:", e)

    '''获取url'''

    def get_request_url(self, row):
        try:
            col = get_url()
            url = self.opera_excel.get_cell_value(row, col)
            return url
        except Exception as e:
            logging.info("错误为:", e)

    '''请求数据'''

    def get_request_data(self, row):
        try:
            col = get_data()
            data = self.opera_excel.get_cell_value(row, col)
            if data == '':
                return None
            return data
        except Exception as e:
            logging.info("错误为:", e)

    '''通过获取关键字拿到data数据'''

    def get_data_for_json(self, row):
        try:
            opera_json = OperationJson()
            request_data = opera_json.get_data(self.get_request_data(row))
            return request_data
        except Exception as e:
            logging.info("错误为:", e)

    '''获取要提取的字段的值'''

    def get_depend_value(self, row):
        try:
            col = get_depend_value()
            depend_value = self.opera_excel.get_cell_value(row, col)
            if depend_value == '':
                return None
            return depend_value
        except Exception as e:
            logging.info("错误为:", e)

    '''获取预期结果'''

    def get_expect_data(self, row):
        try:
            col = get_expect()
            expect = self.opera_excel.get_cell_value(row, col)
            if expect == '':
                return None
            return expect
        except Exception as e:
            logging.info("错误为:", e)

    '''获取http状态码'''

    def get_http_code_data(self, row):
        try:
            col = get_http_code()
            http_code = self.opera_excel.get_cell_value(row, col)
            return http_code
        except Exception as e:
            logging.info("错误为:", e)

    '''获取待替换的值'''

    def get_waiting_replace_key(self, row):
        try:
            col = get_waiting_replace_key()
            wait_rep_key = self.opera_excel.get_cell_value(row, col)
            if wait_rep_key == '':
                return None
            return wait_rep_key
        except Exception as e:
            logging.info('错误为:', e)

    '''获取实际替换的值'''

    def get_actual_replace_key(self, row):
        try:
            col = get_actual_replace_key()
            act_rep_key = self.opera_excel.get_cell_value(row, col)
            if act_rep_key == '':
                return None
            return act_rep_key
        except Exception as e:
            logging.info('错误为:', e)

    '''写入数据'''

    def write_result(self, row, value):
        try:
            col = get_result()
            self.opera_excel.write_value(sheet=self.sheet, content=value, rowNo=row, colsNo=col)
        except Exception as e:
            logging.info("错误为:", e)

    '''获取依赖返回数据的key'''

    def get_depend_key(self, row):
        try:
            col = get_data_depend()
            depent_key = self.opera_excel.get_cell_value(row, col)
            if depent_key == "":
                return None
            else:
                return depent_key
        except Exception as e:
            logging.info("错误为:", e)

    def is_depend(self, row):  # 是否有依赖
        try:
            col = get_case_depend()
            depend_case_id = self.opera_excel.get_cell_value(row, col)
            if depend_case_id == "":
                return None
            else:
                return depend_case_id
        except Exception as e:
            logging.info("错误为:", e)

    '''获取数据依赖字段'''

    def get_depend_field(self, row):
        try:
            col = get_field_depend()
            data = self.opera_excel.get_cell_value(row, col)
            if data == "":
                return None
            else:
                return data
        except Exception as e:
            logging.info("错误为:", e)

    '''获取请求方式'''

    def get_set_key(self, row):
        try:
            col = get_set_key()
            request_method = self.opera_excel.get_cell_value(row, col)
            return request_method
        except Exception as e:
            logging.info("错误为:", e)
コード例 #2
0
ファイル: run_test.py プロジェクト: liuzouzou/api_test_report
class TestRun(object):
    def __init__(self):
        self.run_method = RunMethod()
        self.data = GetData()
        self.common_util = CommonUtil()
        self.send_email = SendEmail()
        self.excel = OperationExcel()
        self.sheet_obj = self.excel.get_data()

    def test_run(self):

        res = None
        pass_count = []
        fail_count = []
        skip_count = []
        # 获取多少行
        rows_count = self.data.get_case_lines()

        for i in range(2, rows_count + 1):
            run_num = [2, 1]
            is_run = self.data.get_is_run(i)
            message = self.data.get_api_msg(i)
            api_name = self.data.get_api_name(i)
            if is_run:

                url_path = self.data.get_request_url(i)

                url = config_global.base_url[0] + url_path

                method = self.data.get_request_method(i)

                data = self.data.get_request_data(i)

                code = self.data.get_http_code_data(i)

                expect = self.data.get_expect_data(i)

                headers = self.data.get_request_headers(i)

                depend_Value = self.data.get_depend_value(i)  # tiquzhi

                set_key = self.data.get_set_key(i)

                wait_key = self.data.get_waiting_replace_key(i)

                actual_key = self.data.get_actual_replace_key(i)

                header = get_header_value()

                if data != None:
                    if '88888888' in data:
                        data = data.replace("88888888", "17154654546")
                    data = json.loads(data)

                if headers != None:
                    try:
                        with open(depend_data_path, 'r',
                                  encoding='utf-8') as f:
                            header_value = json.load(f)
                            header[headers] = header_value["token"]

                    except Exception as e:
                        logging.info("错误为:", e)

                depend_case = self.data.is_depend(i)

                if depend_case != None:
                    self.depend_data = DependentData(depend_case)
                    # 获取的依赖响应
                    depend_response_data = self.depend_data.get_data_for_key(i)

                    # 获取依赖的key
                    depend_key = self.data.get_depend_field(i)

                    # 更新参数值
                    data[depend_key] = depend_response_data
                '''待提取的key'''
                if wait_key != None:

                    try:
                        with open(depend_data_path, 'r',
                                  encoding='utf-8') as f:

                            dependvalue = f.read()

                            depend_value_dict = json.loads(dependvalue)

                        wait_key_list = str_for_list(wait_key)
                        actual_key_list = str_for_list(actual_key)

                        for index_num in range(len(wait_key_list)):
                            Wait_key = wait_key_list[index_num]
                            Act_key = actual_key_list[index_num]
                            depend_value_key = depend_value_dict[Act_key]
                            data[Wait_key] = depend_value_key

                    except Exception as e:
                        logging.info("错误为:", e)

                def fail_run():
                    response = self.run_method.run_main(
                        method, url, data, header)
                    res = response[0]
                    # print('response---->',response)
                    # print('res---->',res)
                    global http_code
                    http_code = response[1]

                    # print(type(http_code), http_code)

                    # 断言

                    if int(code) == http_code:

                        if self.common_util.is_contain(expect, res):
                            logging.info('\033[1;32;m%s接口:%s->接口执行通过\033[0m' %
                                         (api_name, message))
                            writeTestResult(excelObj.get_data(),
                                            rowNo=i,
                                            testResult='pass')
                            pass_count.append(i)

                            if depend_Value != None:
                                self.depend_data = DependentData(depend_case)
                                self.depend_data.save_depend_value(
                                    res, depend_Value, set_key)

                        else:
                            fail = run_num[0]
                            fail -= 1
                            run_num[0] = fail

                            while run_num[0]:
                                fail_num = run_num[1]
                                run_num[1] = run_num[1] + 1
                                print('第%s次执行失败,开始第%s次执行。。。' %
                                      (fail_num, fail_num + 1))

                                time.sleep(1)
                                logging.info(
                                    "\033[0;43;41m%s接口:%s->接口执行失败\033[0m" %
                                    (api_name, message))

                                fail_run()

                            writeTestResult(excelObj.get_data(),
                                            rowNo=i,
                                            testResult='faild',
                                            errorInfo=res)
                            fail_count.append(i)

                    else:
                        fail = run_num[0]
                        fail -= 1
                        run_num[0] = fail

                        while run_num[0]:
                            fail_num = run_num[1]
                            run_num[1] = run_num[1] + 1

                            print('第%s次执行失败,开始第%s次执行。。。' %
                                  (fail_num, fail_num + 1))

                            time.sleep(1)
                            logging.info("\033[0;43;41m%s->接口执行失败\033[0m" %
                                         message)

                            fail_run()

                        writeTestResult(excelObj.get_data(),
                                        rowNo=i,
                                        testResult='faild',
                                        errorInfo=res)
                        fail_count.append(i)

                fail_run()
            else:
                logging.info('%s接口:%s->接口不执行' % (api_name, message))
                skip_count.append(i)
                writeTestResult(excelObj.get_data(),
                                rowNo=i,
                                testResult='skip')

            # 发送邮件
        logging.info("正在发送邮件,请等待。。。")
コード例 #3
0
ファイル: test_run.py プロジェクト: liuzouzou/api_test_report
class TestAllCase(unittest.TestCase):
    def setUp(self):
        self.run = TestRun()
        self.data = GetData()
        self.run_method = RunMethod()
        self.common_util = CommonUtil()
        self.send_email = SendEmail()
        self.excel = OperationExcel()
        self.sheet_obj = self.excel.get_data()

    @ddt.data(*all_data)
    def test_all_case(self, data):  # 提取值 提取值的key 待替换的key 实际替换的key
        get_num, get_case_des, get_api, get_exec, get_module, get_pre, get_url, get_header, get_method, get_parames, get_hope_http_code, get_hope_response, get_results, get_run_time, get_error_msg, get_depend_Value, get_setting_key, get_wait_key, get_actual_key = data
        print(get_num, get_case_des, get_api, get_exec, get_module, get_pre,
              get_url, get_header, get_method, get_parames, get_hope_http_code,
              get_hope_response, get_results, get_run_time, get_error_msg,
              get_depend_Value, get_setting_key, get_wait_key, get_actual_key)
        print("=" * 20)
        print(*all_data)

        # run.test_run()
        res = None
        pass_count = []
        fail_count = []
        skip_count = []

        run_num = [2, 1]

        is_run = get_exec  # exec

        message = get_case_des

        api_name = get_api
        if is_run == 'y':

            url_path = get_url

            url = config_global.base_url[0] + url_path

            method = get_method

            data = get_parames

            code = get_hope_http_code

            expect = get_hope_response

            headers = get_header

            depend_Value = get_depend_Value  # tiquzhi

            set_key = get_setting_key

            wait_key = get_wait_key

            actual_key = get_actual_key

            header = get_header_value()

            if data != None:
                if '88888888' in data:
                    data = data.replace("88888888", "17154654546")
                data = json.loads(data)

            if headers != None:
                try:
                    with open(depend_data_path, 'r', encoding='utf-8') as f:
                        header_value = json.load(f)
                        header[headers] = header_value["token"]

                except Exception as e:
                    logging.info("错误为:", e)

            # depend_case = data.is_depend(num)
            depend_case = get_num
            #
            # if depend_case != None:
            #     self.depend_data = DependentData(depend_case)
            #     # 获取的依赖响应
            #     depend_response_data = self.depend_data.get_data_for_key(i)
            #
            #     # 获取依赖的key
            #     depend_key = self.data.get_depend_field(i)
            #
            #     # 更新参数值
            #     data[depend_key] = depend_response_data
            '''待提取的key'''
            if wait_key != None:

                try:
                    with open(depend_data_path, 'r', encoding='utf-8') as f:

                        dependvalue = f.read()

                        depend_value_dict = json.loads(dependvalue)

                    wait_key_list = str_for_list(wait_key)
                    actual_key_list = str_for_list(actual_key)

                    for index_num in range(len(wait_key_list)):
                        Wait_key = wait_key_list[index_num]
                        Act_key = actual_key_list[index_num]
                        depend_value_key = depend_value_dict[Act_key]
                        data[Wait_key] = depend_value_key

                except Exception as e:
                    logging.info("错误为:", e)

            def fail_run():

                response = self.run_method.run_main(method, url, data, header)
                res = response[0]
                # print('response---->',response)
                # print('res---->',res)
                global http_code
                http_code = response[1]

                # print(type(http_code), http_code)

                # 断言

                if int(code) == http_code:

                    if self.common_util.is_contain(expect, res):
                        logging.info('\033[1;32;m%s接口:%s->接口执行通过\033[0m' %
                                     (api_name, message))
                        self.assertTrue(True)  # 断言
                        writeTestResult(excelObj.get_data(),
                                        rowNo=get_num,
                                        testResult='pass')
                        pass_count.append(get_num)

                        if depend_Value != None:
                            self.depend_data = DependentData(depend_case)
                            self.depend_data.save_depend_value(
                                res, depend_Value, set_key)

                    else:
                        fail = run_num[0]
                        fail -= 1
                        run_num[0] = fail

                        while run_num[0]:
                            fail_num = run_num[1]
                            run_num[1] = run_num[1] + 1
                            print('第%s次执行失败,开始第%s次执行。。。' %
                                  (fail_num, fail_num + 1))

                            time.sleep(1)
                            logging.info(
                                "\033[0;43;41m%s接口:%s->接口执行失败\033[0m" %
                                (api_name, message))

                            fail_run()

                        writeTestResult(excelObj.get_data(),
                                        rowNo=get_num,
                                        testResult='faild',
                                        errorInfo=res)
                        self.assertTrue(False, msg=res)  # 断言

                        fail_count.append(get_num)

                else:
                    fail = run_num[0]
                    fail -= 1
                    run_num[0] = fail

                    while run_num[0]:
                        fail_num = run_num[1]
                        run_num[1] = run_num[1] + 1

                        print('第%s次执行失败,开始第%s次执行。。。' %
                              (fail_num, fail_num + 1))

                        time.sleep(1)
                        logging.info("\033[0;43;41m%s->接口执行失败\033[0m" %
                                     message)

                        fail_run()

                    writeTestResult(excelObj.get_data(),
                                    rowNo=get_num,
                                    testResult='faild',
                                    errorInfo=res)
                    fail_count.append(get_num)

            fail_run()
        else:
            logging.info('%s接口:%s->接口不执行' % (api_name, message))
            skip_count.append(get_num)
            writeTestResult(excelObj.get_data(),
                            rowNo=get_num,
                            testResult='skip')