Example #1
0
    def go_on_run(self):
        res = None
        pass_conut = []
        fail_conut = []
        rows_conut = self.data.get_case_lines()
        for i in range(1, rows_conut):
            is_run = self.data.get_is_run(i)
            if is_run:
                self.request_type = self.data.get_request_type(i)

                url = self.data.get_request_url(i)
                method = self.data.get_request_method(i)
                request_data = self.data.get_json_data(i)
                case_name = self.data.get_case_name(i)
                cookie = self.data.get_cookies_value(i)
                expect = self.data.get_expect_data(i)
                depent_case = self.data.case_id(i)  # 获取依赖字段的key
                if depent_case is not None:
                    # 获取依赖的key
                    depent_key = self.data.get_depnet_file(i)
                    self.depend_data = DepnedentData(depent_case)
                    # 获取依赖的响应数据
                    depnet_response_data = self.depend_data.get_data_for_key(i)
                    le = len(depent_key.split(">"))  # 切分字符串取大小
                    for y in range(0, le):
                        re = depent_key.split(">")
                        request_data[re[y]] = depnet_response_data[y]  # 循环替换
                if cookie == 'write':
                    # 发送请求
                    res = self.run_method.run_main(method, url, request_data)
                    op_cookies = OperationHeader(res)
                    op_cookies.write_cookie()  # 写入cookies到cookie.json文件中
                elif cookie == 'yes':
                    get_cookie = OperationJson('../dataconfig/cookie.json')
                    cooki = get_cookie.get_data('SESSION')
                    cookies = {
                        "SESSION": cooki
                    }
                    res = self.run_method.run_main(method, url, request_data, cookies)
                else:
                    res = self.run_method.run_main(method, url, request_data)
                if self.containt.is_contain(expect, res):
                    self.data.write_result(i, 'pass')
                    pass_conut.append(i)
                    self.logger.info("测试通过")
                    self.logger.info("通过响应数据" + res)
                    print("测试通过")
                else:
                    self.logger.info("测试失败")
                    self.logger.info("失败case名称:" + case_name)
                    self.logger.info("失败的行数:" + str(i))
                    self.data.write_result(i, res)
                    fail_conut.append(i)
                    print("测试失败")
                    self.logger.info("url:" + url)
                    self.logger.info("失败请求数据:" + str(request_data))
                    self.logger.info("失败响应数据:" + res)

        # 发送测试报告邮件
        self.email.send_main(pass_conut, fail_conut)
Example #2
0
    def go_on_run(self):
        res = None
        pass_count = []
        fail_count = []
        rowcount = self.data.get_case_rowCount()
        for i in range(1, rowcount):

            is_run = self.data.get_is_run(i)

            if is_run:
                modname = self.data.get_module(i)
                url = self.data.get_url(i)
                method = self.data.get_request_method(i)
                data = self.data.get_data_for_json(i)
                is_cookie = self.data.get_cookies(i)
                header = self.data.is_header(i)
                hoperesult = self.data.get_hopereval(i)
                is_depend = self.data.is_depend(i)
                # 是否需要依赖
                if is_depend != None:
                    self.depend_data = DependData(is_depend)
                    # 获取依赖的响应数据
                    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

                # 是否保存cookies
                if is_cookie == 'write':
                    res = self.run_method.run_main(method, url, data)
                    op_headers = OperationHeader(res)
                    if modname == "登录":
                        op_headers.write_login_cookie()
                    else:
                        op_headers.write_cookie()

                elif is_cookie == 'yes':
                    op_json = OperationJson('../case/cookie.json')
                    header = {"Cookie": op_json.data}
                    res = self.run_method.run_main(method, url, data, header)
                else:
                    res = self.run_method.run_main(method, url, data)

                res = res.content.decode('utf-8')
                # print(res)

                if self.commonUtil.is_contain(hoperesult, res):
                    self.data.write_realresult(i, res)
                    self.data.write_result(i, "测试成功")
                    pass_count.append(i)

                else:
                    self.data.write_realresult(i, res)
                    self.data.write_result(i, "测试失败")
                    fail_count.append(i)

                res = json.loads(res)
                if (res['statusCode'] != "200"):
                    # print(res['errorMsg'])
                    pass
