class RunTest: def __init__(self): self.run_method = RunMethod() self.data = GetData() self.common = CommonUtil() self.mail = SendEmail() self.opera_json = OperationJson('../json_config/cookie.json') def go_on_run(self): logger.info("*************** 开始执行用例 ***************") res = None pass_count = [] fail_count = [] request_data = {} rows_count = self.data.get_case_lines() try: for i in range(1, rows_count): is_run = self.data.get_is_run(i) if is_run: url = self.data.get_url(i) method = self.data.get_request_method(i) data = json.dumps(self.data.get_data_for_json(i)) expect = self.data.get_expect_data(i) header = self.data.is_cookie(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, data) opera_header = OperationHeader() opera_header.write_cookie() if header != 'write': cookie = self.opera_json.get_data('JSESSIONID') cookies = {"JSESSIONID": cookie} cookies = json.dumps(cookies) res = self.run_method.run_main(method=method, url=url, data=data, cookies=cookies) if self.common.is_contain(expect, res): self.data.write_result(i, 'pass') pass_count.append(i) else: self.data.write_result(i, 'fail') fail_count.append(i) self.mail.send_main(pass_count, fail_count) except requests.exceptions.ConnectionError: print('连接错误') logger.info("*************** 结束执行用例 ***************") return res
class RunTest: def __init__(self): self.run_method = RunMethod() self.data = GetData() self.com_util = CommonUtil() self.send_email = SendEmail() 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) print '----->is_run: ',is_run if is_run: url = self.data.get_request_url(i) method = self.data.get_method(i) request_data = self.data.get_request_data_for_json(i) header = self.data.get_is_header(i) depend_case = self.data.get_depend_case(i) expect = self.data.get_expect(i) print '----->depend_case: ',depend_case if depend_case != '': dependdata = DependData(depend_case) depend_respone_data = dependdata.get_data_for_key(i) depend_key = self.data.get_depend_field(i) request_data[depend_key] = depend_respone_data print '----->header: ',header if header == 'write': op_header = OperationHeader(url, method, request_data) op_header.write_cookie() res = self.run_method.run_main(url, method, request_data) elif header == 'yes': op_json = OperationJson('../data/cookies.json') t_key = op_json.get_data('key') cookies = { "id":t_key } res = self.run_method.run_main(url, method, request_data, cookies) else: res = self.run_method.run_main(url, method, request_data) #print u'----The %s case\' result is:----\n%s' %(i, res) #print '------------------------' if self.com_util.is_contain(expect, res): self.data.write_resule(i, 'Pass') pass_count.append(i) else: self.data.write_resule(i, res) fail_count.append(i) print '--->over' self.send_email.send_main(pass_count, fail_count)
def run_depend(self): run_method = RunMethod() row_num = self.operEx.get_row_num(self.case_id) url = self.data.get_request_url(row_num) method = self.data.get_method(row_num) request_data = self.data.get_request_data_for_json(row_num) header = self.data.get_is_header(row_num) if header == 'yes': res = run_method.run_main(url, method, request_data, header) else: res = run_method.run_main(url, method, request_data) return json.loads(res)
class RunTest(object): def __init__(self): self.run_method = RunMethod() self.data = GetData() self.common_util = CommonUtil() self.send_email = SendEmail() 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)
class RunTest: def __init__(self): self.run_method = RunMethod() self.data = GetData() self.is_contain = CommonUtil() # 执行程序 def go_no_run(self, i): rows_conut = self.data.get_case_lines() # 获取行数 is_run = self.data.get_is_run(i) print(is_run) if is_run: url = self.data.get_url(i) method = self.data.get_request_way(i) # 获取请求的方法 request_data = self.data.get_json_data(i) # 获取请求数据 data_type = self.data.get_data_type(i) print(data_type) cookie = self.data.get_is_cookies(i) # 获取cookies expect = self.data.get_expect(i) # 获取预期结果 result = self.data.get_result(i) depend_case = self.data.get_depend_case_id(i) # 获取依赖的caseid if depend_case is not None: # 获取替换请求字段 depend_key = self.data.get_depned_key(i) # 获取依赖的响应数据 self.depend_data = DependData(depend_case) depend_response_data = self.depend_data.get_depned_value(i) split = len(depend_key.split(">")) # 切割替换的字段 # 根据请求字段的个数替换 for y in range(0, split): re = depend_key.split(">") request_data[re[y]] = depend_response_data[y] if cookie == 'write': res = self.run_method.run_main(method, url, request_data) op_cookie = OperationCookie(res) # 获取cookies op_cookie.write_cookie() # 写入cookies elif cookie == 'yes': get_cookies = OperationJson('../data/scm-cookies.json') cooki = get_cookies.get_data('SESSION') cookies = {"SESSION": cooki} res = self.run_method.run_main(method, url, request_data, cookies, data_type) else: res = self.run_method.run_main(method, url, request_data) if self.is_contain.is_contain(expect, res): self.data.write_data(i, 'pass') print(res) else: self.data.write_data(i, 'fail') print(res)
class RunTest: def __init__(self): self.run_method = RunMethod() self.data = GetData() self.comment_util = CommentUtil() self.send_mail = SendEmail() # 程序执行 def go_on_run(self): count = self.data.get_case_lines() pass_count = [] fail_count = [] for i in range(1, count): is_run = self.data.get_is_run(i) print(i) if is_run is True: url = self.data.get_request_url(i) method = self.data.get_request_method(i) data = self.data.get_data_for_json(i) header = self.data.is_header(i) expect = self.data.get_expect_data(i) # 如果用需要查询数据库则用下面这条 # expect = self.data.get_expect_data_for_mysql(i) depend_case = self.data.is_depend(i) if depend_case is not None: depend_data = DependentData(depend_case) # 获取依赖的相应数据 depend_response_data = depend_data.get_data_for_key(i) # 获取依赖的key depend_key = self.data.get_depend_filed(i) data[depend_key] = depend_response_data if header == 'write': res = self.run_method.run_main(method, url, data) op_header = OperationHerader(res) op_header.write_cookie() elif header == 'yes': op_json = OperationJson('../dataconfig/cookie.json') new_header = op_json.new_header() res = self.run_method.run_main(method, url=url, data=data, header=new_header) else: res = self.run_method.run_main(method, url, data) res = self.comment_util.remate_data(res) # 字符串比较用is_contain,字典比较用is_equal_dict result = self.comment_util.is_contain(expect, res) if result is True: self.data.write_value(i, 'pass') pass_count.append(i) else: self.data.write_value(i, res) fail_count.append(i)
class DependentData: def __init__(self, case_id): self.case_id = case_id self.operat_excel = OperationExcel() self.run_method = RunMethod() self.get_data = GetData() # 通关caseID来获取该caseID的整行数据 def get_case_line_data(self): rows_data = self.operat_excel.get_rows_data(self.case_id) return rows_data # 执行依赖测试。获取结果 def run_dependent(self): row = self.operat_excel.get_row_number(self.case_id) request_data = self.get_data.get_data_for_json(row) # header = self.get_data.is_header(row) url = self.get_data.get_request_url(row) method = self.get_data.get_request_method(row) res = self.run_method.run_main(method, url, request_data) return res.json() # 根据依赖的key去获取执行依赖测试case的相应,然后返回 def get_data_for_key(self, row): depend_data = self.get_data.get_depend_key(row) respense_data = self.run_dependent() json_exe = parse(depend_data) madle = json_exe.find(respense_data) data = [math.value for math in madle] if len(data) == 0: print('依赖数据异常') return None else: return data[0]
class DataDepend: def __init__(self): self.data = GetData() self.run_method = RunMethod() #根据case_id获取行号 def get_row_count(self,case_id,col): row_count = 0 case_datas = self.data.get_case_id(col) for id in case_datas: if case_id in id: return row_count row_count += 1 #获取返回值 def get_response_data(self,case_id,col): row_count = self.get_row_count(case_id,col) url = self.data.request_url(row_count) is_run = self.data.is_run(row_count) request_way = self.data.get_method_way(row_count) request_data = self.data.get_method_data_depend(row_count) if is_run: res = self.run_method.run_main(request_way,url,request_data) return res #获取依赖的数据值 def get_depend_data(self,row,case_id,col): depend_data = self.data.get_response_depend_data(row) response = self.get_response_data(case_id,col) jsonpath_expr = parse(depend_data) res_data = jsonpath_expr.find(response) return [match.value for match in res_data]
class RunTest(object): """这是RunTest""" def __init__(self): self.run_method = RunMethod() self.data = GetData() self.common_util = CommonUtil() self.send_email = SendMail() def go_on_run(self): pass_list = [] fail_list = [] rows_count = self.data.get_case_lines() for i in range(1, rows_count): res = None url = self.data.get_request_url(i) method = self.data.get_request_method(i) is_run = self.data.get_is_run(i) data = self.data.get_request_data_by_json(i) header = self.data.get_header(i) expect_result = self.data.get_expect_result(i) if is_run == "yes": res = self.run_method.run_main(method, url, data, header) if self.common_util.is_contain(expect_result, res): print("用例{}测试通过".format(i)) pass_list.append(i) self.data.write_actual_result(i, 'PASS') else: print("用例{}测试失败".format(i)) fail_list.append(i) self.data.write_actual_result(i, res) print(res) print("=============================") print("{}通过".format(len(pass_list))) print("{}失败".format(len(fail_list))) self.send_email.send_main(pass_list, fail_list)
class RunTest(object): def __init__(self): self.common_util = CommonUtil() self.send_email = SendEmail() self.connect_db = OperationMySQL() self.run_method = RunMethod() self.op_json = OperationJson() def run(self): res = None pass_count = [] fail_count = [] rows_count = self.connect_db.get_count() print(rows_count) for i in range(0, rows_count): run = self.connect_db.search('run', i) if run: url = self.connect_db.search('url', i) request_method = self.connect_db.search('request_method', i) request_data = self.op_json.get_data( self.connect_db.search('request_data', i)) expect = self.connect_db.search('expect', i) print(expect) res = self.run_method.run_main(request_method, url, request_data) print(res) if self.common_util.is_contain(expect, res) == 1: self.connect_db.write('pass', i + 1) pass_count.append(i + 1) print(pass_count) else: self.connect_db.write(res, i + 1) fail_count.append(i + 1) print(fail_count) self.send_email.send_main(pass_count, fail_count)
class LoginApi: def __init__(self): self.data = GetData( "/Users/mac/Desktop/测试资料/python_jiekou_auto/python_jiekou_git/Demo/dataconfig/login.xls" ) self.run_method = RunMethod() self.util = CommonUtil() def go_run_login(self): # 获取用例行数 row_count = self.data.get_case_line() #循环用例 for i in range(1, row_count): #获取isrun,是否运行该用例 is_run = self.data.get_is_run(i) if is_run: #获取url url = self.data.get_url(i) request_type = self.data.get_request_type(i) header = self.data.get_header(i) request_data = self.data.get_data_for_json(i) response = self.run_method.run_main(request_type, url, request_data, header) print("返回结果:", response) expect_res = self.data.get_except(i) print("期望结果:", expect_res) if self.util.is_contain(expect_res, response): self.data.write_result(i, "测试通过") print("测试通过") else: self.data.write_result(i, "测试失败") print("测试失败") return response
def run_depnedent(self): run_method = RunMethod() row_num = self.operation.get_rows_num(self.case_id) # 获取行号 request_data = self.data.get_json_data(row_num) # 获取请求数据 method = self.data.get_request_method(row_num) # 获取请求方法 url = self.data.get_request_url(row_num) cookies = {"SESSION": "bdbd5d09-29c5-40f8-aa16-cae274c1c6cd"} cookie = self.data.get_cookies_value(row_num) if cookie == 'yes': get_cookie = OperationJson('../dataconfig/cookie.json') cooki = get_cookie.get_data('SESSION') cookies = {"SESSION": cooki} res = run_method.run_main(method, url, request_data, cookies) # 发送请求 else: res = run_method.run_main(method, url, request_data) return json.loads(res)
def run_depend_case(self): run_method = RunMethod() row_num = self.operation.get_row_num(self.casi_id) # 获取行号 request_data = self.data.get_json_data(row_num) # 获取请求数据 request_method = self.data.get_request_way(row_num) # 获取请求方法 url = self.data.get_url(row_num) cookie = self.data.get_is_cookies(row_num) if cookie == 'yes': operation = OperationJson('../data/scm-cookies.json') cookie = operation.get_data('SESSION') cookies = { "SESSION": cookie } res = run_method.run_main(request_method, url, request_data, cookie) else: res = run_method.run_main(request_method, url, request_data) return json.loads(res)
def run_denpendent(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
def run_dependent(self): run_merhod = RunMethod() row_num = self.Open_excel.get_row_num(self.case_id) request_data = self.data.get_data_json(row_num) header = self.data.get_is_hander(row_num) method = self.data.get_request_method(row_num) url = self.data.get_request_url(row_num) res = run_merhod.run_main(method, url, request_data, header) return json.loads(res)
def run_dependent(self): run_method = RunMethod() row_num = self.op_excel.get_row_num(self.case_id) request_data = self.data.get_data_for_json(row_num) # header = self.data.get_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) return json.loads(res)
def run_dependdent(self): run_method = RunMethod() row = int(self.opera_excel.get_rows_num(self.case_id)) request_data = self.data.get_data_for_json(row) cookie = self.data.is_cookie(row) method = self.data.get_request_method(row) url = self.data.get_url(row) res = run_method.run_main(method, url, request_data, cookie) return res
class RunTest(): def __init__(self): self.run_method = RunMethod() self.data = GetData() self.com_util = CommonUtil() self.depend_data = DependentData() # 程序执行 def go_on_run(self): res = None #获取case个数(获取有数据的单元格行) rows_count = self.data.get_case_lines() #循环判断要执行的case,跳过第一行标题开始循环 for i in range(1, rows_count): #获取是否执行 is_run = self.data.get_is_run(i) # print is_run # #判断is_run的值为true时执行 if is_run: # print i #获取url url = self.data.get_request_url(i) # print url #获取请求类型 method = self.data.get_request_method(i) # print method #获取数据 data = self.data.get_data_for_json(i) # print data expect = self.data.get_expect_data(i) #获取header(默认demo) # header = self.data.demo_header() #获取excel中指定header header = self.data.is_header(i) # print header #获取依赖数据 depend_case = self.data.is_depend(i) #判断是否需要依赖数据 if depend_case != None: #获取依赖的响应数据 depend_response_data = self.depend_data.get_data_for_key(i) #获取依赖key depend_key = self.data.get_depend_field(i) #将获取到的依赖case响应数据赋值给依赖key request_data[depend_key] = depend_response_data #获取响应数据 res = self.run_method.run_main(method, url, data, header) return res #打印结果并转码为字符串类型 # print res.text.encode('unicode-escape').decode('string_escape') #判断返回结果与预期结果比对 if self.com_util.is_contain(expect, res): self.data.write_result(i, 'pass') else: self.data.write_result(i, 'false')
class RunMain: #实例化一些基本的工具个data def __init__(self): self.opera_excel = OperationExcel() self.data = GetData() self.run_method = RunMethod() self.comm_util = CommUtil() self.send_mail = SendMail() #主函数运行入口 def go_on_run(self): pass_count = 0 fail_count = 0 #获取数据行数 rows_count = self.opera_excel.get_rows() #循环数据,并取值进行post或get请求,并获取结果 for i in range(1, rows_count): is_run = self.data.is_run(i) # print(expact_result) if is_run: url = self.data.request_url(i) request_data = self.data.get_method_data_depend(i) # print(request_data) cookies = self.data.get_cookies(i) request_way = self.data.get_method_way(i) case_depend = self.data.get_case_depend_data(i) # print(case_depend) expact_result = self.data.expact_result(i) request_data_depend = self.data.get_method_data(i) #查看是否有数据依赖 if case_depend != '': data_depend = DataDepend() response_depend = data_depend.get_depend_data( i, case_depend, 0) request_data[request_data_depend] = response_depend #判断是否需要写入或读取cookie if cookies == 'write': cookie_data = OperationCookie().get_cookie( request_way, url, request_data) OperationJson().write_json('../datas/cookie.json', cookie_data) elif cookies == 'yes': cookie_data = OperationJson().get_cookie_data( '../datas/cookies.json') res = self.run_method.run_main(request_way, url, request_data) result = self.comm_util.comm_util(str(res['code']), expact_result) if result: self.data.write_data(i, 'Pass') pass_count += 1 else: self.data.write_data(i, 'Fail') fail_count += 1 self.send_mail.send_main(pass_count, fail_count)
def run_dependent(self): run_method = RunMethod() row_num = self.oper_excel.get_row_num(self.case_id) print("row_num:", row_num) row_data = self.get_case_line_data() print("row_data:", row_data) request_data = self.data.get_data_for_json(row_num) print("request_data:", request_data) url = self.data.get_url(row_num) print("url:", url) method = self.data.get_request_type(row_num) print("method:", method) token_header = self.data.get_token_header(row_num, self.util.getToken(0)) print("依赖的token_header:", token_header) res = run_method.run_main(method, url, request_data, token_header) print("res:", res) return json.loads(res)
class RunTest: def __init__(self, ): self.run_method = RunMethod() self.data = GetData() # 程序执行的 def go_on_run(self): rows_count = self.data.get_case_lines() res = None for i in range(1, rows_count): url = self.data.get_url(i) method = self.data.get_request_method(i) is_run = self.data.get_is_run(i) data = self.data.get_data_for_json(i) header = self.data.is_header(i) if is_run: res = self.run_method.run_main(method, url, data, header) print(res)
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 = [] rows_count = self.data.get_case_lines() print(rows_count) for i in range(1, rows_count): #获取excel表格中的数据 url = self.data.get_request_url(i) method = self.data.get_request_method(i) is_run = self.data.get_is_run(i) data = self.data.get_request_data(i) header = self.data.is_header(i) expect = self.data.get_expcet_data(i) print(url, method, is_run, data, header, expect) #判断是否执行 if is_run: res = self.run_method.run_main(method, url, data, header) info(res) #print(res) #判断预期结果和实际结果 if self.com_util.is_contain(expect, res['data']): #如果通过就把行号写入定义的列表中 pass_count.append(i) #在excel表中的实际结果中输入测试结果 self.data.write_result(i, "pass") print("测试通过") else: print("测试失败") #把执行失败的行号写入定义的列表中 fail_count.append(i) print(len(pass_count)) print(len(fail_count)) self.send_mail.send_main(pass_count, fail_count)
class Test(unittest.TestCase): def setUp(self): self.run = RunMethod() self.ExcelData = OperationExcel() self.JsonData = OperationJson() def tearDown(self): pass def test_01(self): url = self.ExcelData.get_cell_value(1, 2) print url data = self.JsonData.get_data(self.ExcelData.get_cell_value(1, 4)) print data res = self.run.run_main(url, 'POST', data) print res print '123'
class RunTest: def __init__(self): self.run_method = RunMethod() self.data = GetData() self.com_util = CommonUtil() #程序执行的主入口 def go_on_run(self): res = None rows_count = self.data.get_case_lines() for i in range(1, rows_count): url = self.data.get_url(i) method = self.data.get_request_method(i) is_run = self.data.get_is_run(i) data = self.data.get_data_for_json(i) header = self.data.is_header(i) expect = self.data.get_expect(i) if is_run: res = self.run_method.run_main(method, url, data, header) if self.com_util.is_contain(expect, res): self.data.write_result(i, 'pass') else: self.data.write_result(i, 'fail')
class RunTest: def __init__(self): self.run_method = RunMethod() self.data = GetData() self.common_util = CommonUtil() self.email = SendEmail() def go_on_run(self): """获取用例行数""" row_count = self.data.get_case_line() print("用例行数:", row_count) pass_count = [] fail_count = [] #循环执行用例 for i in range(1, row_count): is_run = self.data.get_is_run(i) print("i的值:", i) print("is_run:", is_run) if is_run: url = self.data.get_url(i) print("url:", url) request_type = self.data.get_request_type(i) print("request_type:", request_type) data = self.data.get_data_for_json(i) print("data:", data) #dependent_case = self.data.is_depend(i) dependent_more_case = self.data.is_more_depend(i, 0) print("dependent_more_case:", dependent_more_case) if dependent_more_case != None: self.dependent_data = DependentData(dependent_more_case) #获取依赖的响应数据 dependent_response_data = self.dependent_data.get_data_for_key( i, 0) print("dependent_response_data", dependent_response_data) token_value = self.common_util.getToken(0) token_header = self.data.get_token_header(i, token_value) print("token_header:", token_header) if url == "http://182.61.33.241:8089/app/api/private/1.0/client/": new_url = url + dependent_response_data print("------------------------------------") print("new_url:", new_url) result = self.run_method.run_main( request_type, new_url, data, token_header) else: #获取依赖的key depend_key = self.data.get_dependent_key(i, 0) print("depend_key", depend_key) jsonData = json.loads(data) print("data:", data) jsonData[depend_key] = dependent_response_data requestData = json.dumps(jsonData) result = self.run_method.run_main( request_type, url, requestData, token_header) else: if i == 1: header = self.data.get_header(i) print("header:", header) #result = self.run_method.post_main(url, data, header) result = self.run_method.run_main( request_type, url, data, header) res = json.loads(result) #获取token值 with open(self.common_util.base_dir(), 'w') as f: f.write(res["data"]["token"] + "," + res["data"]["id"]) print("获取token:", res["data"]["token"]) else: token_value = self.common_util.getToken(0) token_header = self.data.get_token_header( i, token_value) if url == "http://182.61.33.241:8089/app/api/private/1.0/follow/clientpage": oper_id = self.common_util.getToken(1) jsonData = json.loads(data) jsonData["operId"] = oper_id request_data = json.dumps(jsonData) result = self.run_method.run_main( request_type, url, request_data, token_header) else: result = self.run_method.run_main( request_type, url, data, token_header) print("返回结果:", result) except_res = self.data.get_except(i) print("except_res:", except_res) if self.common_util.is_contain(except_res, result): self.data.write_result(i, "测试通过") print("测试通过") pass_count.append(i) else: self.data.write_result(i, "测试失败") print("测试失败") fail_count.append(i) print("打印结果:", result) print("通过数:", len(pass_count)) print("失败数:", len(fail_count)) # self.email.send_main(pass_count, fail_count) return result
class RunTest: def __init__(self): self.run_method = RunMethod() self.data = GetData() self.is_contain = CommonUtil() self.log = UserLog() self.logger = self.log.get_log() # 执行程序 def go_no_run(self): 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: url = self.data.get_url(i) case_name = self.data.get_case_name(i) method = self.data.get_request_way(i) # 获取请求的方法 request_data = self.data.get_json_data(i) # 获取请求数据 data_type = self.data.get_data_type(i) cookie = self.data.get_is_cookies(i) # 获取cookies expect = self.data.get_expect(i) # 获取预期结果 depend_case = self.data.get_depend_case_id(i) # 获取依赖的caseid if depend_case is not None: # 获取替换请求字段 depend_key = self.data.get_depned_key(i) # 获取依赖的响应数据 self.depend_data = DependData(depend_case) depend_response_data = self.depend_data.get_depned_value(i) split = len(depend_key.split(">")) # 切割替换的字段 # 根据请求字段的个数替换 for y in range(0, split): re = depend_key.split(">") request_data[re[y]] = depend_response_data[y] if cookie == 'write': res = self.run_method.run_main(method, url, request_data) op_cookie = WriteCookies(res) # 获取cookies op_cookie.write_cookie() # 写入cook ies elif cookie == 'yes': get_cookies = OperationJson('../data/scm-cookies.json') coo = get_cookies.get_data('SESSION') cooki = {"SESSION": coo} res = self.run_method.run_main(method, url, request_data, cooki, data_type) else: # get_cookies = OperationJson('../data/scm-cookies.json') # cooki = get_cookies.get_data('accessToken') # request_data['accessToken'] = cooki res = self.run_method.run_main(method, url, request_data) if self.is_contain.is_contain(expect, res): self.data.write_data(i, 'pass') self.logger.info("第" + str(i) + "个case-->" + case_name + ": 测试通过") self.logger.info("url--> " + url) self.logger.info("request_data-->" + str(request_data)) self.logger.info("response_data-->" + res) print("第" + str(i) + "个case-->" + case_name + ": 测试通过") print(res) else: self.data.write_data(i, res) self.logger.info("第" + str(i) + "个case-->" + case_name + ": 测试失败") self.logger.info("url--> " + url) self.logger.info("request_data-->" + str(request_data)) self.logger.info("response_data-->" + res) print("第" + str(i) + "个case-->" + "测试失败") print(res)
class Run: def __init__(self, json_file, sheet_name, sheet_id, sql_base=None): self.json_file = json_file self.sheet_name = sheet_name self.sheet_id = sheet_id self.sql_base = sql_base self.data = GetData(self.json_file, self.sheet_name) self.run_method = RunMethod() self.util = CommonUtil() self.email = SendEmail() # 连接数据库,查询某字段的所有记录、总数、页数 def get_sql_expect(self, i, sql_key): token = self.util.getToken(0) operid = self.util.getToken(1) # token_header = self.data.get_token_header(i, token) expect_res = self.data.get_except_data_for_sql( i, self.sql_base) # 获取数据库返回的所有楼盘记录(返回类型:数组型) expect_value_list = [] if len(expect_res) == 0: count = 0 new_count = 1 return count, new_count, expect_value_list else: for expect_index in range(0, len(expect_res)): expect_value = expect_res[expect_index][ sql_key] # 循环获取数据库返回的某个字段 expect_value_list.append(expect_value) # 将数据库中所有该字段存入该数组中 count = len(expect_value_list) if count / 15 < 1: new_count = 1 elif (count / 15) >= 1 and (count % 15) >= 1: new_count = int(count / 15) + 1 else: new_count = int(count / 15) return count, new_count, expect_value_list # 获取response def is_depend(self, i, depend_morecase, request_type, url, token_header, data): # 判断是否存在依赖 if depend_morecase != None: depend_data = DependentData(depend_morecase, self.sheet_name, self.json_file) dependent_response_data = depend_data.get_data_for_key(i, 0) if request_type == "get": request_url = url + dependent_response_data response = self.run_method.run_main(request_type, request_url, data, token_header) else: depend_key = self.data.get_dependent_key(i, 0) jsonData = json.loads(data) jsonData[depend_key] = dependent_response_data requestData = json.dumps(jsonData) response = self.run_method.run_main( request_type, url, requestData, token_header) # 获取接口返回数据(返回类型:string型) else: response = self.run_method.run_main( request_type, url, data, token_header) # 获取接口返回数据(返回类型:string型) return response # 判断expect表中记录和response列表记录是否相等,返回result是True或者False def get_result(self, count, expect_value_list, response, response_key): is_result = True response_dict = json.loads(response) # 将response字符串型转换成字典型 # if self.type == "data": # response_list = response_dict['data'] # elif self.type == "list": # response_list = response_dict['data']['list'] response_dict_data = response_dict['data'] if 'list' in response_dict_data: response_list = response_dict['data']['list'] else: response_list = response_dict_data if len(response_list) == count: is_result = True else: for response_index in range(len(response_list)): response_name = response_list[response_index][ response_key] # 循环获取接口返回的字段名称 if self.util.is_contain( response_name, expect_value_list): # 判断如果接口返回的xx字段存在数据库xx字段数组中,则测试通过 is_result = True else: is_result = False return is_result # 判断expect表中记录的总数和response中某字段的数量是否相等,返回result是True或者False def get_result_count(self, count, response, response_key): response_dict = json.loads(response) # 将response字符串型转换成字典型 response_opt = response_dict['data'][response_key] if int(response_opt) == count: result = True else: result = False return result # 写入实际结果 def write_result(self, fail_count, i, pass_count, result): if result: self.data.write_result(i, "测试通过", self.sheet_id) pass_count.append(i) print("测试通过") else: self.data.write_result(i, "测试失败", self.sheet_id) fail_count.append(i) print("测试失败") # 执行用例 def go_to_run(self): # 获取用例行数 row_count = self.data.get_case_line() pass_count = [] fail_count = [] result = True # 循环用例 for i in range(1, row_count): # 获取isrun,是否运行该用例 is_run = self.data.get_is_run(i) response_key = self.data.get_response_key(i) sql_key = self.data.get_sql_key(i) sql_type = self.data.get_sql_type(i) if is_run: url = self.data.get_url(i) request_type = self.data.get_request_type(i) is_connect = self.data.get_is_conn_db( i) # 获取是否连接数据库,若是则执行sql,若不是直接获取期望结果 depend_morecase = self.data.is_more_depend(i, 0) # 获取依赖数据 data = self.data.get_data_for_json(i) if is_connect: token = self.util.getToken(0) token_header = self.data.get_token_header(i, token) # 获取预期结果 count = self.get_sql_expect(i, sql_key)[0] # 数据库查询结果记录总数 new_count = self.get_sql_expect(i, sql_key)[1] # 获取接口执行次数,翻页 expect_value_list = self.get_sql_expect( i, sql_key)[2] # 获取某个字段在表中的所有值 # 获取返回数据 response = self.is_depend(i, depend_morecase, request_type, url, token_header, data) if sql_type == "count": # 比较接口返回的总数与数据库查询出的记录总数是否相等,相等返回True actrual_result = self.get_result_count( count, response, response_key) else: for n in range(1, new_count + 1): data = self.data.get_data_for_newjson( i, '"' + str(n) + '"') # 比较预期结果和返回结果是否相等,相同返回result=True actrual_result = self.get_result( count, expect_value_list, response, response_key) self.write_result(fail_count, i, pass_count, actrual_result) else: expect_res = self.data.get_except(i) if i == 1: header = self.data.get_header(i) response = self.run_method.run_main( request_type, url, data, header) res = json.loads(response) # 获取token、operid并写入文件中 with open(self.util.base_dir(), 'w') as f: f.write(res["data"]["token"] + "," + res["data"]["id"]) # print("获取token:", res["data"]["token"]) else: token = self.util.getToken(0) operid = self.util.getToken(1) token_header = self.data.get_token_header(i, token) response = self.is_depend(i, depend_morecase, request_type, url, token_header, data) # response = self.run_method.run_main(request_type, url, data, token_header) # print("返回结果:", response) result = self.util.is_contain(expect_res, response) self.write_result(fail_count, i, pass_count, result) self.data.write_response(i, response, self.sheet_id) # self.email.send_main(pass_count, fail_count) return response
class RunTest: def __init__(self): self.run_method = RunMethod() self.data = GetData() self.is_contain = CommonUtil() self.log = UserLog() self.logger = self.log.get_log() # 执行程序 def go_no_run(self): 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: url = self.data.get_url(i) case_name = self.data.get_case_name(i) method = self.data.get_request_way(i) # 获取请求的方法 request_data = self.data.get_json_data(i) # 获取请求数据 data_type = self.data.get_data_type(i) cookie = self.data.get_is_cookies(i) # 获取cookies expect = self.data.get_expect(i) # 获取预期结果 depend_case = self.data.get_depend_case_id(i) # 获取依赖的caseid if depend_case is not None: # 获取替换请求字段 depend_key = self.data.get_depned_key(i) # 获取依赖的响应数据 self.depend_data = DependData(depend_case) depend_response_data = self.depend_data.get_depned_value(i) split = len(depend_key.split(">")) # 切割替换的字段 # 根据请求字段的个数替换 for y in range(0, split): re = depend_key.split(">") request_data[re[y]] = depend_response_data[y] if cookie == 'write': res = self.run_method.run_main(method, url, request_data) op_cookie = WriteCookies(res) # 获取cookies op_cookie.write_cookie() # 写入cook ies elif cookie == 'yes': father_path = os.path.join( os.path.dirname( os.path.dirname(os.path.abspath(__file__))), 'data') file_path = os.path.join(father_path, '金城-cookies.json') get_cookies = OperationJson(file_path) cooki = get_cookies.get_data('accessToken') request_data['accessToken'] = cooki res = self.run_method.run_main(method, url, request_data, data_type) else: father_path = os.path.join( os.path.dirname( os.path.dirname(os.path.abspath(__file__))), 'data') file_path = os.path.join(father_path, '金城-cookies.json') get_cookies = OperationJson(file_path) cooki = get_cookies.get_data('accessToken') request_data['accessToken'] = cooki res = self.run_method.run_main(method, url, request_data) if self.is_contain.is_contain(expect, res): self.data.write_data(i, 'pass') # self.logger.info("第" + str(i) + "个case-->" + case_name + ": \033[31m测试通过\033[0m") self.logger.info("第%s 个case-->%s: 测试通过" % (str(i), case_name)) self.logger.info("url--> " + url) self.logger.info("request_data-->" + str(request_data)) self.logger.info("response_data-->" + res) print("第" + str(i) + "个case-->" + case_name + ": 测试通过") # print("第" + str(i) + "个case-->" + case_name + ": \033[32m测试通过\033[0m") else: self.data.write_data(i, res) self.logger.info("第" + str(i) + "个case-->" + case_name + ": 测试失败") self.logger.info("url--> " + url) self.logger.info("request_data-->" + str(request_data)) self.logger.info("response_data-->" + res) # print("第" + str(i) + "个case-->" + "\033[35m测试失败\033[0m") print("第%s 个case-->%s: 测试失败" % (str(i), case_name))
class RunMain: def __init__(self): self.run_method = RunMethod() self.data = RequestData() self.util = CommonUtil() self.send_email = SendMail() #程序执行主入口 def get_go_run(self): #日志打印配置 logs_path = config.get_run_logs_path() if os.path.exists(r"{path}".format(path=logs_path)): os.remove(r"{path}".format(path=logs_path)) logging_setting(logs_path) #用例主体方法 res = None pass_count = [] fail_count = [] rows_count = self.data.get_case_lines() for i in range(1, rows_count): url = self.data.get_request_url(i) request_mode = self.data.get_request_method(i) is_run = self.data.get_is_run(i) is_number = self.data.get_number_value(i) data = self.data.get_data_json(i) expect = self.data.get_expect_data(i) hander = self.data.get_is_hander(i) depend_case = self.data.is_depend(i) if is_run == True: if depend_case != None: self.depend_data = DependtndData() #获取依赖的响应数据 depend_response_data = self.depend_data.get_data_for_key(i) #获取依赖的key depend_key = self.data.get_depend_data(i) request_mode[depend_key] = depend_response_data if hander == "W": res = self.run_method.run_main(request_mode, url, data) op_hander = OpenHeader(url, data) op_hander.write_cookie() elif hander == "Y": op_json = OpenJson( os.path.abspath('..' + '/json/cookies.json')) cookie = op_json.get_cookie() res = self.run_method.run_main(request_mode, url, data, cookie) else: res = self.run_method.run_main(request_mode, url, data) if self.util.is_contain(expect, res): self.data.write_excel(i, "pass") pass_count.append(i) logging.debug("test case (%s) True" % is_number) logging.debug("pass_count:%s True" % len(pass_count)) else: self.data.write_excel(i, res) fail_count.append(i) logging.error("test case (%s) Flase" % is_number) logging.debug("fail_count:%s True" % len(fail_count)) logging.debug("发送邮件中") self.send_email.send_main(pass_count, fail_count) get_Message_push(pass_count, fail_count) logging.debug("发送邮件成功")
class GuestCase: def __init__(self): self.json_file = "/Users/mac/Desktop/测试资料/蜗牛家产品线/woniujia_cc_jiekou/woniujia_cc_jiekou_git/woniujia_cc_project/dataconfig/request_pram.json" self.sheet_name = "客户模块" self.sheet_id = 5 self.data = GetData(self.json_file, self.sheet_name, self.sheet_id) self.run_method = RunMethod() self.util = CommonUtil() self.email = SendEmail() def go_to_guest(self): # self.run_comm = RunCommon(self.excel_file, self.json_file) # self.run_comm.go_run_case() # 获取用例行数 row_count = self.data.get_case_line() pass_count = [] fail_count = [] for i in range(1, row_count): is_run = self.data.get_is_run(i) if is_run: url = self.data.get_url(i) request_type = self.data.get_request_type(i) data = self.data.get_data_for_json(i) if i == 1: header = self.data.get_header(i) response = self.run_method.run_main( request_type, url, data, header) res = json.loads(response) # 获取token、operid并写入文件中 with open(self.util.base_dir(), 'w') as f: f.write(res["data"]["token"] + "," + res["data"]["id"]) else: token = self.util.getToken(0) token_header = self.data.get_token_header(i, token) depend_morecase = self.data.is_more_depend(i, 0) # 判断是否存在依赖 if depend_morecase != None: self.depend_data = DependentData( depend_morecase, self.excel_file, self.json_file) dependent_response_data = self.depend_data.get_data_for_key( i, 0) depend_key = self.data.get_dependent_key(i, 0) print("depend_key", depend_key) if url == "http://182.61.33.241:8089/app/api/private/1.0/client/": request_url = url + dependent_response_data response = self.run_method.run_main( request_type, request_url, data, token_header) if url == "http://182.61.33.241:8089/app/api/private/1.0/reporting/addorupdate": depend_param2 = self.data.is_more_depend(i, 1) self.depend_data2 = DependentData( depend_param2, self.excel_file, self.json_file) dependent_response_data2 = self.depend_data2.get_data_for_key( i, 1) depend_key2 = self.data.get_dependent_key(i, 1) jsonData = json.loads(data) jsonData[depend_key] = dependent_response_data jsonData[depend_key2] = dependent_response_data2 requestData = json.dumps(jsonData) response = self.run_method.run_main( request_type, url, requestData, token_header) else: jsonData = json.loads(data) print("data:", data) jsonData[depend_key] = dependent_response_data requestData = json.dumps(jsonData) response = self.run_method.run_main( request_type, url, requestData, token_header) else: if url == "http://182.61.33.241:8089/app/api/private/1.0/follow/clientpage": oper_id = self.util.getToken(1) json_data = json.loads(data) json_data["operId"] = oper_id request_data = json.dumps(json_data) response = self.run_method.run_main( request_type, url, request_data, token_header) else: response = self.run_method.run_main( request_type, url, data, token_header) expect_res = self.data.get_except(i) print("期望结果:", expect_res) if self.util.is_contain(expect_res, response): self.data.write_result(i, "测试通过", self.sheet_id) pass_count.append(i) print("测试通过") else: self.data.write_result(i, "测试失败", self.sheet_id) fail_count.append(i) print("测试失败") self.data.write_response(i, response, self.sheet_id) self.email.send_main(pass_count, fail_count) return response