class TestRegister(unittest.TestCase): excel = do_excel.DoExcel(contants.case_file, 'TestRegister') cases = excel.get_case() @classmethod def setUpClass(cls): cls.http_request=HTTPRequest2() cls.mysql=do_mysql.DoMysql() @data(*cases) #data可以接收可迭代数据类型,列表,元组,字典 def test_register(self,case): print(case.title) if case.data.find('register_mobile') > -1: sql='select max(mobilephone) from future.member' max_phone=self.mysql.fetch_one(sql)[0] #查询最大手机号码 #最大手机号码加1 # max_phone=int(max_phone)+1 #因为库里最大是18999999999,所以这行被我注释 print('最大手机号码',max_phone) #replace()方法是替换之后重新返回一个新的字符串,需要重新赋值给case.data case.data=case.data.replace('register_mobile',str(max_phone)) #最大手机号码替换参数值 resp = self.http_request.request(case.method, case.url, case.data) try: self.assertEqual(case.expected,resp.text) 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, "fail") raise e
class RegisterTest(unittest.TestCase): excel = do_excel.DoExcel(contants.case_file, 'register') cases = excel.get_cases() @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 max_phone = int(max_phone) + 1 print('最大手机号码', max_phone) # replace方法是特换之后重新返回一个新的字符串,所以需要使用case.data重新接收 case.data = case.data.replace('register_mobile', str(max_phone)) # 特换参数值 resp = self.http_request.request(case.method, case.url, case.data) try: self.assertEqual(case.expected, resp.text) 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, 'FAIL') raise e
class AddTest(unittest.TestCase): excel = do_excel.DoExcel(contants.case_file, 'add') cases = excel.get_cases() @classmethod def setUpClass(cls): cls.http_request = HTTPRequest2() @data(*cases) def test_add(self, case): # case.data = eval(case.data) # 变成字典 # print(type(case.data)) # if case.data.__contains__('mobilephone') and case.data['mobilephone'] == 'normal_user': # case.data['mobilephone'] = config.get('data', 'normal_user') # 拿到配置文件里面的值赋值给mobilephone # # if case.data.__contains__('pwd') and case.data['pwd'] == 'normal_pwd': # case.data['pwd'] = config.get('data', 'normal_pwd') # 拿到配置文件里面的值赋值给mobilephone # # if case.data.__contains__('memberId') and case.data['memberId'] == 'loan_member_id': # case.data['memberId'] = config.get('data', 'loan_member_id') # 拿到配置文件里面的值赋值给mobilephone # 在请求之前替换参数化的值 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_result(case.case_id + 1, resp.text, 'PASS') except AssertionError as e: self.excel.write_result(case.case_id + 1, resp.text, 'FAIL') raise e
class TestAdd(unittest.TestCase): excel = do_excel.DoExcel(contants.case_file, 'TestAdd') cases = excel.get_case() @classmethod def setUpClass(cls): cls.http_request = HTTPRequest2() @data(*cases) #data可以接收可迭代数据类型,列表,元组,字典 def test_add(self, case): print(case.title) # case.data=eval(case.data) #变成字典 # # if case.data.__contains__('mobilephone') and case.data['mobilephone'] == 'normal_user': # case.data['mobilephone'] = config.get('data','normal_user') #拿到配置文件里面的值赋值给mobilephone # # if case.data.__contains__('pwd') and case.data['pwd'] == 'normal_pwd': # case.data['pwd'] = config.get('data','normal_pwd') # # if case.data.__contains__('memberId') and case.data['memberId'] == 'loan_member_id': # case.data['memberId'] = config.get('data','loan_member_id') #在请求之前替换参数化的值 case.data = context.replace(case.data) resp = self.http_request.request(case.method, case.url, case.data) print(resp.text) print(resp.status_code) try: self.assertEqual(case.expected, resp.text) 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, "fail") raise e @classmethod def tearDownClass(cls): cls.http_request.close()
class LoginTest(unittest.TestCase): excel = do_excel.DoExcel(contants.case_file, 'login') cases = excel.get_cases() @classmethod def setUpClass(cls): cls.http_request = HTTPRequest2() @data(*cases) def test_login(self, case): resp = self.http_request.request(case.method, case.url, case.data) try: self.assertEqual(case.expected, resp.text) 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, 'FAIL') raise e @classmethod def tearDownClass(cls): cls.http_request.close()
class RechargeTest(unittest.TestCase): excel = do_excel.DoExcel(contants.case_file, 'recharge') cases = excel.get_cases() @classmethod def setUpClass(cls): cls.http_request = HTTPRequest2() @data(*cases) def test_recharge(self, case): print(case.title) resp = self.http_request.request(case.method, case.url, case.data) actual_code = resp.json()['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, 'FAIL') raise e @classmethod def tearDownClass(cls): cls.http_request.close()
class TestRecharge(unittest.TestCase): excel = do_excel.DoExcel(contants.case_file, 'TestRecharge') cases = excel.get_case() @classmethod def setUpClass(cls): cls.http_request=HTTPRequest2() @data(*cases) #data可以接收可迭代数据类型,列表,元组,字典 def test_recharge(self,case): print(case.title) resp = self.http_request.request(case.method, case.url, case.data) actual_code=resp.json()['code'] #返回的是字典,然后找code,d[key] 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, "fail") raise e @classmethod def tearDownClass(cls): cls.http_request.close()