class Test_Login(unittest.TestCase): def setUp(self) -> None: self.session = requests.Session() self.login_obj = Login() def tearDown(self) -> None: self.session.close() @parameterized.expand(read_json_file()) def test_login(self, mobile, password, success, code, message): print("-" * 100) print("参数化读取的数据:", mobile, password, success, code, message) response = self.login_obj.login(self.session, mobile, password) print("登录响应的结果:", response.json()) self.assertEqual(success, response.json().get("success")) self.assertEqual(code, response.json().get("code")) self.assertIn(message, response.json().get("message")) def test_login_success(self): response = self.login_obj.login(self.session, "13800000002", "123456") print("登录成功的结果", response.json()) # 登录成功的结果 {'success': True, 'code': 10000, 'message': '操作成功!', 'data': '7b3a2252-c859-41b5-83ba-396a99d20ba3'} self.assertEqual(True, response.json().get("success")) self.assertEqual(10000, response.json().get("code")) self.assertIn("操作成功", response.json().get("message")) token = response.json().get("data") app.TOKEN = token
class TestLogin(unittest.TestCase): # def setUp(self): self.session = requests.Session() #创建登陆类对象 self.login = Login() def tearDown(self): self.session.close() #参数化的调用 @parameterized.expand(read_json_file()) #登陆测试 def test_login(self,mobile,password,success,code,message): # response1=request.Session.post(url) response1 = self.login.login_func(self.session,mobile,password) # print("响应体:",response1.json()) #断言 self.assertEqual(success,response1.json().get("success")) self.assertEqual(code, response1.json().get("code")) self.assertIn(message, response1.json().get("message")) #登陆成功测试 def test_login_success(self): response2 = self.login.login_func(self.session,"13800000002","123456") self.assertIn("操作成功", response2.json().get("message")) self.assertEqual(True, response2.json().get("success")) self.assertEqual(10000, response2.json().get("code")) token = response2.json().get("data") app.TOKEN = token
class Test_login(unittest.TestCase): # 3.初始化函数 def setUp(self) -> None: # 初始化 session self.session = requests.session() # 初始化 api对象 self.login_obj = Login() # 4.资源卸载函数 def tearDown(self) -> None: # 销毁 session self.session.close() # 5.核心:测试函数 - 登陆 # 5-1.参数化 # 参数化 步骤 3:调用--------------------------------------------- @parameterized.expand(read_json_file()) def test_login(self, mobile, password, success, code, message): print("-" * 100) print("参数化读取的数据:", mobile, password, success, code, message) # 5-2.请求业务 response = self.login_obj.login(self.session, mobile, password) print("登录响应结果:", response.json()) # 5-3.断言业务 self.assertEqual(success, response.json().get("success")) self.assertEqual(code, response.json().get("code")) self.assertIn(message, response.json().get("message")) # 编写登录成功的测试函数 def test_login_success(self): # 1.直接通过提交正向数据发送请求业务 response = self.login_obj.login(self.session, "13800000002", "123456") # 2.断言业务 # 登录成功的结果: {'success': True, 'code': 10000, 'message': '操作成功!', 'data': '2bb071fd-2f96-4047-9296-4de700d62fba'} print("登录成功的结果:", response.json()) self.assertEqual(True, response.json().get("success")) self.assertEqual(10000, response.json().get("code")) self.assertIn("操作成功", response.json().get("message")) # 提取 token token = response.json().get("data") print("登录后响应的token:", token) # 预期允许其他文件调用该 token ,可以扩大 token 的作用域(将 token 赋值给 app 中的全局变量 TOKEN) app.TOKEN = token
class Test_Login(unittest.TestCase): """3 初始化函数""" def setUp(self) -> None: """初始化 session""" self.session = requests.Session() # 初始化api对象 self.login_obj = Login() # """4 资源卸载函数""" def tearDown(self) -> None: self.session.close() # """5 测试函数 - 登陆""" # 5-1参数化 @parameterized.expand(read_json_file()) def test_login(self, mobile, password, success, code, message): print("-" * 50) print("参数化读取的数据", mobile, password, success, code, message) # 5-2请求业务 response = self.login_obj.login(self.session, mobile, password) print("登录响应结果:", response.json()) # 5-3断言业务 self.assertEqual(success, response.json().get("success")) self.assertEqual(code, response.json().get("code")) self.assertIn(message, response.json().get("message")) def test_login_success(self): # 1 直接通过提交正向数据发送请求业务 response = self.login_obj.login(self.session, "13800000002", "123456") # 2断言业务 print("登录成功结果:", response.json()) self.assertEqual(True, response.json().get("success")) self.assertEqual(10000, response.json().get("code")) self.assertIn("操作成功", response.json().get("message")) token = response.json().get("data") print("登录后相应的token:",token) app.TOKEN = token
def setUp(self) -> None: """初始化 session""" self.session = requests.Session() # 初始化api对象 self.login_obj = Login()
def setUp(self) -> None: # 初始化 session self.session = requests.session() # 初始化 api对象 self.login_obj = Login()
def setUp(self) -> None: self.session = requests.Session() self.login_obj = Login()
def setUp(self): self.session = requests.Session() #创建登陆类对象 self.login = Login()