Exemplo n.º 1
0
    def run(self,
            project_name,
            pattern='test*.py',
            ordered=False,
            html_report=True,
            send_email=False):
        # 创建测试用例
        print("run", project_name)
        suite = self.create_suite(project_name=project_name,
                                  ordered=ordered,
                                  pattern=pattern)
        print("create_suite", project_name)

        #生成测试报告并运行测试用例
        if not html_report:
            runner = unittest.TextTestRunner()
            runner.run(suite)
        else:
            from InterfaceAuto.common.HTMLTestRunner_PY3 import HTMLTestRunner

            now = time.strftime("%y-%m-%d-%H-%M-%S",
                                time.localtime(time.time()))
            report_path = dir_report_path + now + ".html"
            print(report_path)
            with open(report_path, "wb") as f:
                runner = HTMLTestRunner(
                    stream=f,
                    verbosity=2,
                    title=u'{0}自动化测试报告'.format(project_name),
                    description=u'用例执行情况')
                result = runner.run(suite)

            # 是否发邮件
            if send_email:
                email_config = DataHandle().obtain_email_config(project_name)

                #当存在错误时,发送给指定负责人
                send_email_list = []
                if not result.errors == []:
                    for error in result.errors:
                        if re.search(r"leader\_(.*?)$",
                                     error[0]._testMethodName):
                            error_leaders_name = re.search(
                                r"leader\_(.*?)$",
                                error[0]._testMethodName).group(1)
                            error_leaders_list = error_leaders_name.split("_")
                            send_email_list.extend(error_leaders_list)
                    send_email_list = list(set(send_email_list))
                    for each_leader in send_email_list:
                        email_config["receiver"].append(
                            email_list[each_leader])
                    Email().send_email(email_config)
Exemplo n.º 2
0
    def execute_case(self, table_result):

        case_data = table_result[-1]
        warnings.simplefilter("ignore", ResourceWarning)

        if case_data.get("Run") != "N":
            # print(case_data)
            print(
                "**************************Start测试用例:{0}*********************************"
                .format(case_data["用例描述"]))

            table_result = DataHandle().handle_case_data(table_result)

            handle_input = case_data["handle_Input"]
            handle_except_info = case_data["CaseExcept"]
            handle_url = case_data["Url"]
            handle_headers = case_data.get("headers")
            handle_files = case_data.get("files")
            time_out = case_data.get("timeout")

            print("请求方式:{0},   请求地址:{1}".format(case_data["method"],
                                                handle_url))
            print("测试输入:{0}、{1}、{2}".format(case_data["Input"], handle_headers,
                                            handle_files))
            print("期望输出:{0}".format(handle_except_info))
            Response = CallAPI().run(method=case_data["method"],
                                     url=handle_url,
                                     input=handle_input,
                                     headers=handle_headers,
                                     files=handle_files,
                                     time_out=time_out)
            del case_data["handle_Input"]

            print("测试实际返回值:\n{0}".format(Response["res"]))
            case_data["Res"] = Response["res"]
            case_data["Res_headers"] = Response["Res_headers"]
            case_data["Res_time"] = Response["res_time"]
            case_data["status_code"] = Response["status_code"]

            print("接口校验情况:")
            self.check_result(table_result, handle_except_info)

            print(
                "**************************PASS测试用例:{0}**********************************\n\n"
                .format(case_data.get("用例描述")))
        else:
            print("skip and No Run")
Exemplo n.º 3
0
    def create_suite(self, project_name, ordered=False, pattern='test*.py'):
        # 导入该项目下全部测试用例模块
        # import_statement = DataHandle().obtain_import_cases_statement(project_name)
        # exec(import_statement)
        # suite=unittest.defaultTestLoader.discover(project_case_path(project_name),pattern=pattern,top_level_dir=project_case_path(project_name))

        if not ordered:
            suite = unittest.TestSuite()
            discover = unittest.defaultTestLoader.discover(
                project_case_path(project_name),
                pattern=pattern,
                top_level_dir=project_case_path(project_name))
            suite.addTest(discover)

        else:
            test_case_list = DataHandle().obtain_case_exe_order(project_name)
            suite = unittest.TestSuite()
            for case in test_case_list:
                suite.addTest(eval(case))
        return suite
import unittest
from InterfaceAuto.common import ddt
from InterfaceAuto.common.data_handle import DataHandle
from InterfaceAuto.common.general_test import GeneralTest
from InterfaceAuto.common.json_handle import JmespathExtractor
import json
JExtractor = JmespathExtractor()
project = "Risk_assess"
sun_project = "Risk_assess_foreground"
module = "变更抚养关系"
module_cases = DataHandle().obtain_interface_cases(project,
                                                   table_name=module,
                                                   sun_project=sun_project)
