Exemplo n.º 1
0
    def make_test_templates(self) -> dict:
        r"""创建测试用例的基础数据

        :Usage:
            make_test_templates()
        """

        logger.log_debug(self.request_body)
        method = GetJsonParams.get_value(self.request_body, 'method')

        if method in ['get', 'GET']:
            temp = ('url', 'params', 'headers', 'timeout')
            request_body = GetJsonParams.for_keys_to_dict(*temp, my_dict=self.request_body)
            if request_body['params']:
                if '=' in request_body.get('params') or '&' in request_body.get('params'):
                    request_body['params'] = dict(parse.parse_qsl(request_body['params']))

            logger.log_info("接受GET的请求参数为{}".format(
                json.dumps(request_body, indent=4, ensure_ascii=False))
            )

            try:
                response = self.get(**request_body)
                try:
                    response_body = response.json()
                except simplejson.JSONDecodeError:
                    response_body = response.text
                return {
                    "status_code": response.status_code,
                    "response_body": response_body
                }
            except exceptions.Timeout as error:
                raise error

        if method in ['post', 'POST']:
            temp = ('url', 'headers', 'json', 'data', 'files', 'timeout')
            request_body = GetJsonParams.for_keys_to_dict(*temp, my_dict=self.request_body)

            logger.log_info("接受POST的请求参数为{}".format(
                json.dumps(request_body, indent=4, ensure_ascii=False))
            )

            try:
                response = self.post(**request_body)
                try:
                    response_body = response.json()
                except simplejson.JSONDecodeError:
                    response_body = response.text
                return {
                    "status_code": response.status_code,
                    "response_body": response_body
                }
            except exceptions.Timeout as error:
                raise error
        else:
            raise exceptions.TestApiMethodError("接口测试请求类型错误, 请检查相关用例!")
Exemplo n.º 2
0
    def make_test_templates(self, question: dict):
        r"""Run the test request case.

        :param question:   The robot test problem, dict object.
        :return: request.Response dict object
        :rtype: request.Response
        """

        try:
            global out_values
            body = {}
            with open(self.request_file, 'r', encoding='utf-8') as file:
                items = yaml.load(file, Loader=yaml.FullLoader)

                for key, value in items.items():

                    if key == 'body':

                        relevance_body = relevance.custom_manage(
                            str(items['body']), question)
                        body.update(eval(relevance_body))
                    else:
                        out_values = items['outValues']

            method, args = GetJsonParams.get_value(body, 'method'), out_values

            logger.log_debug(json.dumps(body, indent=4))

            if method in ['get', 'GET']:
                temp = ('url', 'params', 'headers')
                request_body = GetJsonParams.for_keys_to_dict(*temp,
                                                              my_dict=body)
                if request_body['params']:
                    if '=' in request_body.get(
                            'params') or '&' in request_body.get('params'):
                        request_body['params'] = dict(
                            parse.parse_qsl(request_body['params']))

                return self.get(**request_body).json(), args

            if method in ['post', 'POST']:
                temp = ('url', 'headers', 'json', 'data', 'files')
                request_body = GetJsonParams.for_keys_to_dict(*temp,
                                                              my_dict=body)

                return self.post(**request_body).json(), args

        except SyntaxError:
            logger.log_warn('出错的问题数据是 => {}, 本次执行跳过请手动检查'.format(question))
        except (TimeoutError, exceptions.ConnectionError):
            logger.log_warn(
                '[WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败')
Exemplo n.º 3
0
 def send_mail(self):
     r"""发送测试报告邮件
     """
     self.email_content()
     self.msg['From'] = setting.EMAIL_CONF['sendaddr_name']
     self.msg['To'] = ','.join(self.receiver)
     server = smtplib.SMTP_SSL('smtp.qq.com', 465)
     try:
         server.login(setting.EMAIL_CONF['sendaddr_name'],
                      setting.EMAIL_CONF['sendaddr_pswd'])
         server.sendmail(setting.EMAIL_CONF['sendaddr_name'], self.receiver,
                         self.msg.as_string())
         logger.log_debug(
             'Please check if the email has been sent successfully.')
     except smtplib.SMTPException as error:
         logger.log_warn(error)
     finally:
         server.quit()
