Exemplo n.º 1
0
    def run_dependent(self):
        run_method = RunMethod()
        row_num = self.opera_excel.get_row_num(self.case_id)
        request_data = self.data.get_data_for_json(row_num)
        header = self.data.is_header(row_num)
        method = self.data.get_request_method(row_num)
        url = self.data.get_url(row_num)

        res = run_method.run_main(method, url, request_data, header)
        return res
Exemplo n.º 2
0
class DependentData:
    def __init__(self, case_id):
        self.case_id = case_id
        self.oper_excel = OperationExcel()
        self.method = RunMethod()
        self.data = GetData()

    # 通过case_id去获取依赖case_id的整行数据
    def get_case_line_data(self):
        rows_data = self.oper_excel.get_rows_data(self.case_id)
        return rows_data

    #执行依赖测试,获取结果
    def run_dependent(self):
        row_num = self.oper_excel.get_row_num(self.case_id)
        request_data = self.data.get_data_value(row_num)
        header = self.data.get_request_header(row_num)
        method = self.data.get_request_method(row_num)
        url = self.data.get_request_url(row_num)
        if (header == 'write_Cookies'):
            header = {
                'Content-Type': 'application/json',
                "X-Lemonban-Media-Type": "lemonban.v2"
            }
            data = json.dumps(request_data)
            res = self.method.run_main(method, url, data, header, params=data)
        else:
            res = self.method.run_main(method,
                                       url,
                                       request_data,
                                       header,
                                       params=request_data)
        return res

    #获取依赖字段的响应数据:通过执行依赖测试case来获取响应数据,响应中某个字段数据作为依赖key的value
    def get_value_for_key(self, row):
        newdepend = ''
        #获取依赖的返回数据key
        depend_data = self.data.get_depend_key(row)
        #执行依赖case返回结果
        response_data = self.run_dependent()
        if ',' in depend_data:
            newdepend_data = depend_data.split(',')
            if len(newdepend_data) > 1:
                for index, item in enumerate(newdepend_data):
                    newdepend += str([
                        match.value
                        for match in parse(item).find(response_data)
                    ][0]) + ","
                return newdepend
        else:
            return [
                match.value for match in parse(depend_data).find(response_data)
            ][0]
Exemplo n.º 3
0
class DependentData:

    def __init__(self, case_id):
        self.case_id = case_id
        self.oper_excel = OperationExcel()
        self.method = RunMethod()
        self.data = GetData()

    # 通过case_id去获取依赖case_id的整行数据
    def get_case_line_data(self):
        rows_data = self.oper_excel.get_rows_data(self.case_id)
        return rows_data

    # 执行依赖测试,获取结果
    def run_dependent(self):
        row_num = self.oper_excel.get_row_num(self.case_id)
        request_data = self.data.get_data_value(row_num)
        header = self.data.get_request_header(row_num)
        method = self.data.get_request_method(row_num)
        url = self.data.get_request_url(row_num)
        res = self.method.run_main(method, url, request_data, header, params=request_data)
        return res

    # 获取依赖字段的响应数据:通过执行依赖测试case来获取响应数据,响应中某个字段数据作为依赖key的value
    def get_value_for_key(self, row):
        # 获取依赖的返回数据key
        depend_data = self.data.get_depend_key(row)
        # 执行依赖case返回结果
        response_data = self.run_dependent()
        # print(depend_data)
        # print(response_data)

        return [match.value for match in parse(depend_data).find(response_data)][0]
Exemplo n.º 4
0
 def run_dependent(self):
     run_method = RunMethod()
     row_num = self.opera_excel.get_row_num(self.case_id)
     request_data = self.data.get_data_for_json(row_num)
     depend_case = self.data.is_depend(row_num)
     method = self.data.get_request_method(row_num)
     if depend_case is not None:
         self.depend_data = DependdentData(depend_case)
         # 获取的依赖响应数据
         # for depend_response_data_list in
         depend_response_data = self.depend_data.get_data_for_key(row_num)
         if len(depend_response_data) == 1:
             request_data_result = "".join(depend_response_data)
         else:
             request_data_result = ",".join(
                 [str(i) for i in depend_response_data])
         # 获取依赖的key
         depend_key = self.data.get_depend_field(row_num)
         depend_key_list = depend_key.split(",")
         for (key_list, key) in zip(request_data_result.split(","),
                                    depend_key_list):
             # print(key_list, key)
             if method == "Post" or method == "Get":
                 # print(key)
                 request_data[key] = key_list
             else:
                 request_data = depend_response_data
                 break
     # print("依赖request_data===========%s" % request_data)
     header = self.data.is_header(row_num)
     op_json = OperetionJson('../dataconfig/weChatToken')
     contentType = "application/json"
     token = op_json.get_data('token')
     if header == 'write':
         header = {'Content-Type': contentType}
     elif header == 'yes':
         op_json = OperetionJson('../dataconfig/token.json')
         token = op_json.get_data('token')
         header = {'Content-Type': contentType, 'Authorization': token}
     elif header == 'weChat':
         header = {'Content-Type': contentType, "Authorization": token}
     # print(header)
     method = self.data.get_request_method(row_num)
     url = self.data.get_request_url(row_num)
     res = run_method.run_main(method, url, request_data, header)
     return json.loads(res)
