class RegisterTest(unittest.TestCase): excel = do_excel.Do_Excel(contants.case_file, 'register') cases = excel.get_cases() ##实例化对象 列表里面放case实例 @classmethod ##类方法,每一个类只执行一次 def setUpClass(cls): cls.http_request = HTTPRequest2() ##实例化 cls.mysql = do_mysql.DoMysql() @data(*cases) def test_register(self, case): ###对象迭代里面的属性值 if case.data.find('register_mobile') > -1: ##字符串的查找 sql = 'select max(mobilephone) from future.member' ##查询最大手机号码 max_phone = self.mysql.fetch_one(sql)[ 0] ##返回最近的一条数据 最大的手机号 元组类型 # 最大手机号码+1, 原来是元组类型,转成int max_phone = int(max_phone) + 1 ##replace方法替换之后重新返回一个新的字符串,有返回值,要接受 case.data = case.data.replace( 'register_mobile', str(max_phone)) ###字符串的替换 replace(old,new) resp = self.http_request.request(case.method, case.url, case.data) try: self.assertEqual(case.expected, resp.text) self.excel.Write_excel(case.case_id + 1, resp.text, 'PASS') except AssertionError as e: self.excel.Write_excel(case.case_id + 1, resp.text, 'FAIL') raise e
class RechargeTest(unittest.TestCase): excel = do_excel.Do_Excel(contants.case_file, 'recharge') cases = excel.get_cases() ##实例化对象 列表里面放case实例 @classmethod ##类方法,每一个类只执行一次 def setUpClass(cls): cls.http_request = HTTPRequest2() ##实例化 cls.mysql = DoMysql() @data(*cases) def test_recharge(self, case): ###对象迭代里面的属性值 print(case.title) ##请求之前先判断是否需要执行sql if case.sql is not None: sql = eval(case.sql)['sql1'] member = self.mysql.fetch_one(sql) print(member['leaveamount']) before = member['leaveamount'] resp = self.http_request.request(case.method, case.url, case.data) resp_code = resp.json()['code'] try: self.assertEqual(str(case.expected), resp_code) self.excel.Write_excel(case.case_id + 1, resp.text, 'PASS') ##成功之前先判断是否需要执行sql if case.sql is not None: sql = eval(case.sql)['sql1'] member = self.mysql.fetch_one(sql) # print(member['leaveamount']) after = member['leaveamount'] recharge_amount = int(eval(case.data)['amount']) ###st转字段,在转整型 self.assertEqual(before + recharge_amount, after) #self.assertEqual(before+case.data['amount'],after) except AssertionError as e: self.excel.Write_excel(case.case_id + 1, resp.text, 'FAIL') raise e
class LoginTest(unittest.TestCase): excel = do_excel.Do_Excel(contants.case_file, 'login') cases = excel.get_cases() ##实例化对象 列表里面放case实例 @classmethod ##类方法,每一个类只执行一次 def setUpClass(cls): logger.info("准备测试前置") cls.http_request = HTTPRequest2() ##实例化 @data(*cases) def test_login(self, case): ###对象迭代里面的属性值 logger.info("开始测试:{0}".format(case.title)) resp = self.http_request.request(case.method, case.url, case.data) try: self.assertEqual(case.expected, resp.text) self.excel.Write_excel(case.case_id + 1, resp.text, 'PASS') except AssertionError as e: self.excel.Write_excel(case.case_id + 1, resp.text, 'FAIL') logger.error("测试报错了:{0}".format(e)) raise e @classmethod def tearDownClass(cls): logger.info("测试后置处理") cls.http_request.close()
class InvestTest(unittest.TestCase): excel = do_excel.Do_Excel(contants.case_file, 'invest') cases = excel.get_cases() ##实例化对象 列表里面放case实例 @classmethod ##类方法,每一个类只执行一次 def setUpClass(cls): cls.http_request = HTTPRequest2() ##实例化 cls.mysql = do_mysql.DoMysql() @data(*cases) def test_invest(self, case): ###对象迭代里面的属性值 print("开始执行测试用例", case.title) print(type(case.data), case.data) ##请求之前,替换参数化的值 case.data = context.replace(case.data) resp = self.http_request.request(case.method, case.url, case.data) try: self.assertEqual(str(case.expected), resp.json()['code']) self.excel.Write_excel(case.case_id + 1, resp.text, 'PASS') ##判断加标成功以后,查询数据库,渠道loan_id if resp.json()['mag'] == '加标成功': sql = "select id from future.loan where memberid=1088 order by id desc limit 1" loan_id = self.mysql.fetch_one(sql)[0] print("标的id", loan_id) ##添加保存到类属性 setattr(Context, 'loan_id', str(loan_id)) except AssertionError as e: self.excel.Write_excel(case.case_id + 1, resp.text, 'FAIL') raise e @classmethod def tearDownClass(cls): cls.http_request.close()
class AddTest(unittest.TestCase): excel = do_excel.Do_Excel(contants.case_file, 'add') cases = excel.get_cases() ##实例化对象 列表里面放case实例 @classmethod ##类方法,每一个类只执行一次 def setUpClass(cls): cls.http_request = HTTPRequest2() ##实例化 @data(*cases) def test_longin(self, case): ###对象迭代里面的属性值 case.data = eval(case.data) ##变成一个字段 print(type(case.data), case.data) if case.data.__contains__( 'mobilephone') and case.data['mobilephone'] == 'normal_user': case.data['mobilephone'] = config.get('data', 'normal_user') ##拿到 值赋值给data
class AddTest(unittest.TestCase): excel = do_excel.Do_Excel(contants.case_file, 'add') cases = excel.get_cases() ##实例化对象 列表里面放case实例 @classmethod ##类方法,每一个类只执行一次 def setUpClass(cls): cls.http_request = HTTPRequest2() ##实例化 @data(*cases) def test_add(self, case): ###对象迭代里面的属性值 print(type(case.data), case.data) ##请求之前,替换参数化的值 case.data = context.replace(case.data) resp = self.http_request.request(case.method, case.url, case.data) try: self.assertEqual(str(case.expected), resp.json()['code']) self.excel.Write_excel(case.case_id + 1, resp.text, 'PASS') except AssertionError as e: self.excel.Write_excel(case.case_id + 1, resp.text, 'FAIL') raise e @classmethod def tearDownClass(cls): cls.http_request.close()
import openpyxl from InterfaceTest.common import do_excel from InterfaceTest.common import http_requests from InterfaceTest.common import contants if __name__ == '__main__': do_excel = do_excel.Do_Excel(contants.case_file, sheet_name="login") case_read = do_excel.get_cases() http_request = http_requests.HTTPRequest() # print(case_read.__dict__) for case in case_read: # print(case.case_id) print(case.__dict__) ##写入的话,要先调用httprequest执行,执行成功写入 resp = http_request.request(case.method, case.url, case.data) actual = resp.text if case.expected == actual: ##判断执行厚的实际结果是否与预期结果一致 do_excel.Write_excel(case.case_id + 1, actual, 'PASS') else: do_excel.Write_excel(case.case_id + 1, actual, 'FAIL')
import openpyxl from InterfaceTest.common import do_excel from InterfaceTest.common import http_requests from InterfaceTest.common import contants if __name__ == '__main__': do_excel = do_excel.Do_Excel(contants.case_file, sheet_name="recharge") case_read = do_excel.get_cases() http_request = http_requests.HTTPRequest2() params = {"mobilephone": "15810447878", "pwd": "123456"} resp2 = http_request.request( 'POST', 'http://test.lemonban.com/futureloan/mvc/api/member/login', params) print(resp2.text) print(resp2.status_code) print(resp2) # print(resp2.json()['msg']) for case in case_read: print(case.__dict__) ##写入的话,要先调用httprequest执行,执行成功写入 resp = http_request.request(case.method, case.url, eval(case.data)) actual = resp.text if case.expected == actual: ##判断执行厚的实际结果是否与预期结果一致 do_excel.Write_excel(case.case_id + 1, actual, 'PASS') else: do_excel.Write_excel(case.case_id + 1, actual, 'FAIL')