Example #3
0
class RunTest:
    def __init__(self, file_name=None, sheet_id=0):
        self.data = GetData(file_name, sheet_id)
        self.oper_header = OperationHeader()
        self.run = RunMethod()
        self.comm = Common()
        self.send_mail = SendEmail()

    def go_on_run(self):
        lines = self.data.get_case_lines()
        pass_count = []
        fail_count = []
        for i in range(1, lines):
            is_run = self.data.get_is_run(i)
            if is_run:
                res = None
                url = self.data.get_url(i)
                method = self.data.get_method_type(i)
                fix_request_key = self.data.get_fix_request_key(i)
                request_data = self.data.get_request_for_json(i)
                expected = self.data.get_expected_result(i)
                header = self.data.get_header(i)
                is_depend = self.data.is_depend(i)
                if is_depend is not None:
                    depend_case_id = self.data.get_depend_case_id(i)
                    depend_request = self.data.get_depend_request(i)
                    depend_data = DependData(depend_case_id)
                    depend_response = depend_data.get_data_for_key(i)  # 从依赖接口的返回结果中匹配需要的依赖字段
                    request_data[depend_request] = depend_response
                if header == 'yes':
                    self.oper_header.write_cookie()
                    cookies = self.oper_header.get_cookie()
                    header = {'Cookie': cookies}
                    # json.dump()
                    res = self.run.run_main(url, method, request_data, header)
                else:
                    res = self.run.run_main(url, method, request_data)
                # if self.comm.contrast(expected, res):
                #     print("测试通过")
                #     self.data.write_result(i, 'pass')
                #     pass_count.append(i)
                # else:
                #     print("测试失败")
                #     self.data.write_result(i, res)
                #     fail_count.append(i)
        # self.send_mail.send_main(pass_count,fail_count)

    def run_request_data(self, index, value):
        request_data = self.data.get_request_for_json(index)
        key = self.data.get_fix_request_key(index)
        request_data[key] = value
        return request_data

    def run_expected_data(self, index, value):
        key = self.data.get_expected_result(index)
        expected_data = {}
        expected_data[key] = value
        expected_data = key + ":" + expected_data[key]
        return expected_data
Example #4
0
    def go_on_run(self):
        # 10  0,1,2,3,
        res = None
        pass_count = []
        fail_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)
            if is_run:
                url = self.data.get_request_url(i)
                print(i, url)
                method = self.data.get_request_method(i)
                request_data = self.data.get_data_for_json(i)
                expect = self.data.get_expect_data(i)
                header = self.data.is_header(i)
                depend_case = self.data.is_depend(i)
                # res = self.run_method.run_main(method, url, data, header)
                # if is_run:
                # method,url,data=None,header=None
                if depend_case != None:
                    # self.depend_data = DependentData(depend_case)
                    print("depend_case is" + " --- " + depend_case)
                    self.depend_data = DependentData(depend_case)
                    # 获取的依赖响应数据
                    depend_response_data = self.depend_data.get_data_for_key(i)
                    #     获取依赖的key
                    print(depend_response_data)
                    depend_key = self.data.get_depend_field(i)
                    print(depend_key)
                    request_data[depend_key] = depend_response_data
                if header == "write":
                    res = self.run_method.run_main(method, url, request_data)
                    op_header = OperationHeader(res)
                    op_header.write_cookie()
                elif header == 'yes':
                    op_json = OperationJson('../dataconfig/cookie.json')
                    cookie = op_json.get_data('apsid')
                    cookies = {'apsid': cookie}
                    # cookie = op_header.get_data()
                    # cookie = ../dataconfig/cookie.json ../dataconfig/cookie.json
                    res = self.run_method.run_main(method, url, request_data,
                                                   cookies)
                else:
                    res = self.run_method.run_main(method, url, request_data)

                # res = self.run_method.run_main(method,url,request_data,header)
                # print(res)
                if self.com_util.is_contain(expect, res):
                    # print("测试通过")
                    self.data.write_result(i, 'pass')
                    pass_count.append(i)
                else:
                    # print("测试失败")
                    # self.data.write_result(i, 'fail')
                    self.data.write_result(i, res)
                    fail_count.append(i)
        self.send_email.send_main(pass_count, fail_count)