Exemplo n.º 5
0
class RunTest:
    def __init__(self):
        self.run_method = RunMethod()
        self.data = GetData()
        self.com_util = CommonUtil()
        self.send_mail = SendEmail()

    #程序执行
    def go_on_run(self):
        res = None
        pass_count = []
        error_count = []
        rows_count = self.data.get_case_lines()
        #print rows_count
        for i in range(1, rows_count):
            is_run = self.data.get_is_run(i)
            # print is_run
            if is_run:
                url = self.data.get_url(i)
                #print url
                method = self.data.get_request_method(i)
                #print method
                request_data = self.data.get_data_for_json(i)
                #print data
                expect = self.data.get_expect_data(i)
                print expect
                header = self.data.is_header(i)
                #print header
                depend_case = self.data.is_depend(i)
                #print depend_case_id
                res = self.run_method.run_main(method, url, request_data,
                                               header)
                #print res
                if depend_case != None:
                    self.depend_data = DependdentData(depend_case)
                    #获取依赖的响应数据
                    depend_response_data = self.depend_data.get_data_for_key(i)
                    #获取依赖的key
                    depend_key = self.data.get_depend_field(i)
                    request_data[depend_key] = depend_response_data

                if self.com_util.is_contain(expect, res):
                    self.data.write_data(i, "PASS")
                    pass_count.append(i)
                    #print "PASS"
                else:
                    print self.data.write_data(i, res)
                    error_count.append(i)
        print len(pass_count)
        print len(error_count)
        self.send_mail.send_main(pass_count, error_count)
Exemplo n.º 6
0
 def __init__(self, case_id):
     self.case_id = case_id
     self.oper_excel = OperationExcel()
     self.method = RunMethod()
     self.data = GetData()
Exemplo n.º 7
0
 def __init__(self):
     self.run_method = RunMethod()
     self.data = GetData()
     self.com_util = CommonUtil()
     self.send_mail = SendEmail()
Exemplo n.º 8
0
class RunTest:
    def __init__(self):
        self.run_method = RunMethod()
        self.data = GetData()
        self.com_util = CommonUtil()
        self.send_mail = SendEmail()

    # 程序执行的
    def go_on_run(self):
        global expect
        expect = True
        res = None
        pass_count = []
        fail_count = []
        # 10  0,1,2,3
        rows_count = self.data.get_case_lines()
        for i in range(1, rows_count):
            is_run = self.data.get_is_run(i)
            if is_run:
                url = self.data.get_request_url(i)
                method = self.data.get_request_method(i)
                request_data = self.data.get_data_for_json(i)
                # expect = self.data.get_expcet_data_for_mysql(i)
                expect = self.data.get_expcet_data(i)
                header = self.data.is_header(i)
                depend_case = self.data.is_depend(i)
                if depend_case is not None:
                    self.depend_data = DependdentData(depend_case)
                    # 获取的依赖响应数据
                    # for depend_response_data_list in
                    depend_response_data = self.depend_data.get_data_for_key(i)
                    if len(depend_response_data) == 1:
                        request_data_result = "".join(depend_response_data)
                    else:
                        request_data_result = ",".join(
                            [str(i) for i in depend_response_data])
                    # 获取依赖的key
                    depend_key = self.data.get_depend_field(i)
                    depend_key_list = depend_key.split(",")
                    print(type(depend_key_list))
                    for (key_list, key) in zip(request_data_result.split(","),
                                               depend_key_list):
                        print(key_list, key)
                        if method == "Post" or method == "Get" or method == "Put_Add" or method == "Put":
                            # print(key)
                            request_data[key] = key_list
                        else:
                            request_data = depend_response_data
                            break

                contentType = "application/json"

                if header == 'write':
                    header = {'Content-Type': contentType}
                    res = self.run_method.run_main(method,
                                                   url,
                                                   data=request_data,
                                                   header=header)
                    op_header = OperationHeader(res)
                    op_header.write_token()

                elif header == 'yes':
                    op_json = OperetionJson('../dataconfig/token.json')
                    token = op_json.get_data('token')
                    header = {
                        'Content-Type': contentType,
                        'Authorization': token
                    }
                    # print("request_data=======%s" % request_data)
                    res = self.run_method.run_main(method,
                                                   url,
                                                   request_data,
                                                   header=header)
                elif header == 'write_user':
                    header = {
                        'Content-Type': contentType,
                    }
                    res = self.run_method.run_main(method,
                                                   url,
                                                   request_data,
                                                   header=header)
                    op_header = OperationHeader(res)
                    op_header.write_user_token()
                elif header == 'weChat':
                    op_json = OperetionJson('../dataconfig/weChatToken')
                    token = op_json.get_data('token')
                    header = {
                        'Content-Type': contentType,
                        'Authorization': token
                    }
                    res = self.run_method.run_main(method,
                                                   url,
                                                   request_data,
                                                   header=header)
                else:
                    res = self.run_method.run_main(method, url, request_data)

            if self.com_util.is_contain(expect, res) == 0:
                self.data.write_result(i, 'pass')
                pass_count.append(i)
            else:
                self.data.write_result(i, res)
                fail_count.append(i)