Exemplo n.º 4
0
def run_test_cases(case_path):
    r"""Run the test case and generate data for the rendering template.

    :param case_path: Test case file path, str object.
    :return: generate data for the rendering template.
    :rtype: tuple object.
    """
    start = time_setting.timestamp('format_now')
    start_time = time.time()

    for file_content in fp.iter_files(os.path.join(BASE_PATH, case_path)):

        logger.log_debug('一共执行的测试集为{}'.format(file_content))

        results = []

        for filename, filepath in dict(file_content).items():

            logger.log_info('本次运行的用例为{} => {}'.format(filename, filepath))
            raw_list = operate_excel.read_excel(filepath)
            logger.log_debug(raw_list)
            for index, content in enumerate(raw_list):
                result = {}
                header = {}

                intent, expected_know = content['intent'], content['knowledge']
                quest = str(int(content['question'])) if isinstance(content['question'], float) else content['question']
                know = os.path.split(expected_know)[-1] if str(expected_know).find('.jpg') else expected_know

                logger.log_debug('当前问题是 => {}'.format(quest))

                try:
                    http = http_hander.BaseKeyWords()
                    res, out_values = http.make_test_templates({'question': quest})
                    res_texts = GJ.get_value(my_dict=res, key=out_values[0])
                    res_headers = GJ.get_value(my_dict=res, key=out_values[1])
                    res_images = GJ.get_value(my_dict=res, key=out_values[2])
                    res_contents = GJ.get_value(my_dict=res, key=out_values[3])

                    logger.log_debug("问题:{},Text的值是->{} Header的值是->{} Image的值是->{} Content的值是->{}".\
                                     format(quest, res_texts, res_headers, res_images, res_contents))

                    res_text, res_header, res_image, res_content = '', '', '', ''
                    if isinstance(res_texts, Iterable):
                        for text in res_texts:
                            res_text += text.strip()
                    if isinstance(res_headers, Iterable):
                        for head in res_headers:
                            res_header += head.strip()
                    if isinstance(res_images, Iterable):
                        for image in res_images:
                            res_image += os.path.split(image)[-1]
                    if isinstance(res_contents, Iterable):
                        for con_val in res_contents:
                            res_content += dict(con_val)['Value']

                    if not isinstance(res_texts, Iterable) and isinstance(res_headers, Iterable):
                        res_text = res_header
                    if not isinstance(res_texts, Iterable) and isinstance(res_images, Iterable):
                        res_text = res_image
                    if not isinstance(res_texts, Iterable) and isinstance(res_contents, Iterable):
                        res_text = res_content
                    if isinstance(res_texts, Iterable) and isinstance(res_headers, Iterable) and isinstance(res_images, Iterable) and not isinstance(res_contents, Iterable):
                        res_text = res_header + res_text + res_image
                    if isinstance(res_texts, Iterable) and isinstance(res_headers, Iterable) and not isinstance(res_images, Iterable) and isinstance(res_contents, Iterable):
                        res_text = res_header + res_text + res_content
                    if isinstance(res_texts, Iterable) and not isinstance(res_headers, Iterable) and isinstance(res_images, Iterable) and isinstance(res_contents, Iterable):
                        res_text = res_text + res_content + res_image
                    if not isinstance(res_texts, Iterable) and isinstance(res_headers, Iterable) and isinstance(res_images, Iterable) and isinstance(res_contents, Iterable):
                        res_text = res_header + res_content + res_image
                    if not isinstance(res_texts, Iterable) and not isinstance(res_headers, Iterable) and isinstance(res_images, Iterable) and isinstance(res_contents, Iterable):
                        res_text = res_content + res_image
                    if isinstance(res_texts, Iterable) and not isinstance(res_headers, Iterable) and isinstance(res_images, Iterable) and not isinstance(res_contents, Iterable):
                        res_text = res_text + res_image
                    if not isinstance(res_texts, Iterable) and isinstance(res_headers, Iterable) and isinstance(res_images, Iterable) and not isinstance(res_contents, Iterable):
                        res_text = res_header + res_image
                    if isinstance(res_texts, Iterable) and isinstance(res_headers, Iterable) and not isinstance(res_images, Iterable) and not isinstance(res_contents, Iterable):
                        res_text = res_header + res_text
                    if isinstance(res_texts, Iterable) and not isinstance(res_headers, Iterable) and not isinstance(res_images, Iterable) and isinstance(res_contents, Iterable):
                        res_text = res_text + res_content
                    if not isinstance(res_texts, Iterable) and isinstance(res_headers, Iterable) and not isinstance(res_images, Iterable) and isinstance(res_contents, Iterable):
                        res_text = res_header + res_content
                    if not isinstance(res_headers, Iterable) and not isinstance(res_texts, Iterable) and not isinstance(res_images, Iterable) and not isinstance(res_contents, Iterable):
                        res_text = "answer.Text & answer.Header & answer.Image & content未返回任何值,机器人未根据意图查找出答案."

                except TypeError:
                    res_text = "Response Body is Null."
                except simplejson.errors.JSONDecodeError:
                    res_text = '抱歉,暂时没有相关的答案信息,我们已记录您的问题,感谢您的支持!'

                diff = JD.contrast_num(know, res_text)

                test_result = 'Pass' if diff > 0.7 else 'Fail'
                run = time.time()-start_time

                header.update({
                    'name': filename,
                    'start': start,
                    'run': run
                })

                result.update({
                    'question': quest,
                    'intent': intent,
                    'knowledge': know,
                    'response': res_text,
                    'diff': diff,
                    'result': test_result
                })

                results.append(result)
                make_report_template.create_test_report(filename, header, results)
