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 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 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 TestLogin(unittest.TestCase): excel = do_excel.DoExcel(contants.case_file, 'TestLogin') cases = excel.get_case() @classmethod def setUpClass(cls): cls.http_request = HTTPRequest2() @data(*cases) #data可以接收可迭代数据类型,列表,元组,字典 def test_login(self, case): print(case.title) 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 LoginTest(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()