Example #5
0
    def go_on_run(self):
        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 != 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 header == 'write':
                    res = self.run_method.run_main(method, url, request_data)
                    op_header = OperationHeader(res)
                    op_header.write_cookie()

                elif header == 'yes':
                    op_json = OperetionJson('../dataconfig/cookie.json')
                    cookie = op_json.get_data('apsid')
                    cookies = {'apsid': cookie}
                    res = self.run_method.run_main(method, url, request_data,
                                                   cookies)
                else:
                    res = self.run_method.run_main(method, url, request_data)
                print(res)
                print(type(res))

                #if self.com_util.is_equal_dict(expect,res) == 0:
                #if self.com_util.is_contains_dict(expect,res):
                if self.com_util.is_contain(expect, res):
                    self.data.write_result(i, 'pass')
                    pass_count.append(i)
                    res = pass_count

                else:
                    #self.data.write_result(i,res)
                    self.data.write_result(i, 'fail')
                    fail_count.append(i)
                    res = fail_count
Example #6
0
    def go_on_run(self):  # 程序执行
        pass_count = []
        fail_count = []
        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)
                data = self.data.get_data_for_json(i)
                # expect = self.data.get_expect_data(i)  # Excel写入预期结果
                expect = self.data.get_expect_data_for_mysql(i)
                header = self.data.is_header(i)
                depend_case = self.data.is_depend(i)
                res = self.run_method.run_main(method, url, data, header)
                values = {"resultCode": res["resultCode"]}
                # print(res["resultData"]["access_id"])
                value = json.dumps(values)  # 字典转字符串匹配
                if depend_case is not None:
                    self.depend_data = DependentDate(depend_case)
                    # 依赖响应数据
                    depend_response_data = self.depend_data.get_data_for_key(i)
                    # 获取依赖的key
                    data = json.loads(data)
                    depend_key = self.data.get_depend_field(i)
                    data[depend_key] = depend_response_data
                if header == 'write':
                    res = self.run_method.run_main(method, url, data)
                    op_header = OperationHeader(res)
                    op_header.write_cookie()

                elif header == 'yes':
                    op_json = OperationJson('../dataconfig/cooking.json')
                    cookie = op_json.get_data('apsid')
                    cookies = {
                        'apsid': cookie
                    }
                    value = self.run_method.run_main(method, url, data, cookies)
                else:
                    value = self.run_method.run_main(method, url, data)
                if self.commonutil.is_equal_dict(expect, value):
                    self.data.write_result(i, '通过')
                    pass_count.append(i)
                    print('测试通过')
                else:
                    self.data.write_result(i, '失败')
                    fail_count.append(i)
                    print('测试失败')

                print(pass_count, fail_count)
        self.sendmail.send_main(pass_count, fail_count)
    def go_on_run(self):
        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(i)
                header = self.data.is_header(i)
                depend_case = self.data.is_depend(i)
                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)
                    #print(request_data)
                    #request_data[depend_key] = depend_response_data
                    request_data = self.operation.deal_data(request_data, depend_key, depend_response_data)


                if header == 'write':
                    res = self.run_method.run_main(method, url, request_data)
                    op_header = OperationHeader(res)
                    op_header.write_cookie()
                elif header == 'yes':
                    op_json = OperetionJson('config/cookie.json')
                    cookie = op_json.get_data('apsid')

                    cookies = {
                        'apsid': cookie
                    }
                    res = self.run_method.run_main(method, url, request_data, cookies)
                else:
                    res = self.run_method.run_main(method, url, request_data, {'Content-Type': 'application/x-www-form-urlencoded'})


                if self.com_util.is_equal_dict(expect, res):
                    self.data.write_result(i, 'pass')
                    pass_count.append(i)
                else:
                    self.data.write_result(i, res)
                    fail_count.append(i)

        self.send_mai.send_email(pass_count, fail_count)
