def test_register(self, case): # 回写结果的row row = case.case_id + 1 # 从excel中读取数据,准备请求参数 # 拼接完整的url new_url = do_yaml.read_yaml('api', 'prefix') + case.url # 参数化获取发起请求的用例数据 new_data = Parameterize.to_parameter(case.data) # 对象调用类属性向服务器发起请求 res = self.do_request.send(new_url, data=new_data) # 将响应报文中的json格式数据转换为字典,得到实际结果:响应体 actual_value = res.json() # 从excel中获取excepted excepted_result = case.excepted # 将json格式的字符串转换为字典格式,得到期望值 excepted_result = json.loads(excepted_result, encoding='utf-8') # 获取title msg = case.title # 从配置文件中读取断言结果:成功 success_result = do_yaml.read_yaml('excel', 'success_result') # 从配置文件中读取断言结果:失败 fail_result = do_yaml.read_yaml('excel', 'fail_result') # 比较预期和实际结果 # 捕获异常,回写结果,打印日志,并主动抛出异常 try: # 多字段断言 self.assertEqual(actual_value.get('code'), excepted_result.get('code'), msg=msg) self.assertEqual(actual_value.get('msg'), excepted_result.get('msg'), msg=msg) except AssertionError as e: # 回写断言结果 self.excel.write_result(row, do_yaml.read_yaml('excel', 'result_col'), fail_result) # 将异常内容写入日志 # do_log.error(f'{msg},执行结果为:{fail_result},具体异常为:{e}\n') do_log.error({e}) # 主动抛出异常 raise e else: # 回写断言结果 self.excel.write_result(row, do_yaml.read_yaml('excel', 'result_col'), success_result) # 打印日志 do_log.info(f'{msg},执行结果为:{success_result}') finally: # 将返回的响应体写入excel self.excel.write_result(row, do_yaml.read_yaml('excel', 'actual_col'), res.text)
def test_sendmcode(self,case): """ 测试发送短信验证码接口 :return: """ # 处理获取的表数据 msg=case['title'] url = http_host + case['url'] data = eval(Context.new_mobile_tel_replace(str(case['data']))) method = case['method'] # 创建一个webservice请求对象 dowebservice = DoWebService(url=url) # 发起数据请求 request_res = dowebservice.webservice_requests(post=method,param=data) # 判断请求方式是否有误 if request_res == 'PostError': do_log.info('{}\t执行请求方式有误!:{}\n'.format(msg, case['method'])) except_result = case['expected'] try: # 断言请求结果 self.assertIn(except_result,str(request_res),msg=msg+'失败!') do_log.info('{}\t执行结果:{}\n'.format(msg,'Pass')) do_excel.write_result(case['case_id']+1,str(request_res),'Pass') except AssertionError as e: do_log.error('{}执行结果:{}\t报错信息:{}\n'.format(msg,'Fail',e)) do_excel.write_result(case['case_id']+1,str(request_res),'Fail') raise e
def tearDownClass(cls): """ 在当前类中所有实例方法执行结束之后会被调用一次 :return: """ do_log.info("\n{:=^40s}\n".format("加标功能用例执行结束")) cls.send_request.close()
def test_register(self, data_namedtuple): """ 测试注册功能 :return: """ url = do_config("api", "url") + data_namedtuple.url new_data = HandleContext().register_parameterization(data_namedtuple.data) do_log.info("\n正在运行的测试用例为:{}".format(data_namedtuple.title)) response = self.send_request(method=data_namedtuple.method, url=url, data=new_data) # 判断服务器是否有异常 try: self.assertEqual(200, response.status_code, msg="测试【{}】失败,http请求状态码为【{}】". format(data_namedtuple.title, response.status_code)) except AssertionError as e: do_log.error("\n具体异常信息为:{}\n".format(e)) raise e try: self.assertEqual(data_namedtuple.expected, response.text, msg="测试{}失败".format(data_namedtuple.title)) except AssertionError as e: do_log.error("\n具体异常信息为:{}\n".format(e)) self.do_excel.write_result(row=data_namedtuple.case_id+1, actual=response.text, result=do_config("msg", "fail_result")) raise e else: self.do_excel.write_result(row=data_namedtuple.case_id+1, actual=response.text, result=do_config("msg", "success_result"))
def setUpClass(cls): cls.do_request = HandleRequest() cls.do_request.add_headers(do_yaml.get_data("api", "api_version")) cls.do_mysql = HandleMysql() # cls.do_mysql.create_not_existed_mobile() do_log.info("开始执行用例")
def setUpClass(cls): # 构造请求参数 cls.do_request = HandleRequest() # 创建HandleRequest对象 cls.do_request.add_headers(do_yaml.get_data( "api", "api_version")) # 添加请求头到公共请求头 do_log.info("开始执行用例")
def setUpClass(cls): # setUpClass是unittest框架的方法,会被unittest框架自动调用 """ 在当前类中所有实例方法执行之前会被调用一次 重写父类的类方法 :return: """ cls.send_request = HandleRequest() do_log.info("\n\n{:=^40s}".format("开始执行加标功能用例"))
def tearDownClass(cls) -> None: """ 在当前类中所有实例方法执行结束之后会被调用一次 :return: """ do_log.info("\n{:=^40s}\n".format("投资功能用例执行结束")) cls.send_request.close() cls.handle_mysql.close()
def test_register(self, case): # src_data = case.data # re.sub(r'{not_existed_tel}', '', src_data) # 1. 参数化 new_data = Parameterize.to_param(case.data) # 2. 拼接完整的url new_url = do_yaml.read('api', 'prefix') + case.url # 3. 向服务器发起请求 res = self.do_request.send( url=new_url, # url地址 # method=case.method, # 请求方法 data=new_data, # 请求参数 # is_json=True # 是否以json格式来传递数据, 默认为True ) # 将相应报文中的数据转化为字典 actual_value = res.json() # 获取用例的行号 row = case.case_id + 1 # 获取预期结果 # excepted = eval(case.excepted) expected_result = case.expected msg = case.title # 获取标题 success_msg = do_yaml.read('msg', 'success_result') # 获取用例执行成功的提示 fail_msg = do_yaml.read('msg', 'fail_result') # 获取用例执行失败的提示 try: # assertEqual第三个参数为用例执行失败之后的提示信息 # assertEqual第一个参数为期望值, 第二个参数为实际值 self.assertEqual(expected_result, actual_value.get('code'), msg=msg) except AssertionError as e: # 将相应实际值写入到actual_col列 self.excel.write_data(row=row, column=do_yaml.read("excel", "actual_col"), value=res.text) # 将用例执行结果写入到result_col self.excel.write_data(row=row, column=do_yaml.read("excel", "result_col"), value=fail_msg) # do_log.error("断言异常: {}".format(e)) do_log.error(f"{msg}, 执行的结果为: {fail_msg}\n具体异常为: {e}\n") raise e else: # 将相应实际值写入到actual_col列 self.excel.write_data(row=row, column=do_yaml.read("excel", "actual_col"), value=res.text) # 将用例执行结果写入到result_col self.excel.write_data(row=row, column=do_yaml.read("excel", "result_col"), value=success_msg) do_log.info(f"{msg}, 执行的结果为: {success_msg}\n")
def test_add(self, case): # 1. 参数化 new_data = Parameterize.to_param(case.data) # 2. 拼接完整的url new_url = do_yaml.read('api', 'prefix') + case.url # 3. 向服务器发起请求 res = self.do_request.send(url=new_url, data=new_data) # 将相应报文中的数据转化为字典 actual_value = res.json() row = case.case_id + 1 expect_result = case.expected msg = case.title # 获取用例标题 success_msg = do_yaml.read("msg", "success_result") # 获取用例执行成功的提示信息 fail_msg = do_yaml.read("msg", "fail_result") # 获取用例执行失败的提示信息 try: self.assertEqual(expect_result, actual_value.get('code'), msg=msg) except AssertionError as e: # 将执行结果写入到日志中 do_log.error(f"{msg}, 执行的结果为: {fail_msg}\n具体异常为: {e}\n") # 将响应数据写入excel self.do_excel.write_data(row=row, column=do_yaml.read( "excel", "actual_col"), value=res.text) # 将测试结果写入excel self.do_excel.write_data(row=row, column=do_yaml.read( "excel", "result_col"), value=fail_msg) raise e else: # 如果登录接口断言成功, 则取出token, 并添加到公共请求头中 if 'token_info' in res.text: token = actual_value['data']['token_info']['token'] headers = {"Authorization": "Bearer " + token} self.do_request.add_headers(headers) # 将执行结果写入到日志中 do_log.info(f"{msg}, 执行的结果为: {success_msg}\n") # 将响应数据写入excel self.do_excel.write_data(row=row, column=do_yaml.read( "excel", "actual_col"), value=res.text) # 将测试结果写入excel self.do_excel.write_data(row=row, column=do_yaml.read( "excel", "result_col"), value=success_msg)
def setUpClass(cls): # 构造请求参数 cls.do_request = HandleRequest() # 创建HandleRequest对象 cls.do_request.add_headers(do_yaml.get_data( "api", "api_version")) # 添加请求头到公共请求头 cls.do_mysql = HandleMysql() # 创建HandleMysql对象 # cls.do_mysql.create_not_existed_mobile() do_log.info("开始执行用例")
def test_login(self, case): # 1. 参数化 new_data = Parameterize.to_param(case.data) # 2. 拼接完整的url new_url = do_yaml.read('api', 'prefix') + case.url # 3. 向服务器发起请求 res = self.do_request.send( url=new_url, # url地址 # method=case.method, # 请求方法 data=new_data, # 请求参数 # is_json=True # 是否以json格式来传递数据, 默认为True ) # 将相应报文中的数据转化为字典 actual_value = res.json() # 获取用例的行号 row = case.case_id + 1 # 将expected期望值转化为字典 expected_result = json.loads(case.expected, encoding='utf-8') msg = case.title # 获取标题 success_msg = do_yaml.read('msg', 'success_result') # 获取用例执行成功的提示 fail_msg = do_yaml.read('msg', 'fail_result') # 获取用例执行失败的提示 try: # 先断言code, 再断言msg self.assertEqual(expected_result.get('code'), actual_value.get('code'), msg=msg) self.assertEqual(expected_result.get('msg'), actual_value.get('msg'), msg=msg) except AssertionError as e: # 将相应实际值写入到actual_col列 self.excel.write_data(row=row, column=do_yaml.read("excel", "actual_col"), value=res.text) # 将用例执行结果写入到result_col self.excel.write_data(row=row, column=do_yaml.read("excel", "result_col"), value=fail_msg) # do_log.error("断言异常: {}".format(e)) do_log.error(f"{msg}, 执行的结果为: {fail_msg}\n具体异常为: {e}\n") raise e else: # 将相应实际值写入到actual_col列 self.excel.write_data(row=row, column=do_yaml.read("excel", "actual_col"), value=res.text) # 将用例执行结果写入到result_col self.excel.write_data(row=row, column=do_yaml.read("excel", "result_col"), value=success_msg) do_log.info(f"{msg}, 执行的结果为: {success_msg}\n")
def test_login(self, case): #参数化 new_data = Parameterize.to_param(case.data) #构造请求url new_url = do_yaml.read("api", "profix") + case.url #发起请求 res = self.do_request.send(url=new_url, data=new_data) #将响应报文转化为json actual_value = res.json() # 获取测试用例行号 row = case.case_id + 1 # 获取预期结果 expected_result = json.loads(case.expected, encoding="utf8") #获取测试用例的标题 msg = case.title success_msg = do_yaml.read('msg', 'success_result') # 获取用例执行成功的提示 fail_msg = do_yaml.read('msg', 'fail_result') # 获取用例执行失败的提示 try: # 先断言code, 再断言msg self.assertEqual(expected_result.get("code"), actual_value.get("code"), msg=msg) self.assertEqual(expected_result.get("msg"), actual_value.get("msg"), msg=msg) except Exception as e: # 将用例执行结果写入到result_col self.excel.write_data(row=row, column=do_yaml.read("excel", "result_col"), value=fail_msg) # error 指的时日志的收集等级 do_log.error(f"{msg}, 执行的结果为: {fail_msg}\n具体异常为: {e}\n") raise e else: # 将用例执行结果写入到result_col self.excel.write_data(row=row, column=do_yaml.read("excel", "result_col"), value=success_msg) # info 指的时日志的收集等级 do_log.info(f"{msg}, 执行的结果为: {success_msg}\n") finally: # 将相应实际值写入到actual_col列 self.excel.write_data(row=row, column=do_yaml.read("excel", "actual_col"), value=res.text)
def test_register(self, case): """ 测试用户注册接口 :return: """ #处理获取的表数据 msg = case['title'] url = http_host + case['url'] data = eval(Context.replace_all(str(case['data']))) method = case['method'] except_result = case['expected'] # 创建一个webservice请求对象 dowebservice = DoWebService(url=url) #发起数据请求 request_res = dowebservice.webservice_requests(post=method, param=data) # 判断请求方式是否有误 if request_res == 'PostError': do_log.info('{}\t执行请求方式有误!:{}\n'.format(msg, case['method'])) # 判断是否为发送验证码接口并获取数据库验证码信息 if 'ok' in str(request_res) and case['method'].lower() == 'sendmcode': setattr(Context, "invest_mobile_num", data["mobile"]) check_sql = case['check_sql'] if check_sql: check_sql = Context.replace_all(check_sql) mysql_data = self.do_mysql.select_mysql(sql=check_sql) mcode = mysql_data['Fverify_code'] # 动态创建Context对象变量sql_mcode(验证码) setattr(Context, "sql_mcode", mcode) try: #断言请求结果 self.assertIn(except_result, str(request_res), msg=msg + '失败!') # 判断是否为用户注册接口并获取数据库用户信息 if 'ok' in str( request_res) and case['method'].lower() == 'userregister': check_sql = case['check_sql'] #动态创建Context对象变量user_id(用户名) setattr(Context, 'user_id', data['user_id']) if check_sql: check_sql = Context.replace_all(check_sql) mysql_data = self.do_mysql_user.select_mysql(sql=check_sql) if mysql_data['Fuid'] is not None: # 动态创建Context对象变量Fuid(数据库中获取的用户ID) setattr(Context, 'Fuid', mysql_data['Fuid']) #断言用户是否添加成功 self.assertIsNotNone(mysql_data, msg=msg + '数据库添加用户失败!') do_log.info('{}\t执行结果:{}\n'.format(msg, 'Pass')) do_excel.write_result(case['case_id'] + 1, str(request_res), 'Pass') except AssertionError as e: do_log.error('{}执行结果:{}\t报错信息:{}\n'.format(msg, 'Fail', e)) do_excel.write_result(case['case_id'] + 1, str(request_res), 'Fail') raise e
def test_addition(self, case): #参数化 new_data = Parameterize.to_param(case.data) #构造请求url new_url = do_yaml.read("api", "profix") + case.url #发起加标请求 res = self.do_request.send(url=new_url, data=new_data) #将相应报文中的数据转化为字典 actul_value = res.json() #获取测试用例所在的行 row = case.case_id + 1 # 获取预期结果 expected_result = case.expected #获取测试用例标题 msg = case.title success_msg = do_yaml.read('msg', 'success_result') # 获取用例执行成功的提示 fail_msg = do_yaml.read('msg', 'fail_result') # 获取用例执行失败的提示 try: self.assertEqual(expected_result, actul_value.get("code"), msg=msg) except Exception as e: # 将用例执行结果写入到result_col self.excel.write_data(row=row, column=do_yaml.read("excel", "result_col"), value=fail_msg) do_log.error(f"{msg}, 执行的结果为: {fail_msg}\n具体异常为: {e}\n") raise e else: if "token_info" in res.text: token = actul_value["data"]["token_info"]["token"] header = {"Authorization": "Bearer " + token} self.do_request.add_headers(header) # 将用例执行结果写入到result_col self.excel.write_data(row=row, column=do_yaml.read("excel", "result_col"), value=success_msg) # info 指的时日志的收集等级 do_log.info(f"{msg}, 执行的结果为: {success_msg}\n") finally: # 将相应实际值写入到actual_col列 self.excel.write_data(row=row, column=do_yaml.read("excel", "actual_col"), value=res.text)
def setUpClass(cls): # 创建HandleRequest对象 cls.do_request = HandleRequest() # 获取url版本请求头数据并添加至请求头中 cls.do_request.add_headers(do_yaml.get_data("api", "api_version")) # 创建HandleMysql对象 cls.do_mysql = HandleMysql() # 创建一个不存在的用户id,并加入到全局数据池中 setattr(GlobalData, "${not_existed_id}", cls.do_mysql.get_not_existed_user_id()) do_log.info("开始执行用例")
def test_ride(self,one_case): actual_results = Jisuan(one_case['l_data'], one_case['r_data']).ride() msg = one_case['title'] expected_results = one_case['expected'] success_msg = do_config.get_config("msg", "success_result") fail_msg = do_config.get_config("msg", "Fail_result") try: self.assertEqual(expected_results,actual_results, msg=msg) # self.rizhi.write("test_ride:{} ,测试结果为 {}\n".format(msg,success_msg)) do_log.info("test_ride:{} ,测试结果为 {}\n".format(msg, success_msg)) do_excel.write_result(one_case['case_id']+1,actual_results,success_msg) except AssertionError as e: # self.rizhi.write("test_ride:{} ,测试结果为 {}.具体异常为{}\n".format(msg,fail_msg,e)) do_log.error("test_ride:{} ,测试结果为 {}.具体异常为{}\n".format(msg,fail_msg,e)) do_excel.write_result(one_case['case_id'] + 1, actual_results, fail_msg) raise e #因为上面捕获了异常,所以这里用raise来返回异常
def test_invest(self, data_namedtuple): """ 测试投资功能 :return: """ do_log.info("\n正在运行的测试用例为:{}".format(data_namedtuple.title)) method = data_namedtuple.method url = do_config("api", "url") + data_namedtuple.url print(url) new_data = HandleContext().invest_parameterization(data_namedtuple.data) print(new_data) response = self.send_request(method=method, url=url, data=new_data) print(response.text) # 判断服务器是否有异常 try: self.assertEqual(200, response.status_code, msg="测试【{}】失败,http请求状态码为【{}】". format(data_namedtuple.title, response.status_code)) except AssertionError as e: do_log.error("\n具体异常信息为:{}\n".format(e)) raise e # 判断是否加标成功,加标成功,获取loan_id if response.json().get("msg") == "加标成功": check_sql = data_namedtuple.check_sql if check_sql: check_sql = HandleContext().invest_parameterization(check_sql) mysql_data = self.handle_mysql(sql=check_sql) # 获取一条记录 HandleContext.loan_id = mysql_data["Id"] # 动态添加属性,获取标的Id # setattr(HandleContext, "loan_id", mysql_data["Id"]) # 动态创建类属性,获取标的Id # 验证预期结果是否与实际结果相同 try: self.assertEqual(data_namedtuple.expected, response.text, msg="测试{}失败".format(data_namedtuple.title)) except AssertionError as e: do_log.error("\n具体异常信息为:{}\n".format(e)) self.do_excel.write_result(row=data_namedtuple.case_id+1, actual=response.text, result=do_config("msg", "fail_result")) raise e else: self.do_excel.write_result(row=data_namedtuple.case_id+1, actual=response.text, result=do_config("msg", "success_result"))
def test_add_event(self, one_case): base_url = do_config.get_config("address", "add_address") payload = Context().readd_parameterization_id(one_case["data"]) self.res = (self.one_session.to_request(base_url, data=payload, method='post')).json() expected_results = json.loads(one_case["expected"]) #预期结果 msg = one_case['title'] try: self.assertEqual(expected_results, self.res) do_excel.write_result(one_case['case_id'] + 1, self.res['message'], "Pass") do_log.info("test_add_event:{} ,测试结果为 {}".format(msg, "Pass")) except AssertionError as e: do_excel.write_result(one_case['case_id'] + 1, self.res['message'], "Fail") do_log.info("test_add_event:{} ,测试结果为 {}".format(msg, "Fail")) raise e
def test_add(self, data_namedtuple): """ 测试加标功能 :return: """ url = do_config("api", "url") + data_namedtuple.url new_data = HandleContext().add_parameterization(data_namedtuple.data) print(new_data) do_log.info("\n正在运行的测试用例为:{}".format(data_namedtuple.title)) # 接口不支持json格式,只支持form表单,is_json=False, 使用data response = self.send_request(method=data_namedtuple.method, url=url, data=new_data) print(response.text) # 判断服务器是否有异常 try: self.assertEqual(200, response.status_code, msg="测试【{}】失败,请求状态码为【{}】" .format(data_namedtuple.title, response.status_code)) except AssertionError as e : # 出现异常,后面代码不会执行 do_log.error("{},执行结果:{}\n具体异常信息:{}\n".format(data_namedtuple.title, "fail", e)) raise e try: self.assertEqual(data_namedtuple.expected, response.text, msg="测试{}失败".format(data_namedtuple.title)) except AssertionError as e: do_log.error("\n具体异常信息为:{}\n".format(e)) self.do_excel.write_result(row=data_namedtuple.case_id+1, actual=response.text, result=do_config("msg", "fail_result")) raise e else: self.do_excel.write_result(row=data_namedtuple.case_id+1, actual=response.text, result=do_config("msg", "success_result"))
def test_register(self, case): # 参数化 new_data = Parameterize.to_param(case.data) # 构造url new_url = do_yaml.read("api", "profix") + case.url # 向服务器发起请求 res = self.do_request.send( url=new_url, # url地址 # method=case.method, # 请求方法 data=new_data # 请求参数 # is_json=True # 是否以json格式来传递数据, 默认为True ) #将响应报文转化为json actual_value = res.json() # 获取测试用例行号 row = case.case_id + 1 #获取预期结果 expected_result = case.expected # 获取测试用例的标题 msg = case.title #获取用例成功与失败的提示 success_msg = do_yaml.read('msg', 'success_result') fail_msg = do_yaml.read('msg', 'fail_result') try: self.assertEqual(expected_result, actual_value.get('code'), msg=msg) except Exception as e: # # # 将相应实际值写入到actual_col列 # self.excel.write_data(row = row, # column = do_yaml.read("excel","actual_col"), # value = res.text) # 将用例执行结果写入到result_col self.excel.write_data(row=row, column=do_yaml.read("excel", "result_col"), value=fail_msg) # error 指的时日志的收集等级 do_log.error(f"{msg}, 执行的结果为: {fail_msg}\n具体异常为: {e}\n") raise e else: # # # 将相应实际值写入到actual_col列 # self.excel.write_data(row = row, # column = do_yaml.read("excel","actual_col"), # value = res.text) # 将用例执行结果写入到result_col self.excel.write_data(row=row, column=do_yaml.read("excel", "result_col"), value=success_msg) # info 指的时日志的收集等级 do_log.info(f"{msg}, 执行的结果为: {success_msg}\n") finally: # 将相应实际值写入到actual_col列 self.excel.write_data(row=row, column=do_yaml.read("excel", "actual_col"), value=res.text)
def tearDownClass(cls): cls.do_request.close() do_log.info("用例执行结束")
def setUpClass(cls): # 3、构造请求参数 cls.do_request = HandleRequest() cls.do_request.add_headers(do_yaml.get_data("api", "api_version")) do_log.info("开始执行用例")
def test_recharge(self, case): # 参数化 new_data = Parameterize.to_param(case.data) # 构造请求url new_url = do_yaml.read("api", "profix") + case.url check_sql = case.check_sql # 判断check_sql字段是否为空(非空即为True) if check_sql: # 如果check_sql不为空, 则代表当前用例需要进行数据校验 #参数化check_sql check_sql = Parameterize.to_param(case.check_sql) #执行sql获取充值前金额 mysql_data = self.do_mysql.run(check_sql) # 不是float类型, 也不是int类型, 是decimal类型 amount_before = float(mysql_data["leave_amount"]) # 由于使用float转化之后的数, 有可能小数位数超过2位, 需要使用round保留2位小数 amount_before = round(amount_before, 2) #发起请求 res = self.do_request.send(url=new_url, data=new_data) # 将相应报文中的数据转化为字典 actual_value = res.json() #获取测试用例的行 row = case.case_id + 1 #获取预期结果 expected_result = case.expected # 获取测试用例的标题 msg = case.title success_msg = do_yaml.read('msg', 'success_result') # 获取用例执行成功的提示 fail_msg = do_yaml.read('msg', 'fail_result') # 获取用例执行失败的提示 try: self.assertEqual(actual_value.get("code"), expected_result, msg=msg) if check_sql: check_sql = Parameterize.to_param(check_sql) mysql_data = self.do_mysql.run(sql=check_sql) amount_after = float(mysql_data["leave_amount"]) amount_after = round(amount_after, 2) #从data中取出充值金额 data = json.loads(new_data, encoding="utf8") # 充值金额 current_recharge_amount = data["amount"] #充值后金额 actual_amount = round(amount_before + current_recharge_amount, 2) self.assertEqual(amount_after, actual_amount, msg="数据库中的金额有误") except Exception as e: # 将用例执行结果写入到result_col self.excel.write_data(row=row, column=do_yaml.read("excel", "result_col"), value=fail_msg) # error 指的时日志的收集等级 do_log.error(f"{msg}, 执行的结果为: {fail_msg}\n具体异常为: {e}\n") raise e else: if "token_info" in res.text: token = actual_value["data"]["token_info"]["token"] header = {"Authorization": "Bearer " + token} self.do_request.add_headers(header) # 将用例执行结果写入到result_col self.excel.write_data(row=row, column=do_yaml.read("excel", "result_col"), value=success_msg) # info 指的时日志的收集等级 do_log.info(f"{msg}, 执行的结果为: {success_msg}\n") finally: # 将相应实际值写入到actual_col列 self.excel.write_data(row=row, column=do_yaml.read("excel", "actual_col"), value=res.text)
def tearDownClass(cls): cls.one_session.close() do_log.info("{:=^40s}".format("用例执行结束"))
def setUpClass(cls): cls.one_session = HandleRequest() do_log.info("{:=^40s}".format("用例开始执行"))
def test_recharge(self, case): # 回写结果的row row = case.case_id + 1 # 从excel中读取数据,准备请求参数 # 拼接完整的url new_url = do_yaml.read_yaml('api', 'prefix') + case.url # 参数化获取发起请求的用例数据 new_data = Parameterize.to_parameter(case.data) # 获取充值前的账户金额 # 根据excel中sql语句是否存在来判断是否获取充值前金额进行数据校验 if case.check_sql: # 将sql语句参数化 sql = Parameterize.to_parameter(case.check_sql) # 执行查询语句,在数据库中查询得到数据字典 res_dict = self.do_mysql.run(sql, is_more=False) # 取得充值前账户金额,转换为float # global amount_before amount_before = float(res_dict['leave_amount']) # 保留2位小数 amount_before = round(amount_before, 2) # 对象调用类属性向服务器发起请求 res = self.do_request.send(new_url, data=new_data) # 将响应报文中的json格式数据(响应体)转换为字典 actual_value = res.json() # # 获取token(也可以在发送请求后通过case_id 来判断登录用例从而获取token) # if case.case_id == 2: # token_headers = {'Authorization': 'Bearer ' + actual_value['data']['token_info']['token']} # self.do_request.add_headers(token_headers) # 从excel中获取excepted excepted_result = case.excepted # 获取title msg = case.title # 从配置文件中读取断言结果:成功 success_result = do_yaml.read_yaml('excel', 'success_result') # 从配置文件中读取断言结果:失败 fail_result = do_yaml.read_yaml('excel', 'fail_result') # 比较预期和实际结果 # 捕获异常,回写结果,打印日志,并主动抛出异常 try: self.assertEqual(actual_value.get('code'), excepted_result, msg=msg) # 如果充值成功,则获取充值成功后的金额(反向用例没有做数据校验) if case.check_sql: # # 将sql语句参数化 # sql = Parameterize.to_parameter(case.check_sql) # 在数据库中查询得到数据字典 res_dict = self.do_mysql.run(sql, is_more=False) # 取得充值后账户金额,取2位 amount_after = round(float(res_dict['leave_amount']), 2) # 进行断言 amount_real = json.loads(new_data) self.assertEqual(round((amount_after - amount_before), 2), amount_real.get('amount')) except AssertionError as e: # 回写断言结果 self.excel.write_result(row, do_yaml.read_yaml('excel', 'result_col'), fail_result) # 将异常内容写入日志 # do_log.error(f'{msg},执行结果为:{fail_result},具体异常为:{e}\n') do_log.error({e}) # 主动抛出异常 raise e else: # 获取token(也可以在发送请求后通过case_id 来判断登录用例从而获取token) if actual_value.get('data'): if actual_value.get('data').get('token_info'): token_headers = {'Authorization': 'Bearer ' + actual_value['data']['token_info']['token']} self.do_request.add_headers(token_headers) # 回写断言结果 self.excel.write_result(row, do_yaml.read_yaml('excel', 'result_col'), success_result) # 打印日志 do_log.info(f'{msg},执行结果为:{success_result}') finally: # 将返回的响应体写入excel self.excel.write_result(row, do_yaml.read_yaml('excel', 'actual_col'), res.text)
def tearDownClass(cls): # cls.rizhi.write("{:=^40s}\n".format("测试用例执行结束")) # cls.rizhi.close() do_log.info("\n{:=^40s}".format("测试用例开始执行"))
def tearDownClass(cls): # 释放资源 cls.do_request.close() cls.do_mysql.close() do_log.info("用例执行结束")
def setUpClass(cls): # wenjian = do_config.get_config("file path", "log_path") # cls.rizhi = open(wenjian,"a",encoding="utf8") # cls.rizhi.write("\n{:=^40s}\n".format("测试用例开始执行")) do_log.info("\n{:=^40s}".format("测试用例开始执行"))