def test_login_case(cls, data): u"""登录成功/失败场景""" res = cls.login_msg(data['url'], json.loads(data['headers']), json.loads(data['data'])) #转换为dict atp_log.info("接口调用......") result = json.loads(res.text)[data['msg']] #获取response的关键信息 atp_log.info('断言:【%s】?=【%s】' % (result, data['expect_res'])) cls.assertIn(data['expect_res'], result) #断言
def setUpClass(cls): atp_log.info('=====测试开始=====') warnings.simplefilter("ignore", ResourceWarning) # 忽略ResourceWarning #cls.s = requests.session() #session关联,会话保持 #cls.data_dic = opexcel.get_test_data(opexcel.get_param()) #从excel获取的参数,用例继承该父类,可直接使用参数 cls.writeResult = WriteResult() cls.cell = 1 # 初始化结果excel cls.file_path = os.path.join(bases.PARAM_PATH, bases.PARAM_NAME) cls.workbook = xlrd.open_workbook(cls.file_path) cls.new_workbook = copy(cls.workbook) # 复制新的工作表 #cls.new_worksheet = cls.new_workbook.get_sheet(0) cls.new_workbook.save( os.path.join(bases.REPORT_PATH, bases.RESULT_NAME)) #结果xls文件创建
def get_response_data(self, str, regex): """ 从接口返回结果,根据regex表达式取出对应数据 :param str: 接口返回结果 :param regex: jsonpath表达式 :return: """ if str !=''and str.endswith('}') and regex !='': pyjson = json.loads(str) #返回参数的json字串转换为dict #pyjson = eval(str) data =jsonpath.jsonpath(pyjson,regex) #根据jsonpath表达式查找对应数据 if isinstance(data, list): return data[0] #jsonpath提取返回结果为list else: atp_log.error('关联参数获取为None') return '' elif str !=''and str.endswith('>') and regex !='': atp_log.error('暂时没有支持xml解析,待下个版本优化') else: atp_log.info('接口返回数据为空或Excel中depend_data为空')
def test_login_fail(cls): u"""登录失败场景""" atp_log.info('==========测试账号密码错误登录失败场景==========') test_login_fail_data = opexcel.get_test_data( opexcel.get_param("LoginCase"), "test_login_fail") # if not test_login_fail_data: atp_log.warning("未获取到用例数据") url = test_login_fail_data.get('url') atp_log.info("读取URL--【%s】" % url) headers = test_login_fail_data.get('headers') data = test_login_fail_data.get('data') atp_log.info("接口参数--【%s】" % data) expect_res = test_login_fail_data.get('expect_res') res = cls.s.post(url=url, headers=json.loads(headers), json=json.loads(data), verify=False) result = json.loads(res.text)["msg"] #se2 = "用户名或密码错误121" #账户或密码错误则返回错误data atp_log.info('断言:【%s】?=【%s】' % (expect_res, result)) cls.assertEqual(expect_res, result)
def test_login_success(cls): u"""登录成功场景""" atp_log.info('==========测试账号密码正确登录成功场景==========') test_login_success_data = opexcel.get_test_data( opexcel.get_param("LoginCase"), "test_login_success") # 类名与sheet页名一致,用例方法名与excel中case_name一致 if not test_login_success_data: atp_log.warning("未获取到用例数据") url = test_login_success_data.get('url') atp_log.info("读取URL--【%s】" % url) headers = test_login_success_data.get('headers') data = test_login_success_data.get('data') atp_log.info("接口参数--【%s】" % data) expect_res = test_login_success_data.get('expect_res') res = cls.s.post(url=url, headers=json.loads(headers), json=json.loads(data), verify=False) result = json.loads(res.text)["code"] #从请求返回中获取关键字 #se1 = "SUCCESS" #登录成功则返回SUCCESS atp_log.info('断言:【%s】?=【%s】' % (expect_res, result)) cls.assertEqual(expect_res, result) #断言
def tearDownClass(cls): atp_log.info('=====测试结束=====')
def test_login_success(cls): atp_log.info("测试LWJ登录成功场景") url = 'http://siteadmin-staging.liweijia.com/security/lv_check?type=normal&returnUrl=http%3A%2F%2Fsiteadmin-staging.liweijia.com%2F' atp_log.info("加载初始URL【%s】" % url) header = { "laravel_session": "hgsjpU63z4cDebMX6XvqWQ9jtaHEirCyP2qn8fpB", "sid": "node_aojia_25513rw1j4iqkiml18o130eh8n0y4.node_aojia_255", "LX-WXSRF-JTOKEN": "93910953-6139-4e9e-8ea7-595c6cf6c4d9" } data = {"lv_username": "******", "lv_password": "******"} res = requests.post(url=url, headers=header, data=data, verify=False, allow_redirects=False) print(res.headers) #字符串操作 #cookie_sid = res.headers['Set-Cookie'].split(';')[0] #cookie_LWJ = res.headers['Set-Cookie'].split(' ')[1].split(';')[0] #正则表达式 cookie_sid = re.findall(r'(sid=.+api)', str(res.headers))[0] #print(cookie_sid) cookie_LWJ = re.findall(r'(LX-WXSRF-JTOKEN=.+;P)', str(res.headers))[0].split(';')[0] print(cookie_LWJ) #print(res.headers['Set-Cookie']) #print(cookie_LWJ) atp_log.info("禁止重定向,获取response headers【%s,%s】" % (cookie_sid, cookie_LWJ)) new_cookie = cookie_sid + ';' + cookie_LWJ atp_log.info("组装新的headers【%s】" % new_cookie) url2 = 'http://cloud.sales-staging.liweijia.com/services/ums/isLogin' atp_log.info("加载登录验证URL【%s】" % url2) header2 = {"Cookie": new_cookie} res2 = cls.s.get(url=url2, headers=header2, verify=False) #print(json.loads(res2.text)) #print(type(json.loads(res2.text))) hope_data = json.loads(res2.text)['result']['name'] atp_log.info("获取验证关键信息【%s】" % hope_data) #print(hope_data) cls.assertIn("管理员", hope_data) atp_log.info("断言结果【%s】? =【管理员】" % hope_data)
def test_run_case(cls,i): result_dict = opexcel.result_dict #全局字典 if i['run'].lower() == 'yes': atp_log.info("==========参数获取==========") print("==========参数获取==========") #准备请求参数 url = i['url'] method = i['method'].lower() data = i['data'] expect = i['expect'] row = int(i['id'].split('-')[1] ) #当前用例在excel中的行数 #获取参数依赖 if i['depend_id'].startswith("smartpig"): depend_id_list = opexcel.relation_data(i['depend_id']) depend_data_list = opexcel.relation_data(i['depend_data']) atp_log.info("=========等待数据依赖处理==========") print("=========等待数据依赖处理==========") try: for j in range(len(depend_id_list)): depend_data = opexcel.get_response_data(result_dict[depend_id_list[j]], depend_data_list[j]) #获取到依赖接口返回的指定数据 if method == 'delete': if depend_data !='': url = os.path.join(url, depend_data) #将url拼接 else: data = json.loads(json.dumps(data).replace('$'+str(j+1),depend_data)) #以此替换json中的$1,$2....... # depend_data = opexcel.get_response_data(result_dict[i['depend_id']],i['depend_data']) #获取依赖的返回数据 # if method == 'delete': # url = os.path.join(url, depend_data) #将url拼接 # else: # data = data.replace('$',depend_data) #如果是json中的依赖参数,就替换 except Exception as e: atp_log.error("获取返回依赖数据失败--%s"%e) atp_log.info("【%s】--url:%s" % (method, url)) print("【%s】--url:%s" % (method, url)) atp_log.info("data == %s"%data) print("data == %s"%data) result = None try: result = myRequest.run_main(method, url, data) except Exception as e: atp_log.error("请求发送失败:%s"%e) atp_log.info("statusCode = 【%s】"%result.status_code) print("statusCode = 【%s】"%result.status_code) atp_log.info("result == %s"%result.text) print("result == %s"%result.text) result_dict[i['id']] = result.text #将返回结果添加到全局字典 #print(result_dict) cls.assertIn(expect, result.text) try: if expect in result.text: cls.writeResult.writeresult(row, "测试通过") else: cls.writeResult.writeresult(row, "测试失败") cls.writeResult.saveresult() except Exception as e: atp_log("测试结果写入失败-----【%s】"%e) else: #cls.assertEqual(1,1,"skip") row = int(i['id'].split('-')[1]) cls.writeResult.writeresult(row, "未执行")
def tearDown(cls): atp_log.info("======tearDown======")
def setUp(cls): cls.s = requests.session() warnings.simplefilter("ignore", ResourceWarning) atp_log.info("======ddt驱动模式======") atp_log.info("======setUp======")