def test_recharge(self,caseId,method,params,expectedResult,sql,**kwargs): register_tel = DoExcel('Tel').read_one_data(3, 2) # 从excel中将已注册的手机号及密码取出来 register_pwd = DoExcel('Tel').read_one_data(3, 3) cookie = HttpRequest().send_request(url=login_url, method='get',params={'mobilephone': register_tel, 'pwd': register_pwd}).cookies # 使用已注册的手机号调取登录接口并获取cookie setattr(CommonData, 'cookie', cookie) # 将cookie存入全局变量 if params.find('${mobile}') != -1: # 如果在参数中发现mobile变量,则用已注册手机号替换 params = params.replace('${mobile}',str(register_tel)) self.my_logger.info_log('第{}条充值用例正在执行中,请求参数为{}'.format(caseId,params)) actual_result = HttpRequest().send_request(url=url,method=method, params=eval(params),cookies=CommonData.cookie).json() # 发送请求并获取响应正文 if sql is not None: # 如果有sql的话,就去查询sql并将结果写入全局变量中 sql = sql.replace('${mobile}',str(register_tel)) leave_amount = self.db.get_one_result(sql)[0] setattr(CommonData, 'end_leave_amount', leave_amount) self.my_logger.debug_log('充值后的金额为{}'.format(leave_amount)) if expectedResult.find('${leaveamount}') != -1: # 如果期望结果中有参数的话,用查询结果代替 expectedResult = expectedResult.replace('${leaveamount}',str(leave_amount)) else: actual_result.pop('data') # 如果没有需要替代的数据,则把data去掉不进行比较 expectedResult = eval(expectedResult) # 将期望结果转换为字典格式,以便于比较 try: self.assertDictEqual(actual_result,expectedResult) test_result = 'Pass' except Exception as e: test_result = 'Fail' self.my_logger.error_log('期望结果与实际结果不一致,错误为{}'.format(e)) raise e finally: actual_result = json.dumps(actual_result, ensure_ascii=False) # 将响应转换为字符串格式 wb.update_excel(int(caseId) + 1, 7, actual_result) wb.update_excel(int(caseId) + 1, 8, test_result) # 将结果写回excel中
def test_bidloan(self, caseId, method, params, expectedResult, sql, **kwargs): register_tel = DoExcel('Tel').read_one_data(3, 2) register_pwd = DoExcel('Tel').read_one_data(3, 3) # register_memberid = DoExcel('Tel').read_one_data(3,4) # 从excel中读出已注册的手机号和密码作为投资人 cookie = HttpRequest().send_request(url=login_url, method='get', params={ 'mobilephone': register_tel, 'pwd': register_pwd }).cookies setattr(CommonData, 'cookie', cookie) # 将cookie存入全局变量 if sql is not None and sql.find( '${mobile}') != -1: # 如果sql存在的话,就去查数据库并进行替换 sql = eval(sql) # 将sql从字符串还原到字典格式 loanid = self.db.get_one_result( sql['sql_1'])[0] # 字典中sql_1对应的是loanid的查询语句 sql_2 = sql['sql_2'].replace( '${mobile}', str(register_tel)) # 先对sql_2的手机号进行替换,再去查询 memberid = self.db.get_one_result(sql_2)[0] # 获取当前登录账号的memberId before_amount = self.db.get_one_result(sql_2)[1] # 获取当前登录账号的起始余额 params = params.replace('${loanId}',str(loanid))\ .replace('${memberId}',str(memberid)).replace('${pwd}',register_pwd) # 将请求参数中的变量进行替换 actual_result = HttpRequest().send_request(url=url, method=method, params=eval(params), cookies=cookie).json() expected_amount = before_amount - eval(params)['amount'] after_amount = self.db.get_one_result(sql_2)[1] try: self.assertEqual(after_amount, expected_amount) self.my_logger.debug_log( '投资前金额为{},投资后期望金额为{},投资后实际金额为{}'.format( before_amount, expected_amount, after_amount)) except Exception as e: self.my_logger.error_log( '充值后,期望余额和数据库中余额不一致,期望余额为{},实际余额为{},错误为{}'.format( expected_amount, after_amount, e)) raise e else: actual_result = HttpRequest().send_request(url=url, method=method, params=eval(params), cookies=cookie).json() actual_result.pop('data') try: self.assertDictEqual(actual_result, eval(expectedResult)) self.my_logger.info_log('实际结果与期望结果一致') test_result = 'Pass' except Exception as e: self.my_logger.error_log('实际结果与期望结果不一致,错误为{}'.format(e)) test_result = 'Fail' raise e finally: actual_result = json.dumps(actual_result, ensure_ascii=False) wb.update_excel(int(caseId) + 1, 7, actual_result) wb.update_excel(int(caseId) + 1, 8, test_result)
def test_register(self, caseId, method, params, expectedResult, sql, **kwargs): if params.find( '${mobile}') != -1: # 如果语句中发现变量,则用Tel表格中电话号码去替换sql和params new_tel = DoExcel('Tel').read_one_data(2, 2) while True: # 进行循环查询,直至查询到未注册手机号 new_sql = sql.replace('${mobile}', str(new_tel)) res = self.db.get_one_result( new_sql) # 替换完成后去执行sql,查询此号码是否已被注册 if res is None: # 当结果为None时,表示电话号码未被注册 params = params.replace('${mobile}', str(new_tel)) # 未被注册时就将手机号替换到参数中 DoExcel('Tel').update_excel(2, 2, new_tel + 1) # 同时将数据加1写回文件中并跳出循环 break else: new_tel += 1 # 当查询到数据已经被注册后,就将数据加1并继续循环 self.my_logger.info_log('正在执行第{}条注册用例,参数为{}'.format(caseId, params)) actual_result = HttpRequest().send_request( url=url, method=method, params=eval(params)).json() # 去发起http请求 actual_result.pop('data') # 注册用例中,data数据无意义,可以不进行比较 try: self.assertDictEqual(actual_result, eval(expectedResult)) # 将实际结果与期望结果进行对比 if sql is not None: # 断言成功后检查是否有sql需要查询 res = self.db.get_one_result( sql.replace('${mobile}', str(new_tel))) # 查询数据库,此手机号是否已注册 try: self.assertEqual(len(res), 1) # 比较是否已查询到此注册账号 test_result = 'Pass' # 查询到结果为Pass self.my_logger.info_log('{}已注册并保存到数据库中'.format(new_tel)) except Exception as e: test_result = 'Fail' # 未查询到结果为Fail self.my_logger.error_log('接口返回正确,但数据未插入数据库中') raise e else: test_result = 'Pass' # 不需要查询数据库时,结果直接为Pass except Exception as e: # 断言比较错误时,直接不进行数据库查询,直接Fail test_result = 'Fail' self.my_logger.error_log('接口返回不一致,错误为{}'.format(e)) raise e finally: actual_result = json.dumps(actual_result, ensure_ascii=False) # 将响应转换为字符串格式 wb.update_excel(int(caseId) + 1, 7, actual_result) wb.update_excel(int(caseId) + 1, 8, test_result) # 将结果写回excel中
def test_login(self,caseId,method,params,expectedResult,**kwargs): if params.find('${mobile}') != -1: register_tel = DoExcel('Tel').read_one_data(3,2) register_pwd = DoExcel('Tel').read_one_data(3,3) params = params.replace('${mobile}',str(register_tel)).replace('${pwd}',register_pwd) self.my_logger.info_log('正在执行第{}条用例,参数为{}'.format(caseId,params)) actual_result = HttpRequest().send_request(url=url,params=eval(params),method=method).json() actual_result.pop('data') try: self.assertDictEqual(actual_result,eval(expectedResult)) test_result = 'Pass' except Exception as e: self.my_logger.error_log('实际结果与期望结果一致,错误为{}'.format(e)) test_result = 'Fail' raise e finally: actual_result = json.dumps(actual_result, ensure_ascii=False) # 将响应转换为字符串格式 wb.update_excel(int(caseId) + 1, 7, actual_result) wb.update_excel(int(caseId) + 1, 8, test_result) # 将结果写回excel中
# -*- coding: utf-8 -*- # @Time : 2019/3/26 10:32 # @Author: Tansy_Xiaoming # @Email : [email protected] # @File : bidloan_case.py import unittest from ddt import ddt, data, unpack import json from class_0313_api_practice_4.common.do_excel import DoExcel from class_0313_api_practice_4.common.my_log import MyLog from class_0313_api_practice_4.common.http_request import HttpRequest from class_0313_api_practice_4.common.check_database import CheckDB from class_0313_api_practice_4.common.read_config import ReadConfig from class_0313_api_practice_4.common.common_data import CommonData wb = DoExcel('BidLoan') test_data = wb.read_all_data() url = ReadConfig().get_str('TestData', 'URL') + '/member/bidLoan' login_url = ReadConfig().get_str('TestData', 'URL') + '/member/login' @ddt class BidLoanCase(unittest.TestCase): '''定义投资的用例实现''' def setUp(self): self.my_logger = MyLog() self.db = CheckDB(session='DBConnect_QA') self.my_logger.info_log('---------投资用例开始执行----------') def tearDown(self): self.my_logger.info_log('---------投资用例结束执行-----------')
# -*- coding: utf-8 -*- # @Time : 2019/3/25 9:41 # @Author: Tansy_Xiaoming # @Email : [email protected] # @File : login_case.py import unittest from ddt import ddt,data,unpack import json # from class_0313_api_practice_4.common import * from class_0313_api_practice_4.common.do_excel import DoExcel from class_0313_api_practice_4.common.my_log import MyLog from class_0313_api_practice_4.common.http_request import HttpRequest from class_0313_api_practice_4.common.check_database import CheckDB from class_0313_api_practice_4.common.read_config import ReadConfig wb = DoExcel('Login') test_data = wb.read_all_data() url = ReadConfig().get_str('TestData','URL') + '/member/login' @ddt class LoginCase(unittest.TestCase): '''定义登录用例的逻辑''' def setUp(self): self.my_logger = MyLog() self.my_logger.info_log('-------登录用例开始执行了-------') print('-----------登录用例开始执行了-----------') def tearDown(self): # self.my_logger = MyLog() self.my_logger.info_log('--------登录用例执行结束了--------')
# -*- coding: utf-8 -*- # @Time : 2019/3/25 15:10 # @Author: Tansy_Xiaoming # @Email : [email protected] # @File : recharge_case.py import unittest from ddt import ddt,data,unpack import json from class_0313_api_practice_4.common.do_excel import DoExcel from class_0313_api_practice_4.common.my_log import MyLog from class_0313_api_practice_4.common.http_request import HttpRequest from class_0313_api_practice_4.common.check_database import CheckDB from class_0313_api_practice_4.common.read_config import ReadConfig from class_0313_api_practice_4.common.common_data import CommonData wb = DoExcel('Recharge') test_data = wb.read_all_data() url = ReadConfig().get_str('TestData','URL') + '/member/recharge' login_url = ReadConfig().get_str('TestData','URL') + '/member/login' @ddt class RechargeCase(unittest.TestCase): '''实现充值的用例逻辑''' def setUp(self): self.my_logger = MyLog() self.db = CheckDB(session='DBConnect_QA') self.my_logger.info_log('---------充值用例开始执行----------') def tearDown(self): self.my_logger.info_log('-----------充值用例执行结束---------')
# -*- coding: utf-8 -*- # @Time : 2019/3/23 16:33 # @Author: Tansy_Xiaoming # @Email : [email protected] # @File : register_case.py import unittest from ddt import ddt, data, unpack import json from class_0313_api_practice_4.common.do_excel import DoExcel from class_0313_api_practice_4.common.my_log import MyLog from class_0313_api_practice_4.common.http_request import HttpRequest from class_0313_api_practice_4.common.check_database import CheckDB from class_0313_api_practice_4.common.read_config import ReadConfig wb = DoExcel('Register') # 实例化一个excel对象 test_data = wb.read_all_data() # 读取注册的需要运行的全部用例 url = ReadConfig().get_str( 'TestData', 'URL') + '/member/register' # 根绝配置文件定义的URL去调对应地址的注册接口 @ddt class RegisterCase(unittest.TestCase): '''定义注册用例的逻辑''' def setUp(self): print('---------注册用例开始执行了----------') self.my_logger = MyLog() self.db = CheckDB(session='DBConnect_QA') def tearDown(self): print('----------注册用例执行结束了----------')
result = requests.get(url, params=params, **kwargs) elif method.lower() == 'post': # 如果是post请求,就调用post方法 try: result = requests.post(url, data=params, **kwargs) except Exception as e: print('传入请求参数类型不对,请求参数类型为{}'.format(type(params))) result = None else: print('请输入正确的请求方法') return result if __name__ == '__main__': # res = HttpRequest().send_request(url='http://47.107.168.87:8080/futureloan/mvc/api/member/register', method='post', # params={'mobilephone':'15168392262','pwd':'123456'}) register_tel = DoExcel('Tel').read_one_data(3, 2) register_pwd = DoExcel('Tel').read_one_data(3, 3) print(register_tel) params = {'mobilephone': register_tel, 'pwd': register_pwd} print(params) res = HttpRequest().send_request( url='http://47.107.168.87:8080/futureloan/mvc/api/member/login', method='post', params={ 'mobilephone': register_tel, 'pwd': register_pwd }) cookies = res.cookies # print(cookies) # params = {'mobilephone':'14168392262','amount':'123456'} # print(type(params))