Example #8
0
    def go_on_run(self):
        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(i)
                header = self.data.is_header(i)
                depend_case = self.data.is_depend(i)
                case_name = self.data.get_case_name(i)
                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 header == 'write':
                    res = self.run_method.run_main(method, url, request_data)
                    op_header = OperationHeader(res)
                    op_header.write_cookie()

                elif header == 'yes':
                    op_json = OperetionJson('./dataconfig/cookie.json')
                    cookie = op_json.get_data('apsid')
                    # cookies = {
                    # 	'apsid':cookie
                    # }
                    res = self.run_method.run_main(method, url, request_data,
                                                   cookie)
                    self.logger.info("用例名称:" + case_name + "--->返回值:" + res)
                    #print(res)
                else:
                    res = self.run_method.run_main(method, url, request_data)
                    self.logger.info("用例名称:" + case_name + "--->返回值:" + res)
                    #print(res)
                if self.com_util.is_contain(expect, res) == True:
                    self.data.write_result(i, 'pass')
                    pass_count.append(i)
                else:
                    self.data.write_result(i, res)
                    fail_count.append(i)
        #self.send_mai.send_main(pass_count,fail_count)  #不带文件发送
        self.send_email.sendemail(pass_count, fail_count)  #带文件发送
Example #9
0
    def go_on_run(self):
        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)
                # sql_data = expect.strip("[]")
                sql_data1 = eval(expect)
                # sql_result = eval(sql_data1)
                header = self.data.is_header(i)
                depend_case = self.data.is_depend(i)
                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
                    # print(request_data[depend_key])
                    # res = self.run_method.run_main(method,url,request_data)
                if header == 'write':
                    res = self.run_method.run_main(method, url, request_data)
                    op_header = OperationHeader(res)
                    op_header.write_cookie()

                elif header == 'yes':
                    res = self.run_method.run_main(method, url, request_data)
                    # print(5,res)
                else:
                    res = self.run_method.run_main(method, url, request_data)
                    print(4, res)

                if self.com_util.is_equal_dict(res, sql_data1):
                    self.data.write_result(i, 'pass')
                    pass_count.append(i)
                else:
                    self.data.write_result(i, str(res))
                    fail_count.append(i)
        #将运行结果通过钉钉发送
        dd_mes(pass_count, fail_count)
        exit()
Example #10
0
    def go_on_run(self):
        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 != 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 header == 'write':
                    res = self.run_method.run_main(method, url, request_data)
                    op_header = OperationHeader(res)
                    op_header.write_cookie()

                elif header == 'yes':
                    op_json = OperetionJson('../dataconfig/cookie.json')
                    cookie = op_json.get_data('apsid')
                    cookies = {'apsid': cookie}
                    res = self.run_method.run_main(method, url, request_data,
                                                   cookies)
                else:
                    res = self.run_method.run_main(method, url, request_data)
                    # print(res["data"][2]["context"])
                    print(res)
                    # print(type(res))

                #自己写的:这个是判断字段是否在结果中
                if expect in res:
                    self.data.write_result(i, "pass")
                    pass_count.append(i)
                else:
                    self.data.write_result(i, "fail")
                    fail_count.append(i)
