class Login(unittest.TestCase): excel = Excel_Hande(os.path.join(DATA_DIR, 'apicases.xlsx'), 'login') case = excel.read_excel() @data(*case) def test_login(self, cas): url = cas['url'] method = cas['method'] headers = eval(conf.get('env', 'headers')) data = eval(cas['data']) expected = eval(cas['expected']) row = cas['case_id'] + 1 respons = request(url=url, method=method, json=data, headers=headers) res = respons.json() print('实际结果:', res) print('预期结果:', expected) try: self.assertEqual(expected['code'], res['code']) self.assertEqual(expected['msg'], res['msg']) except AssertionError as e: log.error('用例{}没有通过,错误等级为error'.format(cas['title'])) log.debug('预期结果{}'.format(cas['expected'])) log.debug('实际结果{}'.format(res)) log.exception(e) self.excel.write(row=row, column=8, value='未通过') raise e else: log.info('用例{}执行通过'.format(cas['title'])) self.excel.write(row=row, column=8, value='通过')
class Loans(unittest.TestCase): execl = Excel_Hande(os.path.join(DATA_DIR,'apicases.xlsx'),'loans') case = execl.read_excel() @data(*case) def test_loans(self,cas): url = conf.get('env','url') + cas['url'] data = eval(cas['data']) headers = eval(conf.get('env','headers')) method = cas['method'] expected = eval(cas['expected']) row = cas['case_id'] + 1 respones = request(url=url,method=method,params=data,headers=headers) res = respones.json() try: self.assertEqual(expected['code'],res['code']) self.assertEqual(expected['msg'],res['msg']) #判断返回的数据条数 self.assertEqual(expected['len'],len(res['data'])) except AssertionError as e: log.error('用例--{}--未通过'.format(cas['title'])) log.debug('预期结果{}'.format(expected)) log.debug('实际结果{}:'.format(res)) log.exception(e) self.execl.write(row=row, column=8, value='不通过') raise e else: log.info('用例--{}--通过'.format(cas['title'])) self.execl.write(row=row, column=8, value='通过')
class Add(unittest.TestCase): exce = Excel_Hande(os.path.join(DATA_DIR, 'apicases.xlsx'), 'add') case = exce.read_excel() bb = HandleMysql() @classmethod def setUpClass(cls) -> None: url = conf.get('env', 'url') + '/member/login' data = { 'mobile_phone': conf.get('test_data', 'phone'), 'pwd': conf.get('test_data', 'pwd') } headers = eval(conf.get('env', 'headers')) respones = request(url=url, method='post', json=data, headers=headers) res = respones.json() token = 'Bearer' + ' ' + jsonpath.jsonpath(res, '$..token')[0] member_id = str(jsonpath.jsonpath(res, '$..id')[0]) setattr(EnvData, 'token', token) setattr(EnvData, 'member_id', member_id) @data(*case) def test_add(self, cas): url = conf.get('env', 'url') + cas['url'] method = cas['method'] cas['data'] = replace_data(cas['data']) data = eval(cas['data']) headers = eval(conf.get('env', 'headers')) headers['Authorization'] = getattr(EnvData, 'token') expected = eval(cas['expected']) row = cas['case_id'] + 1 #查询添加项目之前的标的数量 if cas['check_sql']: sql = replace_data(cas['check_sql']) f = self.bb.find_count(sql) respones = request(url=url, method=method, json=data, headers=headers) res = respones.json() print('预期结果:', expected) print('实际结果:', res) try: self.assertEqual(expected['code'], res['code']) self.assertEqual(expected['msg'], res['msg']) #查询加标后标的数量 if cas['check_sql']: sql = replace_data(cas['check_sql']) q = self.bb.find_count(sql) self.assertEqual(1, q - f) except AssertionError as e: log.error('该用例{}错误'.format(cas['title'])) log.exception(e) self.exce.write(row=row, column=8, value='不通过') raise e else: log.info('该用例{}通过'.format('通过')) self.exce.write(row=row, column=8, value='通过')
class Withdraw(unittest.TestCase): exce = Excel_Hande(os.path.join(DATA_DIR,'apicases.xlsx'),'withdraw') case = exce.read_excel() bb = HandleMysql() @classmethod def setUpClass(cls) -> None: url = conf.get('env','url') + '/member/login' data = { 'mobile_phone':conf.get('test_data','phone'), 'pwd':conf.get('test_data','pwd') } headers = eval(conf.get('env','headers')) respones = request(url=url,method='post',json=data,headers=headers) re = respones.json() token = 'Bearer' + " " + jsonpath.jsonpath(re,'$..token')[0] member_id = str(jsonpath.jsonpath(re,'$..id')[0]) setattr(EnvData,'token',token) setattr(EnvData,'member_id',member_id) @data(*case) def test_withdraw(self,cas): url = conf.get('env','url') + cas['url'] method = cas['method'] #替换用例中中的#member_id# cas['data'] = replace_data(cas['data']) data = eval(cas['data']) headers = eval(conf.get('env','headers')) headers['Authorization'] = getattr(EnvData,'token') expected = eval(cas['expected']) row = cas['case_id'] + 1 #查询取现之前的金额 if cas['check_sql']: sql = replace_data(cas['check_sql']) money_start = self.bb.find_one(sql)['leave_amount'] respons = request(url=url,method=method,json=data,headers=headers) res = respons.json() try: self.assertEqual(expected['code'],res['code']) self.assertEqual(expected['msg'],res['msg']) #查询取现之后账户的金额 if cas['check_sql']: sql = replace_data(cas['check_sql']) money_end = self.bb.find_one(sql)['leave_amount'] #将用例数据的金额数值类型转换为decimal f = decimal.Decimal(str(data['amount'])) self.assertEqual(f,money_start-money_end) except AssertionError as e: log.error('该用例{}未通过'.format(cas['title'])) log.exception(e) self.exce.write(row=row,column=8,value='不通过') raise e else: log.info('该用例{}通过'.format(cas['title'])) self.exce.write(row=row,column=8,value='不通过')
class Register(unittest.TestCase): exce = Excel_Hande(os.path.join(DATA_DIR,'case.xlsx'),'register') case = exce.read_excel() bb = HandleMysql() @data def test_register(self,cas): url = conf.get('env','url2') + '/user/register/' method = cas['method'] expected = eval(cas['expected']) if '#username#' in cas['data']: username = self.usernam_data() setattr(EnvData,'username',username) cas['data'] = replace_data(cas['data']) if '#email#' in cas['data']: email = self.email_data() setattr(EnvData,'email',email) cas['data'] = replace_data(cas['data']) data = eval(cas['data']) respones = request(url=url,method=method,json=data) res = respones.json() #判断用户名是否注册 url3 = 'http://api.keyou.site:8000/keyou1/count/' try: self.assertEqual(res[ ]) @classmethod def usernam_data(cls): while True: name = 'huihui' for i in range(5): r = random.randint(0, 9) name += str(r) sql = 'select * from test.auth_user where username={}'.format(name) qs = cls.bb.find_count(sql) if qs == 0: return name @classmethod def email_data(cls): while True: n = '1' for i in range(8): r = random.randint(0, 9) n += str(r) sql = 'select * from test.auth_user where email={}@qq.com'.format(n) qs = cls.bb.find_count(sql) if qs == 0: return n
class UpdateCase(unittest.TestCase): excel = Excel_Hande(os.path.join(DATA_DIR, 'apicases.xlsx'), 'nickname') case = excel.read_excel() bb = HandleMysql() @classmethod def setUpClass(cls): # 登录 url = conf.get('env', 'url') + '/member/login' data = {'mobile_phone': '13367899876', 'pwd': 'lemonban'} headers = eval(conf.get('env', 'headers')) r = request(url=url, method='post', json=data, headers=headers) re = r.json() cls.token = 'Bearer' + ' ' + jsonpath.jsonpath(re, '$..token')[0] cls.member_id = jsonpath.jsonpath(re, '$..id')[0] cls.reg_name = jsonpath.jsonpath(re, '$..reg_name')[0] print('登陆时的昵称', cls.reg_name) @data(*case) def test_update(self, cas): url = conf.get('env', 'url') + cas['url'] cas['data'] = cas['data'].replace('#member_id#', str(self.member_id)) data = eval(cas['data']) headers = eval(conf.get('env', 'headers')) headers['Authorization'] = self.token method = cas['method'] expected = eval(cas['expected']) row = cas['case_id'] + 1 response = request(method=method, url=url, json=data, headers=headers) res = response.json() print('预期结果:', expected) print('实际结果:', res) try: self.assertEqual(expected['code'], res['code']) self.assertEqual(expected['msg'], res['msg']) if cas['check_sql']: sql = cas['check_sql'].replace('#member_id#', str(self.member_id)) q = self.bb.find_one(sql)['reg_name'] print('更新后昵称', q) self.assertEqual(q, expected['reg_name']) except AssertionError as e: log.error('用例--{}--未通过'.format(cas['title'])) log.debug('预期结果{}'.format(expected)) log.debug('实际结果{}:'.format(res)) log.exception(e) self.excel.write(row=row, column=8, value='不通过') raise e else: log.info('用例--{}--通过'.format(cas['title'])) self.excel.write(row=row, column=8, value='通过')
class RegisterTestcase(unittest.TestCase): excel = Excel_Hande(filename, 'register') case_data = excel.read_excel() bb = HandleMysql() @data(*case_data) def test_register(self, cas): expected = eval(cas['expected']) method = cas['method'] # 用生成的随机手机号码替换#phon# if '#phon#' in cas['data']: phones = self.readom_phon() cas['data'] = cas['data'].replace('#phon#', phones) data = eval(cas['data']) url = cas['url'] headers = eval(conf.get('env', 'headers')) row = cas['case_id'] + 1 respons = request(method=method, url=url, json=data, headers=headers) res = respons.json() print('预期结果:', expected) print('实际结果:', res) try: self.assertEqual(expected['code'], res['code']) self.assertEqual(expected['msg'], res['msg']) if cas['sql']: qy = cas['sql'].replace('#phon#', data['mobile_phone']) re = self.bb.find_one(qy) self.assertTrue(re) except AssertionError as e: log.error('用例--{}--未通过'.format(cas['title'])) log.debug('预期结果{}'.format(expected)) log.debug('实际结果{}:'.format(res)) log.exception(e) self.excel.write(row=row, column=8, value='不通过') raise e else: log.info('用例--{}--通过'.format(cas['title'])) self.excel.write(row=row, column=8, value='通过') @classmethod def readom_phon(cls): while True: phon = '155' for i in range(8): re = random.randint(0, 9) phon += str(re) mysql = 'select * from futureloan.member where mobile_phone={}'.format( phon) sq = cls.bb.find_count(mysql) if sq == 0: return phon
class Update(unittest.TestCase): exce = Excel_Hande(os.path.join(DATA_DIR, 'apicases.xlsx'), 'update') case = exce.read_excel() bb = HandleMysql() @classmethod def setUpClass(cls) -> None: url = conf.get('env', 'url') + '/member/login' data = { 'mobile_phone': conf.get('test_data', 'phone'), 'pwd': conf.get('test_data', 'pwd') } headers = eval(conf.get('env', 'headers')) respones2 = request(url=url, method='post', json=data, headers=headers) res2 = respones2.json() token = 'Bearer' ' ' + jsonpath.jsonpath(res2, '$..token')[0] member_id = str(jsonpath.jsonpath(res2, '$..id')[0]) setattr(EnvData, 'token', token) setattr(EnvData, 'member_id', member_id) @data(*case) def test_update(self, cas): url = conf.get('env', 'url') + cas['url'] method = cas['method'] cas['data'] = replace_data(cas['data']) data = eval(cas['data']) headers = eval(conf.get('env', 'headers')) headers['Authorization'] = getattr(EnvData, 'token') expected = eval(cas['expected']) row = cas['case_id'] + 1 respones = request(url=url, method=method, json=data, headers=headers) res = respones.json() try: self.assertEqual(expected['code'], res['code']) self.assertEqual(expected['msg'], res['msg']) #判断数据库中是否更新昵称 if cas['check_sql']: sql = replace_data(cas['check_sql']) s = self.bb.find_one(sql)['reg_name'] self.assertEqual(data['reg_name'], s) except AssertionError as e: log.error('该用例{}报错'.format(cas['title'])) log.exception(e) self.exce.write(row=row, column=8, value='不通过') raise e else: log.info('该用例{}通过'.format(cas['title'])) self.exce.write(row=row, column=8, value='通过')
class Register(unittest.TestCase): exce = Excel_Hande(os.path.join(DATA_DIR, 'apicases.xlsx'), 'register') case = exce.read_excel() my = HandleMysql() @data(*case) def test_register(self, cas): url = cas['url'] method = cas['method'] if '#phone#' in cas['data']: phon = self.phone_replace() setattr(EnvData, 'mobile_phone', phon) cas['data'] = replace_data(cas['data']) data = eval(cas['data']) headers = eval(conf.get('env', 'headers')) expected = eval(cas['expected']) row = cas['case_id'] + 1 respones = request(url=url, method=method, json=data, headers=headers) res = respones.json() try: self.assertEqual(expected['code'], res['code']) self.assertEqual(expected['msg'], res['msg']) if cas['check_sql']: sql = replace_data(cas['check_sql']) f = self.my.find_count(sql) self.assertEqual(1, f) except AssertionError as e: log.error('这条用例{}报错'.format(cas['title'])) log.exception(e) self.exce.write(row=row, column=8, value='不通过') raise e else: log.info('这条用例{}通过了'.format(cas['title'])) self.exce.write(row=row, column=8, value='通过') @classmethod def phone_replace(cls): while True: phone = '135' for i in range(8): re = random.randint(0, 9) phone += str(re) sql = 'select * from futureloan.member where mobile_phone={}'.format( phone) d = cls.my.find_count(sql) if d == 0: return phone
class Loans(unittest.TestCase): execl = Excel_Hande(os.path.join(DATA_DIR, 'apicases.xlsx'), 'info') case = execl.read_excel() @classmethod def setUpClass(cls) -> None: #普通用户登录 url = conf.get('env', 'url') + '/member/login' data = { 'mobile_phone': conf.get('test_data', 'phone'), 'pwd': conf.get('test_data', 'pwd') } headers = eval(conf.get('env', 'headers')) r = request(url=url, method='post', json=data, headers=headers) res = r.json() token = 'Bearer' + ' ' + jsonpath.jsonpath(res, '$..token')[0] member_id = str(jsonpath.jsonpath(res, '$..id')[0]) setattr(EnvData, 'token', token) setattr(EnvData, 'member_id', member_id) @data(*case) def test_loans(self, cas): cas['url'] = replace_data(cas['url']) url = conf.get('env', 'url') + cas['url'] headers = eval(conf.get('env', 'headers')) headers["Authorization"] = getattr(EnvData, "token") method = cas['method'] expected = eval(cas['expected']) row = cas['case_id'] + 1 respones = request(url=url, method=method, headers=headers) res = respones.json() try: self.assertEqual(expected['code'], res['code']) self.assertEqual(expected['msg'], res['msg']) except AssertionError as e: log.error('用例--{}--未通过'.format(cas['title'])) log.debug('预期结果{}'.format(expected)) log.debug('实际结果{}:'.format(res)) log.exception(e) self.execl.write(row=row, column=8, value='不通过') raise e else: log.info('用例--{}--通过'.format(cas['title'])) self.execl.write(row=row, column=8, value='通过')
class Withdraw(unittest.TestCase): exce = Excel_Hande(os.path.join(DATA_DIR,'apicases.xlsx'),'withdraw') case = exce.read_excel() @data(*case) def test_withdraw(self, cas): print(cas) # @ddt # class kk(unittest.TestCase): # exce = Excel_Hande(os.path.join(DATA_DIR,'apicases.xlsx'),'withdraw') # case = exce.read_excel() # # @data(*case) # def test_gg(self,cas): # print(cas)
class Invest(unittest.TestCase): excel = Excel_Hande(os.path.join(DATA_DIR, 'apicases.xlsx'), 'invest') case = excel.read_excel() bb = HandleMysql() @data(*case) def test_invest(self, cas): url = conf.get('env', 'url') + cas['url'] method = cas['method'] data = eval(replace_data(cas['data'])) headers = eval(conf.get('env', 'headers')) if cas['interface'] != 'login': #如果不是登录的接口需要添加token headers['Authorization'] = getattr(EnvData, 'token') expected = eval(cas['expected']) row = cas['case_id'] + 1 respones = request(url=url, method=method, json=data, headers=headers) res = respones.json() if cas['interface'] == 'login': #如果是登录接口,需要提取id和token token = 'Bearer' + ' ' + jsonpath.jsonpath(res, '$..token')[0] member_id = str(jsonpath.jsonpath(res, '$..id')[0]) setattr(EnvData, 'token', token) setattr(EnvData, 'member_id', member_id) if cas['interface'] == 'add': #如果是加标接口,需要获取加标后的标id loan_id = str(jsonpath.jsonpath(res, '$..id')[0]) setattr(EnvData, 'loan_id', loan_id) print('预期结果:', expected) print('实际结果:', res) try: self.assertEqual(expected['code'], res['code']) self.assertEqual(expected['msg'], res['msg']) except AssertionError as e: log.error('这是{}用例未通过'.format(cas['title'])) log.debug('预期结果{}'.format(expected)) log.debug('实际结果{}'.format(res)) log.exception(e) self.excel.write(row=row, column=8, value='不通过') raise e else: log.info('用例{}通过'.format(cas['title'])) self.excel.write(row=row, column=8, value='通过')
class AuditTest(unittest.TestCase): excel = Excel_Hande(os.path.join(DATA_DIR, 'apicases.xlsx'), 'Sheet1') case = excel.read_excel() bb = HandleMysql() @classmethod def setUpClass(cls): # 普通用户登录 # 准备数据 url = conf.get('env', 'url') + '/member/login' user_data = { 'mobile_phone': conf.get('test_data', 'phone'), 'pwd': conf.get('test_data', 'pwd') } headers = eval(conf.get('env', 'headers')) respons = request(method='post', url=url, json=user_data, headers=headers) re = respons.json() user_member_id = str(jsonpath.jsonpath(re, '$..id')[0]) user_token = 'Bearer' + ' ' + jsonpath.jsonpath(re, '$..token')[0] setattr(EnvData, 'user_member_id', user_member_id) setattr(EnvData, 'user_token', user_token) # 管理员登录 data = { 'mobile_phone': conf.get('test_data', 'admin_phone'), 'pwd': conf.get('test_data', 'admin_pwd') } resp = request(method='post', url=url, json=data, headers=headers) es = resp.json() admin_token = 'Bearer' + ' ' + jsonpath.jsonpath(es, '$..token')[0] setattr(EnvData, 'admin_token', admin_token) # 加标 def setUp(self) -> None: url = conf.get("env", "url") + "/loan/add" headers = eval(conf.get("env", "headers")) headers["Authorization"] = getattr(EnvData, 'user_token') data = { "member_id": getattr(EnvData, 'user_member_id'), "title": "木森借钱买飞机", "amount": 2000, "loan_rate": 12.0, "loan_term": 3, "loan_date_type": 1, "bidding_days": 5 } # 发送请求,添加项目 response = request(method="post", url=url, json=data, headers=headers) res = response.json() # 获取标id loan_id = str(jsonpath.jsonpath(res, "$..id")[0]) setattr(EnvData, 'loan_id', loan_id) @data(*case) def test_audit(self, cas): # 准备数据 url = conf.get('env', 'url') + cas['url'] # 判断是否需要替换为审核通过的标id if "#pass_loan_id#" in cas['data']: cas['data'] = replace_data(cas['data']) cas['data'] = replace_data(cas['data']) data = eval(cas['data']) method = cas['method'] headers = eval(conf.get('env', 'headers')) headers['Authorization'] = getattr(EnvData, 'admin_token') expected = eval(cas['expected']) row = cas['case_id'] + 1 # 获取实际结果 response = request(url=url, method=method, json=data, headers=headers) rese = response.json() print('预期结果:', expected) print('实际结果:', rese) # 判断是否是审核通过的用例,并且审核成功 if cas['title'] == '审核通过' and rese['msg'] == 'OK': setattr(EnvData, 'pass_loan_id', str(data['loan_id'])) try: self.assertEqual(expected['code'], rese['code']) self.assertEqual(expected['msg'], rese['msg']) if cas['check_sql']: sql = replace_data(cas['check_sql']) status = self.bb.find_one(sql)['status'] self.assertEqual(expected['status'], status) print(status, type(status)) except AssertionError as e: log.error('用例--{}--未通过'.format(cas['title'])) log.debug('预期结果{}'.format(expected)) log.debug('实际结果{}:'.format(rese)) log.exception(e) self.excel.write(row=row, column=8, value='不通过') raise e else: log.info('用例--{}--通过'.format(cas['title'])) self.excel.write(row=row, column=8, value='通过')
class AddTest(unittest.TestCase): excel = Excel_Hande(os.path.join(DATA_DIR, 'apicases.xlsx'), 'add') case = excel.read_excel() bb = HandleMysql() @classmethod def setUpClass(cls): # 准备登录用的数据 url = conf.get('env', 'url') + '/member/login' data = { 'mobile_phone': conf.get('test_data', 'phone'), 'pwd': conf.get('test_data', 'pwd') } headers = eval(conf.get('env', 'headers')) respons = request(method='post', url=url, json=data, headers=headers) re = respons.json() member_id = str(jsonpath.jsonpath(re, '$..id')[0]) token = "Bearer" + " " + jsonpath.jsonpath(re, '$..token')[0] setattr(EnvData, 'member_id', member_id) setattr(EnvData, 'token', token) @data(*case) def test_add(self, cas): # 准备用例数据 url = conf.get('env', 'url') + cas['url'] cas['data'] = replace_data(cas['data']) data = eval(cas['data']) method = cas['method'] headers = eval(conf.get('env', 'headers')) headers['Authorization'] = getattr(EnvData, 'token') expected = eval(cas['expected']) row = cas['case_id'] + 1 # 请求之前查询用户标的数量 if cas['check_sql']: sql = replace_data(cas['check_sql']) st = self.bb.find_count(sql) print('请求之前的标数:', st) response = request(url=url, method=method, json=data, headers=headers) res = response.json() print('预期结果:', expected) print('实际结果:', res) # 请求后查询用户标的数量 try: self.assertEqual(expected['code'], res['code']) self.assertEqual(expected['msg'], res['msg']) if cas['check_sql']: sql = replace_data(cas['check_sql']) end = self.bb.find_count(sql) print('请求之后标数', end) self.assertEqual(1, end - st) except AssertionError as e: log.error('用例--{}--未通过'.format(cas['title'])) log.debug('预期结果{}'.format(expected)) log.debug('实际结果{}:'.format(res)) log.exception(e) self.excel.write(row=row, column=8, value='不通过') raise e else: log.info('用例--{}--通过'.format(cas['title'])) self.excel.write(row=row, column=8, value='通过')
class Recharge_Case(unittest.TestCase): excel = Excel_Hande(fms, 'recharge') case_data = excel.read_excel() bb = HandleMysql() @classmethod def setUpClass(cls): url = conf.get('env', 'url') + '/member/login' data = { 'mobile_phone': conf.get('test_data', 'phone'), 'pwd': conf.get('test_data', 'pwd') } headers = eval(conf.get('env', 'headers')) resp = request(method='post', url=url, json=data, headers=headers) qw = resp.json() cls.member_id = str(jsonpath.jsonpath(qw, "$..id")[0]) cls.token = "Bearer" + " " + jsonpath.jsonpath(qw, '$..token')[0] print('用户id', cls.member_id) print('token值', cls.token) @data(*case_data) def test_recharge(self, cas): # 准备数据 url = conf.get('env', 'url') + cas['url'] cas['data'] = cas['data'].replace('#member_id#', self.member_id) data = eval(cas['data']) method = cas['method'] headers = eval(conf.get('env', 'headers')) headers['Authorization'] = self.token expected = eval(cas['expected']) row = cas['case_id'] + 1 if cas['check_sql']: sql = cas['check_sql'].format(self.member_id) money_data = self.bb.find_one(sql)['leave_amount'] print("充值之前的钱:", money_data) # 发送请求获取实际结果 repans = request(url=url, method=method, json=data, headers=headers) res = repans.json() if cas['check_sql']: sql = cas['check_sql'].format(self.member_id) money_data2 = self.bb.find_one(sql)['leave_amount'] print("充值之后的钱:", money_data2) print('预期结果:', expected) print('实际结果:', res) # 断言 try: self.assertEqual(expected['code'], res['code']) self.assertEqual(expected['msg'], res['msg']) if cas['check_sql']: # 将准备的data数据中的amount数值类型转换为decimal my_data = decimal.Decimal(str(data['amount'])) self.assertEqual(my_data, money_data2 - money_data) except AssertionError as e: log.error('用例--{}--未通过'.format(cas['title'])) log.debug('预期结果{}'.format(expected)) log.debug('实际结果{}:'.format(res)) log.exception(e) self.excel.write(row=row, column=8, value='不通过') raise e else: log.info('用例--{}--通过'.format(cas['title'])) self.excel.write(row=row, column=8, value='通过')
class Withdraw(unittest.TestCase): excel = Excel_Hande(os.path.join(DATA_DIR, 'apicases.xlsx'), 'withdraw') case = excel.read_excel() bb = HandleMysql() @classmethod def setUpClass(cls): # 准备的登录的数据 url = conf.get('env', 'url') + '/member/login' data = { 'mobile_phone': conf.get('test_data', 'phone'), 'pwd': conf.get('test_data', 'pwd') } headers = eval(conf.get('env', 'headers')) respons = request(url=url, method='post', json=data, headers=headers) re = respons.json() cls.member_id = jsonpath.jsonpath(re, '$..id')[0] cls.token = "Bearer" + " " + jsonpath.jsonpath(re, '$..token')[0] @data(*case) def test_withdraw(self, cas): # 准备用例数据 url = conf.get('env', 'url') + cas['url'] cas['data'] = cas['data'].replace('#member_id#', str(self.member_id)) data = eval(cas['data']) method = cas['method'] headers = eval(conf.get('env', 'headers')) headers['Authorization'] = self.token expected = eval(cas['expected']) row = cas['case_id'] + 1 # 查询取现之前的余额 if cas['check_sql']: sql = cas['check_sql'].format(self.member_id) start_money = self.bb.find_one(sql)['leave_amount'] print('取现前:', start_money) # 发送请求 response1 = request(method=method, url=url, headers=headers, json=data) res = response1.json() print('实际结果:', res) print('预期结果:', expected) # 断言 try: self.assertEqual(expected['code'], res['code']) self.assertEqual(expected['msg'], res['msg']) # 查询请求之后的账户余额 if cas['check_sql']: sql = cas['check_sql'].format(self.member_id) end_money = self.bb.find_one(sql)['leave_amount'] print('取现后:', end_money) # 用例数据中data,amount的数值类型转换 my_data = decimal.Decimal(str(data['amount'])) self.assertEqual(my_data, start_money - end_money) except AssertionError as e: log.error('用例--{}--未通过'.format(cas['title'])) log.debug('预期结果{}'.format(expected)) log.debug('实际结果{}:'.format(res)) log.exception(e) self.excel.write(row=row, column=8, value='不通过') raise e else: log.info('用例--{}--通过'.format(cas['title'])) self.excel.write(row=row, column=8, value='通过')
class Audit(unittest.TestCase): exce = Excel_Hande(os.path.join(DATA_DIR, 'apicases.xlsx'), 'audit') case = exce.read_excel() bb = HandleMysql() #普通用户登录 @classmethod def setUpClass(cls) -> None: url = conf.get('env', 'url') + '/member/login' user_data = { 'mobile_phone': conf.get('test_data', 'phone'), 'pwd': conf.get('test_data', 'pwd') } headers = eval(conf.get('env', 'headers')) respones = request(url=url, method='post', json=user_data, headers=headers) res = respones.json() user_token = 'Bearer' + ' ' + jsonpath.jsonpath(res, '$..token')[0] user_member_id = str(jsonpath.jsonpath(res, '$..id')[0]) setattr(EnvData, 'user_token', user_token) setattr(EnvData, 'user_member_id', user_member_id) #管理员登录 admin_data = { 'mobile_phone': conf.get('test_data', 'admin_phone'), 'pwd': conf.get('test_data', 'admin_pwd') } q = request(url=url, method='post', json=admin_data, headers=headers) f = q.json() admin_token = 'Bearer' + ' ' + jsonpath.jsonpath(f, '$..token')[0] admin_member_id = str(jsonpath.jsonpath(f, '$..id')[0]) setattr(EnvData, 'admin_token', admin_token) setattr(EnvData, 'admin_member_id', admin_member_id) #每条用例执行前,添加一个标 def setUp(self) -> None: url = conf.get('env', 'url') + '/loan/add' data = { "member_id": getattr(EnvData, 'user_member_id'), "title": "借钱娶媳妇", "amount": 2000, "loan_rate": 12.0, "loan_term": 3, "loan_date_type": 1, "bidding_days": 5 } headers = eval(conf.get('env', 'headers')) headers['Authorization'] = getattr(EnvData, 'user_token') pn = request(url=url, method='post', json=data, headers=headers) ss = pn.json() loan_id = str(jsonpath.jsonpath(ss, '$..id')[0]) setattr(EnvData, 'loan_id', loan_id) @data(*case) def test_audit(self, cas): url = conf.get('env', 'url') + cas['url'] method = cas['method'] data = eval(replace_data(cas['data'])) headers = eval(conf.get('env', 'headers')) headers['Authorization'] = getattr(EnvData, 'admin_token') expected = eval(cas['expected']) row = cas['case_id'] + 1 respones1 = request(url=url, method=method, json=data, headers=headers) res1 = respones1.json() if cas['title'] == '审核通过' and res1['msg'] == 'OK': setattr(EnvData, 'pass_loan_id', str(data['loan_id'])) print('预期结果:', expected) print('实际结果:', res1) try: self.assertEqual(expected['code'], res1['code']) self.assertEqual(expected['msg'], res1['msg']) #判断审核状态是否符合预期的结果 if cas['check_sql']: sql = replace_data(cas['check_sql']) status = self.bb.find_one(sql)['status'] self.assertEqual(expected['status'], status) except AssertionError as e: log.error('这条用例{}没有通过'.format(cas['title'])) log.exception(e) self.exce.write(row=row, column=8, value='不通过') raise e else: log.info('这条用例{}执行通过'.format(cas['title'])) self.exce.write(row=row, column=8, value='通过')