Exemplo n.º 5
0
    def wrap(*args):

        for items in iter(TestContainer()):

            for key, value in dict(items).items():
                if value == func.__name__:

                    body = {}

                    # 用例文件与临时变量文件相互关联
                    # relevant_params = items.get('body').get('relevant_parameter')
                    relevant_params = GetJsonParams.get_value(
                        items, 'relevant_parameter')
                    if relevant_params:

                        if isinstance(relevant_params, list):
                            _relevance = {}

                            for relevant_param in relevant_params:
                                relevant_files = relevant_param + '.yaml'
                                for base_path in VariablesPathList:
                                    if exists(
                                            path.join(base_path,
                                                      relevant_files)):
                                        with open(base_path + relevant_files,
                                                  encoding='utf-8') as file:
                                            _relevance.update(
                                                yaml.safe_load(file))

                            # 判断关联文件中是否存在替换的变量,将其替换
                            _relevance_params = GetJsonParams.get_value(
                                _relevance, 'relevant_parameter')
                            if _relevance_params:

                                if isinstance(_relevance_params, list):
                                    _next_relevance = {}

                                    for _relevance_param in _relevance_params:
                                        _relevant_files = _relevance_param + '.yaml'
                                        for base_path in VariablesPathList:
                                            if exists(
                                                    path.join(
                                                        base_path,
                                                        _relevant_files)):
                                                with open(base_path +
                                                          _relevant_files,
                                                          encoding='utf-8'
                                                          ) as file:
                                                    _next_relevance.update(
                                                        yaml.safe_load(file))

                                    _relevance_body = relevance.custom_manage(
                                        str(_relevance), _next_relevance)
                                    _relevance.update(_relevance_body)

                            relevance_body = relevance.custom_manage(
                                str(items['body']), _relevance)
                            body.update(relevance_body)

                    # 运行用例,暂支持Post与Get请求接口
                    handler = http_keywords.BaseKeyWords(body)
                    result = handler.make_test_templates()

                    logger.log_info("本次用例执行的测试结果为{}".format(
                        json.dumps(result, indent=4, ensure_ascii=False)))

                    # 将临时变量写入yaml文件
                    res_index = items.get('body').get('res_index')
                    if res_index:
                        if isinstance(res_index, list):
                            for res_key in res_index:
                                return_res = GetJsonParams.get_value(
                                    result, res_key)
                                file_name = setting.EXTRACT_PARAMS + res_key
                                logger.log_debug(
                                    '保存的变量值为 => {} '.format(return_res))

                                with open(file_name + '.yaml',
                                          'w',
                                          encoding='utf-8') as file:
                                    file.write('{}: {}'.format(
                                        res_key, return_res))

                        if isinstance(res_index, str):
                            return_res = GetJsonParams.get_value(
                                result, res_index)
                            file_name = setting.EXTRACT_PARAMS + res_index
                            logger.log_debug('保存的变量值为 {}'.format(return_res))

                            with open(file_name, 'w',
                                      encoding='utf-8') as file:
                                file.write('{}: {}'.format(
                                    res_index, return_res))

                    # 验证接口请求数据是否落库
                    excep_columns, res_sql = {}, {}
                    relevant_database = items.get('body').get('relevant_sql')
                    if relevant_database:

                        if isinstance(relevant_database, list):
                            for relevant_db in relevant_database:
                                filename = relevant_db + '.yaml'

                                relevant_sql = {}
                                with open(setting.CASE_DATA + filename,
                                          'rb') as file:
                                    relevant_sql.update(yaml.safe_load(file))

                                action = relevant_sql[relevant_db]['action']
                                columns = relevant_sql[relevant_db]['execSQL'][
                                    'columns']
                                table = relevant_sql[relevant_db]['execSQL'][
                                    'table']
                                params = relevant_sql[relevant_db]['execSQL'][
                                    'params']
                                desc = relevant_sql[relevant_db]['execSQL'][
                                    'desc']
                                execute_sql = '{} {} FROM {} {} {}'.format(
                                    action, columns, table, params, desc)
                                execute_res = ExecuteSQL(
                                    DataBaseSetting).execute(execute_sql)[0][0]

                                res_sql.update({columns: execute_res})
                                logger.log_debug(
                                    '执行sql结果为{}'.format(execute_res))

                    return func(
                        *args,
                        response=result,
                        kwassert=items.get('body').get('assert'),
                        kwassert_same=items.get('body').get('assert_same_key'),
                        json_diff=items.get('body').get('json_diff'),
                        execute_res=res_sql,
                        db_check=items.get('body').get('check_db'))