Example #11
0
 def run(self):
     res = None
     pass_count = []
     fail_count = []
     rows_count = self.data.get_case_lines()
     for i in range(1, rows_count):
         run = self.data.get_run(i)
         if run:
             url = self.data.get_url(i)
             request_method = self.data.get_request_method(i)
             request_data = self.data.get_data_for_json(i)
             expect = self.data.get_expect(i)
             print(expect)
             header = self.data.get_header(i)
             # case_depend = self.data.get_case_depend(i)
             # if case_depend != None:
             #     self.depend_data = DependentData(case_depend)
             #     depend_response_data = self.depend_data.get_data_for_key(i)
             #     depend_key = self.data.get_depend_response_data(i)
             #     request_data[depend_key] = depend_response_data
             if header == 'write':
                 res = self.run_method.run_main(request_method, url,
                                                request_data)
                 op_header = OperationHeader(res)
                 op_header.write_cookie()
             elif header == 'yes':
                 op_json = OperationJson('../dataconfig/cookie.json')
                 cookie = op_json.get_data('apsid')
                 cookies = {'apsid': cookie}
                 res = self.run_method.run_main(request_method, url,
                                                request_data, cookies)
             else:
                 res = self.run_method.run_main(request_method, url,
                                                request_data)
                 print(res)
             if self.common_util.is_contain(expect, res) == 1:
                 self.data.write_result(i, 'pass')
                 pass_count.append(i)
                 print(pass_count)
             else:
                 self.data.write_result(i, res)
                 fail_count.append(i)
                 print(fail_count)
     self.send_email.send_main(pass_count, fail_count)
Example #12
0
    def go_on_run(self):
        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)
                header = self.data.is_header(i)
                depend_case = self.data.is_depend(i)
                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 header == 'write':
                    res = self.run_method.run_main(method, url, request_data)
                    op_header = OperationHeader(res)
                    op_header.write_cookie()

                elif header == 'yes':
                    op_json = OperetionJson('../dataconfig/cookie.json')
                    cookie = op_json.get_data('apsid')
                    cookies = {
                        'apsid': cookie
                    }
                    res = self.run_method.run_main(method, url, request_data, cookies)
                else:
                    res = self.run_method.run_main(method, url, request_data)

                if self.com_util.is_equal_dict(expect, res) == 0:
                    self.data.write_result(i, 'pass')
                    pass_count.append(i)
                else:
                    self.data.write_result(i, res)
                    fail_count.append(i)
        self.send_mai.send_main(pass_count, fail_count)
Example #13
0
    def test_main(self, sheet_name, case_name, sheet_id, case_id):
        self.data = GetData(sheet_id)
        is_run = self.data.get_is_run(case_id)
        if is_run:
            url = self.data.get_request_url(case_id)
            method = self.data.get_request_method(case_id)

            #request_data = self.data.get_data_for_json(i)
            request_data = self.data.get_request_data(case_id)
            # expect = self.data.get_expcet_data_for_mysql(i)
            expect = self.data.get_expcet_data(case_id)
            header = self.data.is_header(case_id)
            depend_case = self.data.is_depend(case_id)
            if depend_case != None:
                self.depend_data = DependdentData(sheet_id, depend_case)
                # 获取的依赖响应数据
                depend_response_data = self.depend_data.get_data_for_key(case_id)
                # 获取依赖的key
                depend_key = self.data.get_depend_field(case_id)
                request_data[depend_key] = depend_response_data
            if header == 'write':
                res = self.run_method.run_main(method, url, request_data)
                op_header = OperationHeader(res)
                op_header.write_cookie()

            elif header == 'yes':
                op_json = OperetionJson('../dataconfig/cookie.json')
                cookie = op_json.get_data('apsid')
                cookies = {
                    'apsid': cookie
                }
                res = self.run_method.run_main(method, url, request_data, cookies)
            else:
                res = self.run_method.run_main(method, url, request_data)
            print(res)
            if self.com_util.is_contain(expect, res):
                self.data.write_result(case_id,'pass')

            else:
                self.data.write_result(case_id, res)
            #self.assertIn(expect,res,"预期结果:{0} 未在实际返回结果:{1} 中! ".format(expect,res))
            #添加最后结果时间戳
            self.data.write_result(0,  '实际结果 {}'.format(time.strftime("%Y_%m_%d_%H_%M_%S")))
            self.assertIn(expect,res)
Example #14
0
    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.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_key = self.data.get_request_header(i)

                    #获取json文件中header对应的文件数据

                    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

                    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是dic类型

                    if self.com_util.is_contain(expect, json.dumps(res)):
                        self.data.write_result(i, 'pass')
                        pass_count.append(i)

                    else:
                        #返回res是dic类型,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('expect:%s\n Actual:%s\n' % (expect, res))
                        fail_count.append(i)

                else:
                    no_run_count.append(i)

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

                initlogging(log_file, e)
                fail_count.append(i)
        self.send_email.send_main(pass_count, fail_count, no_run_count)