Exemplo n.º 9
0
class RunTest:

    def __init__(self):
        self.run_method = RunMethod()
        self.data = GetData()
        self.com_util = CommonUtil()
        self.send_mail = SendEmail()

    # 程序执行
    def go_on_run(self):
        res = None
        pass_count = []
        fail_count = []
        no_run_count = []
        rows_count = self.data.get_case_lines()

        # 每次执行用例之前将log日志文件清空数据
        log_file = '../log/log.txt'
        with open(log_file, 'w') as f:
            f.seek(0, 0)  # 加上f.seek(0),把文件定位到position 0;没有这句的话,文件是定位到数据最后,truncate也是从最后这里删除
            f.truncate()  # 清空数据

        # 循环执行每行用例
        for i in range(1, rows_count):
            try:
                is_run = self.data.get_is_run(i)
                if is_run:
                    url = self.data.get_request_url(i)
                    method = self.data.get_request_method(i)
                    # 获取请求参数
                    data = self.data.get_data_value(i)
                    # 获取excel文件中header关键字
                    header_key = self.data.get_request_header(i)
                    # 获取json文件中header_key对应的头文件数据
                    header = self.data.get_header_value(i)
                    expect = self.data.get_expect_data(i)
                    depend_case = self.data.is_depend(i)

                    if depend_case != None:
                        self.depend_data = DependentData(depend_case)
                        # 获取依赖字段的响应数据
                        depend_response_data = self.depend_data.get_value_for_key(i)
                        # 获取请求依赖的key
                        depend_key = self.data.get_depend_field(i)
                        # 将依赖case的响应返回中某个字段的value赋值给该接口请求中某个参数
                        data[depend_key] = depend_response_data

                    # cookie相关的没有跑通,代码逻辑是正常的,但是模拟登陆返回一直是非法请求
                    if header_key == 'write_Cookies':
                        res = self.run_method.run_main(method, url, data, header, params=data)
                        op_header = OperationHeader(res)
                        op_header.write_cookie()

                    elif header_key == 'get_Cookies':
                        op_json = OperationJson('../dataconfig/cookie.json')
                        cookie = op_json.get_data('apsid')
                        cookies = {'apsid': cookie}
                        res = self.run_method.run_main(method, url, data, header=cookies, params=data)

                    else:
                        res = self.run_method.run_main(method, url, data, header, params=data)

                    '''
                    get请求参数是params:request.get(url='',params={}),post请求数据是data:request.post(url='',data={})
                    excel文件中没有区分直接用请求数据表示,则data = self.data.get_data_value(i)拿到的数据,post请求就是data=data,get请就是params=data
                    '''

                    # excel中拿到的expect数据是str类型,但是返回的res是dict类型,两者数据比较必须都是字符类型
                    if self.com_util.is_contain(expect, json.dumps(res)):
                        self.data.write_result(i, 'pass')
                        pass_count.append(i)
                    else:
                        # 返回的res是dict类型,要将res数据写入excel中,需将dict类型转换成str类型
                        self.data.write_result(i, json.dumps(res))
                        with open(log_file, 'a', encoding='utf-8') as f:
                            f.write("\n第%s条用例实际结果与预期结果不一致:\n" % i)
                            f.write("Expected:%s\n  Actual:%s\n" % (expect, res))
                        fail_count.append(i)

                else:
                    no_run_count.append(i)

            except Exception as e:
                # 将异常写入excel的测试结果中
                self.data.write_result(i, str(e))
                # 将报错写入指定路径的日志文件里
                with open(log_file, 'a', encoding='utf-8') as f:
                    f.write("\n第%s条用例报错:\n" % i)
                initLogging(log_file, e)
                fail_count.append(i)

        self.send_mail.send_main(pass_count, fail_count, no_run_count)