class RegisterTest(unittest.TestCase): excel = Do_Excel(contants.case_data, 0) cases = excel.create_obj_read_data() @classmethod def setUpClass(cls): log.info("测试前置准备") cls.http_request = http_api.Http_Session() #实例化request的session类 cls.db = CommDB() #实例化mysql类 @data(*cases) def test_register(self, case): log.info("开始执行测试{}".format(case.case_title)) if case.data.find("normal_user") > -1: sql = "SELECT max(mobilephone) FROM future.member WHERE mobilephone LIKE '1860735%'" max_phone = self.db.fetch_one(sql)['max(mobilephone)'] print("mysql:", max_phone) #查询最大手机号码,返回的是元组,根据索引获取第一条数据 before_max_phone = int( max_phone) + 1 #max_phone是str类型,转成int类型,最大手机号码+1(也可以最小手机号码-1) log.debug("before_max_phone号码:{}".format(before_max_phone)) #字符串的替换 case.data = case.data.replace("normal_user", str(before_max_phone)) # self.db.commit() # 修改或替换数据后,mysql要提交,否则后面有参数的数据将不会执行 datafill = Do_Excel(contants.case_data, 0) #再次打开excel读取数据 resp = self.http_request.http_session(case.method, case.url, case.data) log.debug("返回的response是:{}".format(resp)) try: self.assertEqual(str(case.expected), resp.text) datafill.write_result(case.case_id + 1, resp.text, "PASS") # 判断注册成功之后,查询数据库,取到mobilephone if resp.json()['msg'] == "注册成功": if case.sql is not None: print('用例验证sql:', case.sql) sql = case.sql + eval(case.data)['mobilephone'] log.debug("执行的sql语句是:{}".format(sql)) after_max_phone = self.db.fetch_one(sql)['MobilePhone'] # print("mysql:", after_max_phone) # 查询最大手机号码,返回的是元组,根据索引获取第一条数据 log.debug("注册成功后的最大号码:{}".format(after_max_phone)) self.assertEqual(str(before_max_phone), after_max_phone) except AssertionError as e: datafill.write_result(case.case_id + 1, resp.text, "FAILED") log.error("出错了:{}".format(e)) raise e log.info("测试结束了{}".format(case.case_title))
def test_audit(self, case): log.info("开始执行测试{}".format(case.case_title)) # 打印测试用例title case.data = ConText().replace( p='#(.*?)#', data=case.data) #用正则表达式,在conf文件替换excel文件的值 print(case.data) datafill = Do_Excel(contants.case_data, 6) resp = self.http_request.http_session(method=case.method, url=case.url, data=case.data) log.debug("返回的response是:{}".format(resp)) actual_code = resp.json()["code"] log.debug("actual_code是:{}".format(actual_code)) try: self.assertEqual( str(case.expected), actual_code) #case.expected从excel取出来是int类型,要转换成str类型 datafill.write_result(case.case_id + 1, resp.text, "PASS") except AssertionError as e: datafill.write_result(case.case_id + 1, resp.text, "FAILED") log.error("测试报错了:{}".format(e)) raise e log.info("结束测试:{}".format(case.case_title))
def test_login(self, case): log.info("开始执行测试{}".format(case.case_title)) #数据读取方法一: # case.data=eval(case.data) # print(case.data) # # #判断case.data里面是否有mobilephone这个key,且key的值是否等于login_mobile # if case.data.__contains__("mobilephone") and case.data["mobilephone"]=="login_mobile": # #如果有,则case.data["mobilephone"]替换成配置文件ogin_mobile的value值 # case.data["mobilephone"]=config.get_str("data","login_mobile") # # #判断case.data里面是否有mobilephone这个key,且key的值是否等于login_mobile # if case.data.__contains__("pwd") and case.data["pwd"]=="login_pwd": # # 如果有,则case.data["mobilephone"]替换成配置文件ogin_mobile的value值 # case.data["pwd"]=config.get_str("data","login_pwd") #数据读取方法二: #正则表达式 case.data = ConText().replace(p='#(.*?)#', data=case.data) print(type(case.data)) datafill = Do_Excel(contants.case_data, 1) resp = self.http_request.http_session(case.method, case.url, case.data) # actual_code=resp.json()["code"] #获取实际结果的code log.debug("返回的response是:{}".format(resp)) try: self.assertEqual(str(case.expected), resp.text) #预期结果和时间结果对比 datafill.write_result(case.case_id + 1, resp.text, "PASS") #写入实际结果,和通过结果:pass except AssertionError as e: datafill.write_result(case.case_id + 1, resp.text, "FAILED") #写入实际结果,和通过结果:failed log.error("出错了:{}".format(e)) raise e #抛出异常 log.info("测试结束了{}".format(case.case_title))
class WithdrawTest(unittest.TestCase): excel=Do_Excel(contants.case_data,3) cases=excel.create_obj_read_data() @classmethod def setUpClass(cls): log.info("准备测试前置") cls.http_request=Http_Session() @data(*cases) def test_withdraw(self,case): log.info("开始执行测试{}".format(case.case_title)) # 打印测试用例title case.data=ConText().replace(p='#(.*?)#',data=case.data) resp=self.http_request.http_session(case.method,case.url,case.data) print(case.method,case.url,case.data) actual_code=resp.json()["code"] log.debug("actual_code是:{}".format(actual_code)) try: self.assertEqual(str(case.expected),actual_code) self.excel.write_result(case.case_id+1,resp.text,"PASS") except AssertionError as e: self.excel.write_result(case.case_id+1,resp.text,"FAILED") log.error("测试报错了:{}".format(e)) raise e log.info("结束测试:{}".format(case.case_title)) # finally: # self.excel.save_excel() @classmethod def tearDownClass(cls): log.info("测试后置处理") # cls.excel.close_excel() cls.http_request.close()
def test_bid_loan(self, case): log.info("开始执行测试:{0}".format(case.case_title)) case.data = ConText().replace(p='#(.*?)#', data=case.data) print(case.data) data_fill = Do_Excel(contants.case_data, 4) resp = self.http_request.http_session(method=case.method, url=case.url, data=case.data) log.debug("返回的response是:{}".format(resp)) actual_code = resp.json()["code"] log.debug("actual_code是:{}".format(actual_code)) try: self.assertEqual(str(case.expected), actual_code) data_fill.write_result(case.case_id + 1, resp.text, "PASS") if resp.json()["msg"] == "加标成功": sql = "SELECT MAX(id) FROM future.loan WHERE memberid=1312 LIMIT 1" loan_id = self.db.fetch_one(sql)[0] print("loan_id是:", loan_id) setattr(ConText, "loan_id", str(loan_id)) except AssertionError as e: data_fill.write_result(case.case_id + 1, resp.text, "FAILED") log.error("测试报错了:{}".format(e)) raise e log.info("结束测试:{}".format(case.case_title))
class LoginTest(unittest.TestCase): excel = Do_Excel(contants.case_data, 1) cases = excel.create_obj_read_data() #setUpClass(): 必须使用 @ classmethod装饰器, 所有case运行之前只运行一次 @classmethod def setUpClass(cls): log.info("测试前置准备") cls.http_request = Http_Session() @data(*cases) def test_login(self, case): log.info("开始执行测试{}".format(case.case_title)) #数据读取方法一: # case.data=eval(case.data) # print(case.data) # # #判断case.data里面是否有mobilephone这个key,且key的值是否等于login_mobile # if case.data.__contains__("mobilephone") and case.data["mobilephone"]=="login_mobile": # #如果有,则case.data["mobilephone"]替换成配置文件ogin_mobile的value值 # case.data["mobilephone"]=config.get_str("data","login_mobile") # # #判断case.data里面是否有mobilephone这个key,且key的值是否等于login_mobile # if case.data.__contains__("pwd") and case.data["pwd"]=="login_pwd": # # 如果有,则case.data["mobilephone"]替换成配置文件ogin_mobile的value值 # case.data["pwd"]=config.get_str("data","login_pwd") #数据读取方法二: #正则表达式 case.data = ConText().replace(p='#(.*?)#', data=case.data) print(type(case.data)) datafill = Do_Excel(contants.case_data, 1) resp = self.http_request.http_session(case.method, case.url, case.data) # actual_code=resp.json()["code"] #获取实际结果的code log.debug("返回的response是:{}".format(resp)) try: self.assertEqual(str(case.expected), resp.text) #预期结果和时间结果对比 datafill.write_result(case.case_id + 1, resp.text, "PASS") #写入实际结果,和通过结果:pass except AssertionError as e: datafill.write_result(case.case_id + 1, resp.text, "FAILED") #写入实际结果,和通过结果:failed log.error("出错了:{}".format(e)) raise e #抛出异常 log.info("测试结束了{}".format(case.case_title)) #tearDownClass(): 必须使用 @ classmethod装饰器, 所有case运行完之后只运行一次 @classmethod def tearDownClass(cls): # cls.excel.save_excel() # cls.excel.close_excel() log.info("测试后置处理") cls.http_request.close()
class AuditTest(unittest.TestCase): excel = Do_Excel(contants.case_data, 6) cases = excel.create_obj_read_data() @classmethod def setUpClass(cls): log.info("准备测试前置") cls.http_request = Http_Session() #实例化request类 @data(*cases) def test_audit(self, case): log.info("开始执行测试{}".format(case.case_title)) # 打印测试用例title case.data = ConText().replace( p='#(.*?)#', data=case.data) #用正则表达式,在conf文件替换excel文件的值 print(case.data) datafill = Do_Excel(contants.case_data, 6) resp = self.http_request.http_session(method=case.method, url=case.url, data=case.data) log.debug("返回的response是:{}".format(resp)) actual_code = resp.json()["code"] log.debug("actual_code是:{}".format(actual_code)) try: self.assertEqual( str(case.expected), actual_code) #case.expected从excel取出来是int类型,要转换成str类型 datafill.write_result(case.case_id + 1, resp.text, "PASS") except AssertionError as e: datafill.write_result(case.case_id + 1, resp.text, "FAILED") log.error("测试报错了:{}".format(e)) raise e log.info("结束测试:{}".format(case.case_title)) # finally: # self.excel.save_excel() @classmethod def tearDownClass(cls): log.info("测试后置处理") # cls.excel.close_excel() cls.http_request.close()
class RechargeTest(unittest.TestCase): excel=Do_Excel(contants.case_data,2) cases=excel.create_obj_read_data() @classmethod def setUpClass(cls): log.info("测试前置准备") cls.http_request=Http_Session() cls.db=CommDB() @data(*cases) def test_recharge(self,case): if case.sql is not None: sql = eval(case.sql)['sql1'] # 取到excel的check_sql列的第一条sql语句 amount = self.db.fetch_one(sql) # 执行sql语 # 句 print("金额",amount['LeaveAmount']) before_amount = amount['LeaveAmount'] # 充值之前的金额 case.data = ConText().replace(p='#(.*?)#', data=case.data) # 加入正则表达式 resp=self.http_request.http_session(case.method,case.url,case.data) actual_code=resp.json()["code"] #返回的code try: self.assertEqual(str(case.expected),actual_code) self.excel.write_result(case.case_id+1,actual_code,"PASS") if case.sql is not None: sql = eval(case.sql)['sql1'] # 取到excel的check_sql列的第一条sql语句 amount = self.db.fetch_one(sql) # 执行sql语句 after_amount = amount['LeaveAmount'] recharge_amount=float(eval(case.data)['amount']) self.assertEqual(float(before_amount)+recharge_amount,float(after_amount)) #充值之前的金额+这次充值的金额 与 充值之后的金额做对比 except AssertionError as e: self.excel.write_result(case.case_id+1,actual_code,"FAILED") log.error("出错了:{}".format(e)) raise e log.info("测试结束了{}".format(case.case_title))