Example #15
0
    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)
Example #16
0
    def go_on_run(self):
        res = None
        # 定义三个列表,存储成功,失败,未执行的用例(case_id)
        pass_count = []
        fail_count = []
        no_run_count = []

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

        # 先获取excel表格的行数
        row_count = self.data.get_case_lines()
        print("start_用例开始执行,请稍候.....")

        for i in range(1, row_count): # 每循环一次,拿一次接口的所有数据
            try:
                # 先拿到is_run判断是否执行,如果是yes(会返回Ture),就去拿其它数据
                is_run = self.data.get_is_run(i)
                if is_run == True:
                    method = self.data.get_request_method(i)
                    url = self.data.get_request_url(i)
                    data = self.data.get_data_for_json(i)
                    header_key = self.data.is_header(i) # 获取excel文件中header关键字
                    header = self.data.get_header_value(i) # 获取json文件中header_key对应的头文件数据
                    expect = self.data.get_expcet_data(i) # 这里拿到的预期结果是字符串
                    # expect = self.data.get_expcet_data_for_mysql(i) # 断言方法如果是is_equal_dict就要取数据库这个值
                    depend_case = self.data.is_depend(i) # 获取依赖case_id

                    # 判断是否有依赖id,然后把返回数据替换到请求数据
                    if depend_case != None:
                        self.depend_data = DependdetData(depend_case)
                        # 1.获取依赖的响应字段数据
                        depend_response_data = self.depend_data.get_data_for_key(i)
                        # 2.获取需要替换的字段
                        depend_key = self.data.get_depend_field(i)
                        # 3.把1步拿到的values去替换第2步拿到的字段
                        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()
                        res = res.json()  # 先获取cookies再转成json字典类型,不然后面无法和预期结果对比
                    elif header_key == "get_Cookies":
                        op_json = OperationJson("D:\GZRJ\Python_XM\\api_automation_test\dataconfig\cookie.json")
                        cookie = op_json.get_data("PHPSESSID")
                        cookies = {"PHPSESSID": cookie}
                        #print(cookies)
                        res = self.run_method.run_main(method, url, data, header, params=data, cookies=cookies).json()
                    else:
                        res = self.run_method.run_main(method, url, data, header, params=data).json()
                    #print(res)
                    #print(res["data"]["accessToken"])

                    # 判断预期结果是否在返回结果里面,需要把返回结果res转成json字符串中文和预期结果比较
                    if self.com_util.is_contain(expect, json.dumps(res, ensure_ascii=False)):
                        self.data.write_result(i, "pass")
                        pass_count.append(i)
                    else:
                        self.data.write_result(i, json.dumps(res, ensure_ascii=False))
                        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)

                # 如果is_run是no,会返回False,然后把当前的case_id(行号)写入no_run_count
                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)

                """                  
                    if self.com_util.is_equal_dict(expect, res):
                        self.data.write_result(i, "PASS")
                        pass_count.append(i)
                    else:
                        self.data.write_result(i, res)
                        fail_count.append(i)
                                              """
        #我这里打印了一下用例的执行情况,嘿嘿
        print("end_用例执行完毕!")
        print("成功的用例:%s" % pass_count)
        print("失败的用例:%s" % fail_count)
