def setUp(self): # 实例化session self.session = requests.Session() # 定义获取验证码的url self.verify_url = "http://localhost/index.php?m=Home&c=User&a=verify" # 定义登陆的url self.login_url = "http://localhost/index.php?m=Home&c=User&a=do_login" # 实例化api中login.py中的LogiinApi类 self.login_api = LoginApi()
class TestLogin(unittest.TestCase): # 前置处理 def setUp(self): self.login_api = LoginApi() # 后置处理 def tearDown(self): pass # 定义测试用例 @parameterized.expand(build_data()) def test01_case001(self, phone, password, status_code): # 处理密码 password = generate_md5(password + app.PEPPER) # 调用登录接口 response = self.login_api.login({ "phone": phone, "password": password, 'grant_type': 'password' }) # 断言 self.assertEqual(status_code, response.status_code) # 获取token if response.status_code == 200: app.TOKEN = "Bearer " + response.json().get("access_token") app.header_data["Authorization"] = app.TOKEN
class TestIHRMLoginParams(unittest.TestCase): # 进行初始化 def setUp(self): self.login_api = LoginApi() def tearDown(self): pass # 定义登录数据文件的路径 filepath = app.BASE_DIR + "/data/login_data.json" @parameterized.expand(read_login_data(filepath)) # 编写登录成功函数 def test01_login(self, case_name, request_body, success, code, message, http_code): # 使用封装的接口调用登录接口,并接收返回的响应数据 response = self.login_api.login(request_body, {"Content-Type": "application/json"}) # 打印响应数据 logging.info("登录的结果为:{}".format(response.json())) assert_common(self, http_code, success, code, message, response)
def setUp(self): self.login_api = LoginApi()
def setUpClass(cls): cls.login_api = LoginApi()
class TestIHRMLogin(unittest.TestCase): # 进行初始化 def setUp(self): self.login_api = LoginApi() def tearDown(self): pass # # 编写登录成功函数 # def test01_login_success(self): # # 使用封装的接口调用登录接口,并接收返回的响应数据 # response = self.login_api.login({"mobile":"13800000002","password":"******"}, # {"Content-Type":"application/json"}) # # 打印响应数据 # logging.info("登录成功的结果为:{}".format(response.json())) # # 断言 # # self.assertEqual(200, response.status_code) # # self.assertEqual(True, response.json().get("success")) # # self.assertEqual(10000, response.json().get("code")) # # self.assertIn("操作成功", response.json().get("message")) # assert_common(self, 200, True, 10000, "操作成功", response) # # # # 实现手机号码为空 # def test02_mobile_is_empty(self): # # 使用封装的接口调用登录接口,并接收返回的响应数据 # response = self.login_api.login({"mobile": "", "password": "******"}, # {"Content-Type": "application/json"}) # # 打印响应数据 # logging.info("手机号码为空的结果为:{}".format(response.json())) # assert_common(self, 200, False, 20001, "用户名或密码错误", response) # # # 手机号码不存在 # def test03_mobile_is_not_exists(self): # # 使用封装的接口调用登录接口,并接收返回的响应数据 # response = self.login_api.login({"mobile": "13900000002", "password": "******"}, # {"Content-Type": "application/json"}) # # 打印响应数据 # logging.info("手机号码不存在的结果为:{}".format(response.json())) # assert_common(self, 200, False, 20001, "用户名或密码错误", response) # # # 密码错误 # def test04_password_is_error(self): # # 使用封装的接口调用登录接口,并接收返回的响应数据 # response = self.login_api.login({"mobile": "13800000002", "password": "******"}, # {"Content-Type": "application/json"}) # # 打印响应数据 # logging.info("密码错误的结果为:{}".format(response.json())) # assert_common(self, 200, False, 20001, "用户名或密码错误", response) # # # 无参 # def test05_params_is_none(self): # # 使用封装的接口调用登录接口,并接收返回的响应数据 # response = self.login_api.login({}, {"Content-Type": "application/json"}) # # 打印响应数据 # logging.info("无参的结果为:{}".format(response.json())) # assert_common(self, 200, False, 20001, "用户名或密码错误", response) # # # 传入Null # def test06_params_is_null(self): # # 使用封装的接口调用登录接口,并接收返回的响应数据 # response = self.login_api.login(None, {"Content-Type": "application/json"}) # # 打印响应数据 # logging.info("传入None的结果为:{}".format(response.json())) # assert_common(self, 200, False, 99999, "抱歉,系统繁忙,请稍后重试!", response) # # # 多参 # def test07_more_params(self): # # 使用封装的接口调用登录接口,并接收返回的响应数据 # response = self.login_api.login({"mobile": "13800000002", "password": "******", "more_params": "1"}, # {"Content-Type": "application/json"}) # # 打印响应数据 # logging.info("多参的结果为:{}".format(response.json())) # assert_common(self, 200, True, 10000, "操作成功", response) # # # 少参-缺少mobile # def test08_less_params_mobile(self): # # 使用封装的接口调用登录接口,并接收返回的响应数据 # response = self.login_api.login({"password": "******", "more_params": "1"}, # {"Content-Type": "application/json"}) # # 打印响应数据 # logging.info("少参-缺少mobile的结果为:{}".format(response.json())) # assert_common(self, 200, False, 20001, "用户名或密码错误", response) # # # 少参-缺少Passowrd # def test09_less_password(self): # # 使用封装的接口调用登录接口,并接收返回的响应数据 # response = self.login_api.login({"mobile": "13800000002", "more_params": "1"}, # {"Content-Type": "application/json"}) # # 打印响应数据 # logging.info("少参-缺少Passowrd的结果为:{}".format(response.json())) # assert_common(self, 200, False, 20001, "用户名或密码错误", response) # # # 错误参数 # def test10_params_is_error(self): # # 使用封装的接口调用登录接口,并接收返回的响应数据 # response = self.login_api.login({"mboile": "13800000002", "password": "******", "more_params": "1"}, # {"Content-Type": "application/json"}) # # 打印响应数据 # logging.info("错误参数的结果为:{}".format(response.json())) # assert_common(self, 200, False, 20001, "用户名或密码错误", response) # # # 密码为空 # def test11_password_is_empty(self): # # 使用封装的接口调用登录接口,并接收返回的响应数据 # response = self.login_api.login({"mobile": "13800000002", "password": "", "more_params": "1"}, # {"Content-Type": "application/json"}) # # 打印响应数据 # logging.info("密码为空的结果为:{}".format(response.json())) # assert_common(self, 200, False, 20001, "用户名或密码错误", response) # 定义登录数据文件的路径 filepath = app.BASE_DIR + "/data/login.json" @parameterized.expand(read_login_data(filepath)) # 编写登录成功函数 def test01_login(self,case_name, request_body, success,code,message,http_code): # 使用封装的接口调用登录接口,并接收返回的响应数据 response = self.login_api.login(request_body, {"Content-Type": "application/json"}) # 打印响应数据s logging.info("登录的结果为:{}".format(response.json())) assert_common(self, http_code, success, code, message, response)
def setUpClass(cls) -> None: # 初始化LoginApi cls.login_api = LoginApi()
def setUp(self): # 实例化登录 self.login_api = LoginApi() # 实例化员工 self.dep_api = DepartmentApi()
class TestIHRMEmployeeParams(unittest.TestCase): # 初始化unittest的函数 def setUp(self): # 实例化登录 self.login_api = LoginApi() # 实例化员工 self.dep_api = DepartmentApi() def tearDown(self): pass # 实现登录成功的接口 def test01_login_success(self): # 发送登录的接口请求 jsonData = {"mobile": "13800000002", "password": "******"} response = self.login_api.login(jsonData, {"Content-Type": "application/json"}) # 打印登录接口返回的结果 logging.info("登录接口返回的结果为:{}".format(response.json())) # 提取登录返回的令牌 token = 'Bearer ' + response.json().get('data') # 把令牌拼接成HEADERS并保存到全局变量HEADERS app.HEADERS = { "Content-Type": "application/json", "Authorization": token } # 打印请求头 logging.info("保存到全局变量中的请求头为:{}".format(app.HEADERS)) # 断言 assert_common(self, 200, True, 10000, "操作成功", response) # 定义员工模块的文件路径 dep_filepath = app.BASE_DIR + "/data/dep_data.json" # 参数化 @parameterized.expand(read_dep_data(dep_filepath, 'add_dep')) def test02_add_dep(self, depname1, depcode1, magname1, intoinfo1, success, code, message, http_code): logging.info("app.HEADERS的值是:{}".format(app.HEADERS)) # 发送添加员工的接口请求 response = self.dep_api.add_dep(depname1, depcode1, magname1, intoinfo1, app.HEADERS) # 打印添加员工的结果 logging.info("添加员工的结果为:{}".format(response.json())) # 提取员工中的令牌并把员工令牌保存到全局变量中 app.DEP_ID = response.json().get("data").get("id") # 打印保存的员工ID logging.info("保存到全局变量的员工的ID为:{}".format(app.DEP_ID)) # 断言 assert_common(self, http_code, success, code, message, response) @parameterized.expand(read_dep_data(dep_filepath, "query_dep")) def test03_query_dep(self, success, code, message, http_code): # 发送查询员工的接口请求: response = self.dep_api.query_dep(app.DEP_ID, app.HEADERS) # 打印查询员工的数据 logging.info("查询员工的结果为:{}".format(response.json())) # 断言 assert_common(self, http_code, success, code, message, response) @parameterized.expand(read_dep_data(dep_filepath, "modify_dep")) def test04_modify_dep(self, depname2, depcode2, success, code, message, http_code): # 调用封装的修改员工接口,发送接口请求 response = self.dep_api.modify_dep(app.DEP_ID, depname2, depcode2, app.HEADERS) # 打印数据 logging.info("修改员工的结果为:{}".format(response.json())) # 断言 assert_common(self, http_code, success, code, message, response) @parameterized.expand(read_dep_data(dep_filepath, "delete_dep")) def test05_delete_dep(self, success, code, message, http_code): # 调用封装的删除员工接口哦,发送接口请求 response = self.dep_api.delete_dep(app.DEP_ID, app.HEADERS) # 打印删除员工的结果为 logging.info("删除员工的结果为:{}".format(response.json())) # 断言 assert_common(self, http_code, success, code, message, response)
def setUp(self): # 实例化登录 self.login_api = LoginApi() # 实例化员工 self.emp_api = EmployeeApi()
def setUpClass(cls) -> None: # 初始化登陆 cls.login_api = LoginApi() # 初始化员工 cls.employee_api = EmployeeApi()
class TestTpshopLogin(unittest.TestCase): # 初始化函数 def setUp(self): # 实例化session self.session = requests.Session() # 定义获取验证码的url self.verify_url = "http://localhost/index.php?m=Home&c=User&a=verify" # 定义登陆的url self.login_url = "http://localhost/index.php?m=Home&c=User&a=do_login" # 实例化api中login.py中的LogiinApi类 self.login_api = LoginApi() def tearDown(self): # 关闭session if self.session: self.session.close() # 测试登录成功 def test01_login_success(self): # 发送获取验证码接口请求 response_verify = self.login_api.get_verify(self.session, url=self.verify_url) # 断言验证码 self.assertEqual("image/png", response_verify.headers.get("Content-Type")) response_login = self.login_api.login( self.session, self.login_url, { "username": "******", "password": "******", "verify_code": "8888" }, ) print(response_login.json()) # 断言tpshop登录响应数据:响应状态码,status,msg self.assertEqual(200, response_login.status_code) self.assertEqual(1, response_login.json().get("status")) self.assertEqual("登陆成功", response_login.json().get("msg")) def test02_username_is_not_exists(self): # 发送获取验证码接口请求 response_verify = self.login_api.get_verify(self.session, self.verify_url) # 断言验证码 self.assertEqual("image/png", response_verify.headers.get("Content-Type")) # 发送tpshop登录接口请求 response_login = self.login_api.login(self.session, url=self.login_url, data={ "username": "******", "password": "******", "verify_code": "8888" }) # 输出tpshop登录的接口结果 print(response_login.json()) # 断言tpsho登录响应数据:响应状态码。status,msg self.assertEqual(200, response_login.status_code) self.assertEqual(-1, response_login.json().get("status")) self.assertEqual("账号不存在!", response_login.json().get("msg")) def test03_password_is_error(self): # 发送获取验证码接口请求 response_verify = self.login_api.get_verify(self.session, url=self.verify_url) # 断言验证码 self.assertEqual("image/png", response_verify.headers.get("Content-Type")) # 发送tpshop登录接口请求 response_login = self.login_api.login(self.session, url=self.login_url, data={ "username": "******", "password": "******", "verify_code": "8888" }) # 输出tpshop登录接口结果 print(response_login.json()) # 断言tpshop登录响数据:响应状态码, status, msg self.assertEqual(200, response_login.status_code) self.assertEqual(-2, response_login.json().get("status")) self.assertEqual("密码错误!", response_login.json().get("msg"))