table_result = []


@ddt.ddt
class TestCase(unittest.TestCase):
    '''【变更抚养关系】数据关联,只需查看第一个错误接口'''
    def setUp(self):
        self.run = GeneralTest()

    def tearDown(self):
        pass

    @ddt.data(*module_cases)
    def test_module_cases(self, case_data):
        table_result.append(case_data)
        self.run.execute_case(table_result)

Exemplo n.º 5
0
import unittest
from InterfaceAuto.common import ddt
from InterfaceAuto.common.data_handle import DataHandle
from InterfaceAuto.common.general_test import GeneralTest
project = "File_handle_service"
module = "excel"
module_cases = DataHandle().obtain_interface_cases(project, module)
table_result = []


@ddt.ddt
class TestCase(unittest.TestCase):
    def setUp(self):
        self.run = GeneralTest()

    def tearDown(self):
        pass

    @ddt.data(*module_cases)
    def test_module_cases(self, case_data):
        table_result.append(case_data)
        self.run.execute_case(table_result)


if __name__ == '__main__':
    unittest.main()
Exemplo n.º 6
0
    def check_result(self, table_result, check_infos):
        case_data = table_result[-1]

        quoto_situation = case_data["QuotoSituation"]
        if quoto_situation != None and quoto_situation != "":
            for k, v in quoto_situation.items():
                v = DataHandle().obtain_quote_data(v, table_result)
                v = DataHandle().handle_string_obj(v)
                if isinstance(v, str) and re.search(r'&extract\[(.*?)]$', v):
                    pattern = re.search(r'&extract\[(.*?)]$', v).group(1)
                    v = re.search(pattern, v).group(1)
                quoto_situation[k] = v
            case_data["QuotoSituation"] = quoto_situation

        for check_info in check_infos:
            try:
                quary_string = check_info[0]
                check_des = check_info[1]
                check_value = check_info[2]

                check_des = DataHandle().obtain_quote_data(
                    check_des, table_result)
                check_des = DataHandle().obtain_QuotoSituation_data(
                    case_data["project"], quoto_situation, check_des)

                check_des_list = check_des.split(",")
                location_list = None
                if len(check_des_list) == 1:
                    check_obj_type, check_method = "default", check_des_list[0]
                elif len(check_des_list) == 2:
                    check_obj_type, check_method = check_des_list[
                        0], check_des_list[1]
                elif len(check_des_list) == 3:
                    check_obj_type, check_method, location_des = check_des_list[
                        0], check_des_list[1], check_des_list[2]
                    location_list = location_des.split("@")
                else:
                    raise Exception("check_info:{0},验证格式错误:{1}".format(
                        check_info, check_des))

                if re.search(r'\%(.*?)\%', quary_string):
                    check_obj = DataHandle().obtain_QuotoSituation_data(
                        case_data["project"], quoto_situation, quary_string,
                        check_obj_type, location_list)
                elif re.search(r'\<(.*?)\>', quary_string):
                    check_obj = DataHandle().obtain_quote_data(
                        quary_string, table_result)
                else:
                    check_obj = DataHandle().obtain_type_data(
                        quary_string, check_obj_type, case_data["Res"],
                        location_list)

                check_value = DataHandle().obtain_QuotoSituation_data(
                    case_data["project"], quoto_situation, check_value)

                self.assert_result(check_obj, check_method, check_value)
                print("成功:{0}".format(check_info))
            except Exception as e:
                print("失败:{0},error info:{1}\n".format(check_info, e))
                raise Exception(
                    "case_data:{0}\n\n\n验证check_info失败:{1},error info:{2}\n".
                    format(case_data, check_info, e))
Exemplo n.º 7
0
import unittest
from InterfaceAuto.common import ddt
from InterfaceAuto.common.data_handle import DataHandle
from InterfaceAuto.common.general_test import GeneralTest
project = "Intelligent_mediation"

dispute_flow_table = "dispute_flow"
dispute_flow_cases=DataHandle().obtain_interface_cases(project, table_name=dispute_flow_table)
dispute_flow_result=[]


quick_dispute_flow_table = "quick_dispute_flow"
quick_dispute_flow_cases=DataHandle().obtain_interface_cases(project, table_name=quick_dispute_flow_table)
quick_dispute_flow_result=[]


written_dispute_flow_table = "written_dispute_flow"
written_dispute_flow_cases=DataHandle().obtain_interface_cases(project, table_name=written_dispute_flow_table)
written_dispute_flow_result=[]


record_flow_table = "record_flow"
record_flow_cases=DataHandle().obtain_interface_cases(project, table_name=record_flow_table)
record_flow_result=[]




@ddt.ddt
class TestCase(unittest.TestCase):
    '''数据关联,只需查看第一个错误接口'''