Example #17
0
    def go_on_run(self):
        """程序执行"""
        pass_count = []
        fail_count = []
        no_run_count = []
        res = None
        # 获取用例数
        rows_count = self.data.get_case_lines()
        print(rows_count)
        # 第一行索引为0
        for i in range(1, rows_count):
            print("i:", i)
            is_run = self.data.get_is_run(i)
            print(is_run)
            if is_run:
                url = self.data.get_request_url(i)
                print("url:", url)
                method = self.data.get_request_method(i)
                print("method:", method)
                request_data = self.data.get_data_for_json(i)
                print("request_data:", request_data)
                print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$", type(request_data))
                expect = self.data.get_expect_data(i)
                print("expect:", expect)
                header = self.data.is_header(i)
                print("header:", header)
                token = self.data.is_token(i)
                print("token:", token)
                depend_case = self.data.is_depend(i)
                print("depend_case:", depend_case)

                if depend_case != None:
                    self.depend_data = DependentData(depend_case)
                    # 获取依赖的响应数据
                    print("#### i", i)
                    depend_response_data = self.depend_data.get_data_for_key(i)
                    print("##################")
                    print("1depend_response_data:", depend_response_data)
                    # 获取依赖的key
                    depend_key = self.data.get_depend_field(i)
                    print("depend_key:", depend_key)
                    # 更新请求字段
                    request_data.get(
                        'params')[depend_key] = depend_response_data
                    #request_data = json.dumps(request_data, ensure_ascii=False)
                    print("request_data:", request_data)
                    print(type(request_data))
                # 如果header字段值为write则将该接口的返回的token写入到token.json文件,如果为yes则读取token.json文件
                if header == "write_token":
                    print("********************")
                    res = self.run_method.run_main(method, url,
                                                   request_data).json()
                    print("res:", res)
                    print("res type", type(res))
                    #print("res.json():", res.json())
                    op_header = OperationHeader(res)
                    op_header.write_token()
                elif header == 'write_cookie' and token == 'yes':
                    print("case22222222222222")
                    op_json = OperationJson("../dataconfig/token.json")
                    token = op_json.get_data('data')
                    request_data = dict(request_data,
                                        **token)  # 把请求数据与登录token合并,并作为请求数据
                    print("login_data", request_data)
                    res = self.run_method.run_main(method, url, request_data)
                    print("res_login:"******"@@@@@@@@@@@@@@@@@@@@@@@@@@")
                    op_json1 = OperationJson('../dataconfig/cookie.json')
                    op_json2 = OperationJson('../dataconfig/token.json')
                    cookies = op_json1.get_data('cookie')
                    token = op_json2.get_data('data')
                    request_data = dict(request_data, **token)
                    print("post_data:", request_data)
                    print("cookie:", cookies)
                    new_value = json.dumps(request_data.get("params"))
                    request_data["params"] = new_value
                    res = self.run_method.run_main(method,
                                                   url,
                                                   request_data,
                                                   cookies=cookies)
                    print("res:", type(res))
                else:
                    res = self.run_method.run_main(method, url, request_data)

                if expect != None:
                    if self.com_util.is_contain(expect, res):
                        self.data.write_result(i, "Pass")
                        pass_count.append(i)
                        if type(res) is dict:
                            self.data.write_real_res(i, json.dumps(res))
                        else:
                            self.data.write_real_res(i, json.dumps(res.json()))
                    else:
                        self.data.write_result(i, "Fail")
                        fail_count.append(i)
                        if type(res) is dict:
                            self.data.write_real_res(i, json.dumps(res))
                        else:
                            self.data.write_real_res(i, json.dumps(res.json()))
                else:
                    print(f"用例ID:case-{i},预期结果不能为空")
            else:
                self.data.write_result(i, "Not run")
                no_run_count.append(i)

        # 发送邮件

        print(f"通过用例数:{len(pass_count)}")
        print(f"失败用例数:{len(fail_count)}")
        print(f"未执行败用例数:{len(no_run_count)}")

        self.send_email.send_main(pass_count, fail_count, no_run_count)
