Ejemplo n.º 1
0
class DependData:
    def __init__(self):
        self.get_dep_data = GetDependData()
        self.data = GetData()
        self.config = ReadConfig()
        self.run_method = RunMethod()

    # 执行依赖测试,获取结果
    def run_depedn_test(self, case_id):
        run_host = self.config.get_http()
        row = self.get_dep_data.in_case_id_get_depend_Case_id_lines(case_id)
        request_method = self.data.get_request_method(row)
        url = str(run_host) + str(self.data.get_request_url(row))
        data = self.data.get_request_data(row)
        header = self.data.get_request_header(row, self.config.get_token())
        # print(run_host)
        # print(row)
        # print(request_method)
        # print(url)
        # print(data)
        # print(header)
        ress = self.run_method.run_main(method=request_method,
                                        url=url,
                                        data=data,
                                        header=header)
        return ress

    # 根据依赖的key获取执行依赖case的响应数据,然后返回
    def get_data_in_key(self, row, case_id):
        depent_key = self.data.get_depend_return_data_key(row)
        print(depent_key)
        res_depent_data = self.run_depedn_test(case_id)
        # print(res_depent_data)
        dict_json = json.loads(res_depent_data)
        json_exe = parse(depent_key)
        madle = json_exe.find(dict_json)
        depent_data = [match.value for match in madle][0]
        #print(depent_data)
        return depent_data
Ejemplo n.º 2
0
class RunTest:
    def __init__(self):
        self.method = RunMethod()
        self.data = GetData()
        self.com_result = CompareResult()
        self.config = ReadConfig()
        self.depend = DependData()
        self.sendemail = SendEmail()

    def start_on_run(self):
        res = None
        result = None
        pass_num = []
        fail_num = []
        no_run_num = []
        rows_count = self.data.get_case_lines()
        run_host = self.config.get_http()
        if run_host:
            self.data.write_run_env(1, run_host)
        print("总共 %s 行" % (rows_count))
        print("总共 %s 个测试用例" % (rows_count - 2))
        print("测试结果:")
        for i in range(3, rows_count + 1):
            #time.sleep(5)
            is_run = self.data.get_is_run(i)
            is_depend = self.data.get_is_depend(i)
            request_method = self.data.get_request_method(i)
            case_id = self.data.get_case_id(i)
            data = self.data.get_request_data(i)
            # print(case_id)
            # print("是否有依赖:%s"%is_depend)
            if is_run and is_depend:
                url = run_host + self.data.get_request_url(i) + self.depend.get_data_in_key(i, case_id)
                # print("编号%s的url:%s" % (case_id, url))
            else:
                url = run_host + self.data.get_request_url(i)
                # print("编号%s的url:%s"%(case_id, url))
            if case_id == "DEV-001" or case_id == "CMB-001":
                header = self.data.get_request_header(i)
            else:
                header = self.data.get_request_header(i, self.config.get_token())
            expect_result = self.data.get_expect_result(i)
            if is_run:
                run_case_id = self.data.get_case_id(i)
                run_case_title = self.data.get_case_title(i)
                res = self.method.run_main(method=request_method, url=url, data=data, header=header)
                # print("====>>>")
                # print(res)
                # if i == 3:
                if case_id == "DEV-001":
                    dict_json = json.loads(res)  # 把json数据转换成字典对象
                    orc_token = dict_json['orchestrator_admin_token']
                    # print(dict_json)
                    self.config.write_token(orc_token)
                    # print(orc_token)
                if case_id == "CMB-001":
                    dict_json = json.loads(res)  # 把json数据转换成字典对象
                    ten_token = dict_json['tenant_admin_token']
                    # print(dict_json)
                    self.config.write_token(ten_token)
                    # print(orc_token)
                if self.com_result.is_contain(res, expect_result):
                    print("编号%s(%s): 测试通过" % (run_case_id, run_case_title))
                    self.data.write_data_actual(i, 'Pass')
                    self.data.write_data_run(i, res)
                    pass_num.append(i)
                else:
                    print("编号%s(%s): 测试不通过" % (run_case_id, run_case_title))
                    self.data.write_data_actual(i, 'Fail')
                    self.data.write_data_run(i, res)
                    fail_num.append(i)
            else:
                no_run_case_id = self.data.get_case_id(i)
                no_run_case_title = self.data.get_case_title(i)
                print("编号%s(%s): 不需要执行" % (no_run_case_id, no_run_case_title))
                self.data.write_data_actual(i, 'NA')
                no_run_num.append(i)
        print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
        print("%s 个测试用例被执行" % (len(pass_num+fail_num)))
        print("%s 个测试用例没有执行" % (len(no_run_num)))
        # 以邮件方式发送测试报告
        self.sendemail.start_send(pass_num, fail_num)