def setUpClass(cls): # 准备登录的数据 # 获取登录的URL地址 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")) # 发送请求进行登录 response = cls.request.send_requests_sc(url=url, method="post", json=data, headers=headers) # 获取返回的数据 res = response.json() # 提取token() token = jsonpath.jsonpath(res, "$..token")[0] token_type = jsonpath.jsonpath(res, "$..token_type")[0] # 将提取到的token设置为类属性 Case_Data.token_value = token_type + " " + token # 提取用户的登录id,保存为类属性 Case_Data.member_id = str(jsonpath.jsonpath(res, "$..id")[0])
def test_login(self, case): # 准备用例数据 url = conf.get("env", "url") + case["url"] method = case['method'] data = eval(case["data"]) headers = eval(conf.get('env', 'headers')) # 预期结果 expected = eval(case['expected']) row = case['case_id'] + 1 # 发送请求获取结果 response = self.request.send_requests_sc(url=url, method=method, json=data, headers=headers) res = response.json() # 断言比对预期结果和实际结果 try: self.assertEqual(expected['code'], res['code']) self.assertEqual(expected['msg'], res['msg']) except AssertionError as e: self.excel.write_excel(row=row, column=8, value='未通过') log.error('用例:{},执行'.format(case['title']) + color.white_red('未通过')) raise e else: self.excel.write_excel(row=row, column=8, value='通过') log.info('用例:{},执行'.format(case['title']) + color.white_green('通过'))
def test_update(self,case): url = conf.get('env','url') + case["url"] method =case['method'] data = eval(replace_data(case["data"])) headers =eval(conf.get('env','headers')) headers["Authorization"] = getattr(Case_Data, "token_value") expected = eval(case['expected']) row = case['case_id'] + 1 response = self.request.send_requests_sc(url=url, method=method, json=data, headers=headers) res = response.json() # 断言比对预期结果和实际结果 try: self.assertEqual(expected['code'], res['code']) self.assertEqual(expected['msg'], res['msg']) except AssertionError as e: self.excel.write_excel(row=row, column=8, value='未通过') log.error('用例:{},执行'.format(case['title']) + color.white_red('未通过')) raise e else: self.excel.write_excel(row=row, column=8, value='通过') log.info('用例:{},执行'.format(case['title']) + color.white_green('通过'))
def test_add(self, case): # 准备数据 url = conf.get("env", "url") + case["url"] headers = eval(conf.get("env", "headers")) headers["Authorization"] = getattr(Case_Data, "admin_token_value") data = eval(replace_data(case["data"])) expected = eval(case["expected"]) method = case["method"] row = case["case_id"] + 1 # 发送请求 response = self.request.send_requests_sc(url=url, method=method, json=data, headers=headers) res = response.json() # 断言结果 try: self.assertEqual(expected['code'], res['code']) self.assertEqual(expected['msg'], res['msg']) except AssertionError as e: self.excel.write_excel(row=row, column=8, value='未通过') log.error('用例:{},执行'.format(case['title']) + color.white_red('未通过')) raise e else: self.excel.write_excel(row=row, column=8, value='通过') log.info('用例:{},执行'.format(case['title']) + color.white_green('通过')) pass
def test_recharge(self, case): # 准备用例数据 url = conf.get("env", "url") + case["url"] method = case['method'] # 替换用户id case["data"] = replace_data(case["data"]) data = eval(case["data"]) headers = eval(conf.get('env', 'headers')) headers["Authorization"] = getattr(Case_Data, "token_value") # 预期结果 expected = eval(case['expected']) row = case['case_id'] + 1 # 发送请求前获取用户 if case["check_sql"]: sql = 'SELECT leave_amount FROM futureloan.member where mobile_phone={}'.format( conf.get('test_data', 'phone')) start_money = self.db.find_one(sql)['leave_amount'] # 发送请求获取结果 response = self.request.send_requests_sc(url=url, method=method, json=data, headers=headers) res = response.json() # 发送请求后获取用户 if case["check_sql"]: sql = 'SELECT leave_amount FROM futureloan.member where mobile_phone={}'.format( conf.get('test_data', 'phone')) end_money = self.db.find_one(sql)['leave_amount'] # 断言比对预期结果和实际结果 try: self.assertEqual(expected['code'], res['code']) self.assertEqual(expected['msg'], res['msg']) # 判断是否需要进行判断 if case['check_sql']: self.assertEqual(end_money - start_money, Decimal(str(data['amount']))) except AssertionError as e: self.excel.write_excel(row=row, column=8, value='未通过') log.error('用例:{},执行'.format(case['title']) + color.white_red('未通过')) raise e else: self.excel.write_excel(row=row, column=8, value='通过') log.info('用例:{},执行'.format(case['title']) + color.white_green('通过'))
def test_invest(self, case): # 准备用例数据 url = conf.get("env", "url") + case["url"] method = case['method'] case["data"] = replace_data(case["data"]) data = eval(case["data"]) headers = eval(conf.get('env', 'headers')) # 判断当前接口是不是登录,加token if case["interface"] != "login": headers["Authorization"] = getattr(Case_Data,"token_value") # 预期结果 expected = eval(case['expected']) row = case['case_id'] + 1 # 发送请求获取结果 response = self.request.send_requests_sc(url=url, method=method, json=data, headers=headers) res = response.json() # 判断发送请求之后,判断是不是登录,提取token if case["interface"].lower() == "login": Case_Data.member_id = str(jsonpath.jsonpath(res, '$..id')[0]) token = jsonpath.jsonpath(res, '$..token')[0] token_type = jsonpath.jsonpath(res, '$..token_type')[0] # 提取token保存类属性 Case_Data.token_value = token_type + " " + token # 判断是否是加标的用例,就加标id if case["interface"] == "add": Case_Data.loan_id = str(jsonpath.jsonpath(res,"$..id")[0]) # 断言比对预期结果和实际结果 try: self.assertEqual(expected['code'], res['code']) self.assertIn(expected["msg"],res["msg"]) except AssertionError as e: self.excel.write_excel(row=row, column=8, value='未通过') log.error('用例:{},执行'.format(case['title']) + color.white_red('未通过')) raise e else: self.excel.write_excel(row=row, column=8, value='通过') log.info('用例:{},执行'.format(case['title']) + color.white_green('通过'))
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")) response = cls.request.send_requests_sc(url=url, method="post", json=data, headers=headers) res = response.json() token = jsonpath.jsonpath(res,"$..token")[0] token_type = jsonpath.jsonpath(res,"$..token_type")[0] Case_Data.token_value = token_type + " " + token Case_Data.member_id = str(jsonpath.jsonpath(res,"$..id")[0])
def test_register(self, case): # 准备用例数据 url = conf.get("env", "url") + case['url'] method = case['method'] # 替换手机号码 phone = self.iphone() case['data'] = case['data'].replace('#phone#', phone) data = eval(case["data"]) headers = eval(conf.get('env', 'headers')) # 预期结果 expected = eval(case['expected']) row = case['case_id'] + 1 # 发送请求获取结果 response = self.request.send_requests_sc(url=url, method=method, json=data, headers=headers) res = response.json() # 注册后,进行数据库校验 if case["check_sql"]: sql = 'select * from futureloan.member where mobile_phone = {}'.format( data['mobile_phone']) count = self.db.find_count(sql) self.assertEqual(1, count) # 断言比对预期结果和实际结果 try: self.assertEqual(expected['code'], res['code']) self.assertEqual(expected['msg'], res['msg']) except AssertionError as e: self.excel.write_excel(row=row, column=8, value='未通过') log.error('用例:{},执行'.format(case['title']) + color.white_red('未通过')) raise e else: self.excel.write_excel(row=row, column=8, value='通过') log.info('用例:{},执行'.format(case['title']) + color.white_green('通过'))
def test_loans(self, case): url = conf.get("env", "url") + case["url"] method = case["method"] data = replace_data(case['data']) headers = eval(conf.get("env", "headers")) expected = eval(case['expected']) row = case['case_id'] + 1 response = self.request.send_requests_sc(url=url,method=method,params=data,headers=headers) res = response.json() try: self.assertEqual(expected['code'], res['code']) self.assertEqual(expected['msg'], res['msg']) except AssertionError as e: self.excel.write_excel(row=row, column=8, value='未通过') log.error('用例:{},执行'.format(case['title']) + color.white_red('未通过')) raise e else: self.excel.write_excel(row=row, column=8, value='通过') log.info('用例:{},执行'.format(case['title']) + color.white_green('通过'))
def setUp(cls): """登录管理员账号""" url = conf.get("env", "url") + "/member/login" data = { "mobile_phone": conf.get("test_data", "admin_phone"), "pwd": conf.get("test_data", "admin_pwd") } # 请求头 headers = eval(conf.get("env", "headers")) response = cls.request.send_requests_sc(url=url, method="post", json=data, headers=headers) res = response.json() token = jsonpath.jsonpath(res, "$..token")[0] token_type = jsonpath.jsonpath(res, "$..token_type")[0] member_id = str(jsonpath.jsonpath(res, "$..id")[0]) # 将提取的数据保存到Case_data属性中 Case_Data.admin_token_value = token_type + " " + token Case_Data.admin_member_id = member_id
def send_email(filename, title): # 连接邮箱的smtp服务器,并登录 smtp = smtplib.SMTP_SSL(host=conf.get("email", 'host'), port=conf.getint("email", 'port')) smtp.login(user=conf.get("email", 'user'), password=conf.get("email", 'password')) # 构建一封邮箱 msg = MIMEMultipart() with open(filename, "rb") as F: content = F.read() # 创建邮文本内容 test_msg = MIMEText(content, _subtype="html", _charset='utf-8') # 添加到多组邮件的附件中 msg.attach(test_msg) # 创建邮件附件 report_file = MIMEApplication(content) report_file.add_header('content-disposition', 'attachment', filename=os.path.split(filename)[-1]) # 将附件添加到多组件的邮件中 msg.attach(report_file) # 主题 msg["Subject"] = title # 发件人 msg["From"] = conf.get("email", 'from_adder') # 收件人 msg["To"] = conf.get("email", 'to_adder') # 发送邮箱 smtp.send_message(msg, from_addr=conf.get("email", 'from_adder'), to_addrs=conf.get("email", 'to_adder'))