Example #18
0
    def go_on_run(self):
        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)
                # print("except data is %s" %expect)
                header = self.data.is_header(i)
                depend_case = self.data.is_depend(i)
                response_value = self.data.get_response_result_field(i)
                is_post_previous_case = self.data.get_post_result(i)
                depend_excel_file = self.data.is_excel(i)

                if depend_case == None:

                    if header == 'write':
                        res = self.run_method.run_main(method, url,
                                                       request_data)
                        op_header = OperationHeader(res)
                        op_header.write_cookie()
                    elif header == 'yes':
                        op_json = OperetionJson('../dataconfig/cookie.json')
                        cookie = op_json.get_data('apsid')
                        cookies = {'apsid': cookie}
                        headers1 = op_json.get_data('Authorization')
                        headers = {'Authorization': headers1}
                        if depend_excel_file == 'Y':
                            files = {
                                'file':
                                ('new1.xlsx',
                                 open("C:\\Users\\Aaron\\Desktop\\new1.xlsx",
                                      'rb'), 'application/vnd.ms-excel')
                            }
                            res = self.run_method.run_main(
                                method, url, request_data, headers, files,
                                cookies)
                        else:
                            convert_data = json.dumps(request_data)
                            res = self.run_method.run_main(method,
                                                           url=url,
                                                           data=convert_data,
                                                           headers=headers,
                                                           cookies=cookies)
                    else:
                        res = self.run_method.run_main(method, url,
                                                       request_data)
                    # res = self.run_method.run_main(method, url, request_data)

                elif depend_case == "write":
                    if header == 'write':
                        res = self.run_method.run_main(method, url,
                                                       request_data)
                        op_header = OperationHeader(res)
                        op_header.write_cookie()

                    elif header == 'yes':
                        op_json = OperetionJson('../dataconfig/cookie.json')
                        cookie = op_json.get_data('apsid')
                        cookies = {'apsid': cookie}
                        headers1 = op_json.get_data('Authorization')
                        headers = {'Authorization': headers1}
                        if depend_excel_file == 'Y':
                            files = {
                                'file':
                                ('new1.xlsx',
                                 open("C:\\Users\\Aaron\\Desktop\\new1.xlsx",
                                      'rb'), 'application/vnd.ms-excel')
                            }
                            res = self.run_method.run_main(
                                method, url, request_data, headers, files,
                                cookies)
                        else:
                            res = self.run_method.run_main(
                                method, url, request_data, headers, cookies)
                    else:
                        res = self.run_method.run_main(method, url,
                                                       request_data)

                    # res = self.run_method.run_main(method, url, request_data)
                    self.data.write_response_to_cell(
                        i, res.content.decode("utf-8"))

                else:
                    self.depend_data = DependdentData(depend_case)
                    # 获取依赖的key
                    depend_key = self.data.get_depend_field(i)

                    if is_post_previous_case == 'N':

                        depend_response_data = self.depend_data.get_data_for_excel(
                            i)
                    else:
                        #获取的依赖响应数据
                        depend_response_data = self.depend_data.get_data_for_key(
                            i)
                    request_data[depend_key] = depend_response_data
                    if header == 'write':
                        res = self.run_method.run_main(method, url,
                                                       request_data)
                        op_header = OperationHeader(res)
                        op_header.write_cookie()

                    elif header == 'yes':
                        op_json = OperetionJson('../dataconfig/cookie.json')
                        cookie = op_json.get_data('apsid')
                        cookies = {'apsid': cookie}
                        headers1 = op_json.get_data('Authorization')
                        headers = {'Authorization': headers1}

                        files = {
                            'file':
                            ('new1.xlsx',
                             open("C:\\Users\\Aaron\\Desktop\\new1.xlsx",
                                  'rb'), 'application/vnd.ms-excel')
                        }
                        request_data = json.dumps(request_data)
                        print(type(request_data))
                        res = self.run_method.run_main(method,
                                                       url=url,
                                                       data=request_data,
                                                       headers=headers,
                                                       cookies=cookies)

                    else:
                        res = self.run_method.run_main(method, url,
                                                       request_data)

                # if depend_case == "write":
                # 	self.data.write_response_to_cell(i,res.decode("utf-8"))

                # if self.com_util.is_equal_dict(expect,json.dumps(res)) == 0:

                if self.com_util.is_contain(
                        expect, res.content.decode("utf-8")) == True:
                    self.data.write_result(i, 'pass')
                    pass_count.append(i)
                else:
                    # self.data.write_result(i,json.dumps(res,ensure_ascii=False))
                    self.data.write_result(i, res.content.decode("utf-8"))
                    fail_count.append(i)
        self.send_mai.send_main(pass_count, fail_count)