class TestUser(unittest.TestCase): # 初始化函数 def setUp(self): # 初始化session self.session = requests.Session() # 初始化 api 对象 self.user_obj = UserLogin() # 资源销毁函数 def tearDown(self): # 销毁 session self.session.close() # 测试函数: 登录 # 使用参数化 @parameterized.expand(read_json()) def test_login(self, mobile, password, success, code, message): print("-" * 100) print(mobile, password, success, code, message) # 1.调用请求业务 # response = 初始化api对象.登录() response = self.user_obj.login(self.session, mobile, password) # 2.调用断言业务 print(response.json()) result = response.json() self.assertEqual(success, result.get("success")) self.assertEqual(code, result.get("code")) self.assertIn(message, result.get("message")) # 测试函数: 只实现登录成功 def test_login_success(self): print("-" * 100) print("登录成功接口") # 1.请求业务 response = self.user_obj.login(self.session, "13800000002", "123456") # 2.断言业务 result = response.json() print("登录成功的响应结果:", result) self.assertEqual(True, result.get("success")) self.assertEqual(10000, result.get("code")) self.assertIn("操作成功", result.get("message")) # 提取token值 token = result.get("data") print("test_login_success中登录成功后token值为 %s:" % token) app.TOKEN = token # 变局部变量为全局变量 print("*" * 100) print("test_login_success中登录成功后传给app.TOKEN值为 %s:" % app.TOKEN)
class TestUser(unittest.TestCase): # 初始化函数 def setUp(self): # 初始化session self.session = requests.Session() # 创建UserLogin() self.user_obj = UserLogin() # 资源销毁函数 def tearDown(self): # 销毁session self.session.close() def test_get_verify_code(self): # 1.请求业务 # 调用get_verify_code函数 response = self.user_obj.get_verify_code(self.session) # 2.断言业务 self.assertEqual(200, response.status_code) self.assertIn("image", response.headers.get("Content-Type")) @parameterized.expand(read_json()) def test_login(self, username, password, verify_code, status, msg): # 1.请求业务 # 调用get_verify_code函数 response1 = self.user_obj.get_verify_code(self.session) # 调用login函数 response2 = self.user_obj.login(self.session, username, password, verify_code) # 2.断言业务 print(response2.json()) self.assertEqual(status, response2.json().get("status")) self.assertIn(msg, response2.json().get("msg"))
class TestUser(unittest.TestCase): # 初始化函数 def setUp(self): # 初始化session self.session = requests.Session() # 初始化 api 对象 self.user_obj = UserLogin() # 资源销毁函数 def tearDown(self): # 销毁session self.session.close() # 测试函数1:登录 # 使用参数化 def test_login(self, session, mobile, password): # 1.调用请求业务 # response = 初始化api对象.登录() response = self.user_obj.login(self.session, mobile, password) # 2.调用断言业务 print(response.json()) result = response.json() self.assertEqual(successe, result.get("success")) # 测试函数2:只实现登录成功 def test_login_success(self): print("-" * 100) print("登录成功的接口:") # 1.请求业务 response = self.user_obj.login(self.session, "13800000002", "123456") print(response) # 2.断言业务 result = response.json() print("登录成功的响应结果", result) self.assertEqual(True, result.get("success")) self.assertEqual(10000, result.get("code")) self.assertIn("操作成功", result.get("message")) # 获取响应结果的token token = response.json().get("data") app.TOKEN = token print("-------登录成功后的token值为:%s" % app.TOKEN)
class TestUser(unittest.TestCase): def setUp(self): self.session = requests.Session() # 创建UserLogin对象 self.userlogin = UserLogin() def tearDown(self): self.session.close() # 测试函数1:获取验证码 def test_get_verify_code(self): # 1.请求业务 # 调用get_verify_code函数 response = self.userlogin.get_verify_code(self.session) # 2.断言 self.assertEqual(200, response.status_code) self.assertIn("image", response.headers.get("Content-Type")) # 创建函数2 def test_Login(self): # 1.请求业务 respones = self.userlogin.get_verify_code(self.session) respones2 = self.userlogin.login(self.session, "13712273958", "123456", "8888") # 2.断言 print(respones2.json()) @parameterized.expand(read_json()) def test_Login_red(self, username, password, verify_code, status, msg): print("-" * 100) print(username, password, verify_code, status, msg) response1 = self.userlogin.get_verify_code(self.session) response2 = self.userlogin.login(self.session, username, password, verify_code) self.assertEqual(status, response2.json().get("status")) self.assertIn(msg, response2.json().get("msg"))
class TestIHRMUser(unittest.TestCase): # 初始化函数 def setUp(self): self.session = requests.Session() # 实例化api对象 self.api = UserLogin() # 关闭函数 def tearDown(self): self.session.close() # 测试函数登录 @parameterized.expand(read_json()) def test_login(self, mobile, password, success, code, message): # 登录请求业务 response = self.api.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): print("登陆成功接口") # 请求业务 response1 = self.api.login(self.session, "13800000002", "123456") # 断言业务 result = response1.json() print("登陆成功的响应结果", result) self.assertEqual(True, result.get("success")) self.assertEqual(10000, result.get("code")) self.assertIn("操作成功", result.get("message")) token = result.get("data") print("登陆成功后的token值", token) app.TOKEN = token
class TestUserLogin(unittest.TestCase): def setUp(self): self.session = requests.Session() self.user_obj = UserLogin() def tearDown(self): self.session.close() def test_login_success(self): response = self.user_obj.login(self.session, "13800000002", "123456") print("登录成功的响应结果:", response.json()) result = response.json() self.assertEqual(True, result.get("success")) self.assertEqual(10000, result.get("code")) self.assertIn("成功", result.get("message")) @parameterized.expand(read_from_json()) def test_login(self, mobile, password, success, code, message): response = self.user_obj.login(self.session, mobile, password) result = response.json() self.assertEqual(success, result.get("success")) self.assertEqual(code, result.get("code")) self.assertIn(message, result.get("message"))
class TestUserLogin(unittest.TestCase): #初始化函数 def setUp(self): self.session = requests.Session() self.user_obj = UserLogin() #销毁函数 def tearDown(self): self.session.close() #测试函数:登录成功 def test_login_success(self): # 请求业务 response = self.user_obj.login(self.session, "13800000002", "123456") # 断言业务 print(response.json()) result = response.json() self.assertEqual(True, result.get("success")) self.assertEqual(10000, result.get("code")) self.assertIn("成功", result.get("message")) app.TOKEN = result.get("data") #参数化实现的测试函数 @parameterized.expand(read_from_json()) def test_login(self, mobile, password, success, code, message): print("-" * 100) print("解析的数据:", mobile, password, success, code, message) # 请求业务 response = self.user_obj.login(self.session, mobile, password) # 断言业务 print(response.json()) result = response.json() self.assertEqual(success, result.get("success")) self.assertEqual(code, result.get("code")) self.assertIn(message, result.get("message"))
class TestUser(unittest.TestCase): #初始化函数 def setUp(self): #初始化 session self.session = requests.Session() # 创建 UserLogin 对象 self.user_obj = UserLogin() #资源销毁函数 def tearDown(self): #销毁 session self.session.close() #测试函数1: 获取验证码 def test_get_verify_code(self): # 1.请求业务 # 调用 get_verify_code 函数 response = self.user_obj.get_verify_code(self.session) # 2.断言业务 self.assertEqual(200,response.status_code) self.assertIn("image",response.headers.get("Content-Type")) #测试函数2: 登录 def test_login_success(self): # 1.请求业务 # 调用 get_verify_code 函数 response1 = self.user_obj.get_verify_code(self.session) # 调用 login 函数 response2 = self.user_obj.login(self.session, "13012345678","123456","8888") # 2.断言业务 print(response2.json()) #测试函数3: 以参数化的方式读取测试数据然后执行登录 """ 参数化:动态(程序驱动代替人工驱动)的生成或导入数据 流程: 1.导包 from parameterized import parameterized 2.定义一个获取数据的函数 def read_json(): # ..... return [("13012345678","123456","8888",1,"登陆成功"),("13012345679","123456","8888",-1,"账号有误"),("13012345678","123457","8888",-2,"密码错误")] 3.测试函数声明 @parameterized.expand(函数) 将数据导入测试函数 """ @parameterized.expand(read_json()) def test_login(self,username,password,verify_code,status,msg): print("-"*100) print(username,password,verify_code,status,msg) # 1.请求业务 # 调用 api 包下对象的相关函数 # 1.1 现获取验证码 response1 = self.user_obj.get_verify_code(self.session) # 1.2 再执行登录 response2 = self.user_obj.login(self.session,username,password,verify_code) # 2.断言业务 print(response2.json()) self.assertEqual(status,response2.json().get("status")) self.assertIn(msg,response2.json().get("msg"))