class TestEmployee(unittest.TestCase): # 初始化unittest函数 def setUp(self): # 实例化EmployeeApi self.emp_api = EmployeeApi() def tearDown(self): pass def test01_login_success(self): # 调用登陆 response = self.emp_api.login("13800000002", "123456") # 打印登陆结果 logging.info("员工模块的登陆结果为:{}".format(response.json())) # 取出令牌,并拼接成以Bearer开头的字符串 token = "Bearer " + response.json().get('data') logging.info("取出的令牌为:{}".format(token)) # 设置员工模块所需要的请求头 headers = {"Content-Type": "application/json", "Authorization": token} # 保存请求头到app.py中的HEADERS app.HEADERS = headers self.HEADERS = headers logging.info("员工模块请求头为:{}".format(app.HEADERS)) @parameterized.expand(read_add_emp) def test02_add_emp(self, username, mobile, http_code, success, code, message): logging.info("保存在类方法的属性值{}".format(app.HEADERS)) # 调用添加员工 response_add_emp = self.emp_api.add_emp(username, mobile, app.HEADERS) logging.info("添加员工接口的结果为:{}".format(response_add_emp.json())) # 断言结果:响应状态码,success,code,message assert_common_utils(self, response_add_emp, http_code, success, code, message) # 由于添加员工成功后,还需要保存员工ID给后续的查询、修改、删除员工使用,所以我们需要保存员工ID emp_id = response_add_emp.json().get("data").get("id") # 保存员工ID到app.py的EMPID中 app.EMPID = emp_id logging.info("保存的员工ID为:{}".format(emp_id)) @parameterized.expand(read_query_emp) def test03_query_emp(self, http_code, success, code, message): response_query = self.emp_api.query_emp(app.EMPID, headers=app.HEADERS) logging.info("查询员工的结果为:{}".format(response_query.json())) # 断言结果:响应状态码,success,code,message assert_common_utils(self, response_query, http_code, success, code, message) @parameterized.expand(read_modify_emp) def test04_modify_emp(self, username, http_code, success, code, message): # 调用修改员工 response_modify = self.emp_api.modify_emp(app.EMPID, username, headers=app.HEADERS) logging.info("修改员工结果为:{}".format(response_modify.json())) # 建立连接 username:readuser # password:iHRM_user_2019 with DBUtils() as db: # 执行SQL语句 # 根据添加员工返回的id查询数据库中员工表的username,这样就能获取到修改之后的数据 sql = "select username from bs_user where id = {};".format(app.EMPID) logging.info("要查询的SQL语句为:{}".format(sql)) # 执行查询的sql语句 db.execute(sql) # 获取返回结果 result = db.fetchone() logging.info("SQL查询出来的结果为:{}".format(result)) # ('new_tom',) # 断言修改结果是否正确 # 注意:如果是用fetchall()取出的数据,那么取出result时,需要有两个下表result[0][0] self.assertEqual("new_tom", result[0]) # 断言结果:响应状态码,success,code,message assert_common_utils(self, response_modify, http_code, success, code, message) @parameterized.expand(read_delete_emp) def test05_delete_emp(self, http_code, success, code, message): # 调用删除员工 response_delete = self.emp_api.delete_emp(app.EMPID, headers=app.HEADERS) logging.info("删除员工的结果为:{}".format(response_delete.json())) # 断言结果:响应状态码,success,code,message assert_common_utils(self, response_delete, http_code, success, code, message)
class TestEmployee(unittest.TestCase): # 初始化unittest的函数 def setUp(self): # 实例化EmployeeApi self.emp_api = EmployeeApi() def tearDown(self): pass # # 创建测试函数 # def test01_emp_management(self): # # 调用登陆 # response = self.emp_api.login("13800000002", "123456") # # 打印登陆结果 # logging.info("员工模块的登陆结果为:{}".format(response.json())) # # 取出令牌,并拼接成以Bearer 开头的字符串 # token = "Bearer " + response.json().get('data') # logging.info("取出的令牌为:{}".format(token)) # # # 设置员工模块所需要的请求头 # headers = {"Content-Type": "application/json", "Authorization": token} # logging.info("员工模块请求头为:{}".format(headers)) # # # 调用添加员工 # response_add_emp = self.emp_api.add_emp("王健林sup99perstar95374", # "16782323675", # headers) # logging.info("添加员工接口的结果为:{}".format(response_add_emp.json())) # # 断言结果:响应状态码,success,code,message # assert_common_utils(self, response_add_emp, 200, True, 10000,"操作成功") # # # 由于添加员工成功后,还需要保存员工ID给后续的查询、修改、删除员工使用,所以我们需要保存员工ID # emp_id = response_add_emp.json().get("data").get("id") # logging.info("保存的员工ID为:{}".format(emp_id)) # # # 调用查询员工 # response_query = self.emp_api.query_emp(emp_id, headers=headers) # logging.info("查询员工的结果为:{}".format(response_query.json())) # # 断言结果:响应状态码,success,code,message # assert_common_utils(self, response_query, 200, True, 10000, "操作成功") # # # 调用修改员工 # response_modify = self.emp_api.modify_emp(emp_id, "new_tom", headers=headers) # logging.info("修改员工结果为:{}".format(response_modify.json())) # # 断言结果:响应状态码,success,code,message # assert_common_utils(self, response_modify, 200, True, 10000, "操作成功") # # # # 调用删除员工 # response_delete = self.emp_api.delete_emp(emp_id, headers=headers) # logging.info("删除员工的结果为:{}".format(response_delete.json())) # # 断言结果:响应状态码,success,code,message # assert_common_utils(self, response_delete, 200, True, 10000, "操作成功") def test02_login_success(self): # 调用登陆 response = self.emp_api.login("13800000002", "123456") # 打印登陆结果 logging.info("员工模块的登陆结果为:{}".format(response.json())) # 取出令牌,并拼接成以Bearer 开头的字符串 token = "Bearer " + response.json().get('data') logging.info("取出的令牌为:{}".format(token)) # 设置员工模块所需要的请求头 headers = {"Content-Type": "application/json", "Authorization": token} # 保存请求头到app.py中的HEADERS中 app.HEADERS = headers logging.info("员工模块请求头为:{}".format(app.HEADERS)) def test03_add_emp(self): # 调用添加员工 response_add_emp = self.emp_api.add_emp("王健林sup99perstar35376", "16732323666", app.HEADERS) logging.info("添加员工接口的结果为:{}".format(response_add_emp.json())) # 断言结果:响应状态码,success,code,message assert_common_utils(self, response_add_emp, 200, True, 10000, "操作成功") # 由于添加员工成功后,还需要保存员工ID给后续的查询、修改、删除员工使用,所以我们需要保存员工ID emp_id = response_add_emp.json().get("data").get("id") # 保存员工ID到app.py的EMPID中 app.EMPID = emp_id logging.info("保存的员工ID为:{}".format(app.EMPID)) def test04_query_emp(self): # 调用查询员工 response_query = self.emp_api.query_emp(app.EMPID, headers=app.HEADERS) logging.info("查询员工的结果为:{}".format(response_query.json())) # 断言结果:响应状态码,success,code,message assert_common_utils(self, response_query, 200, True, 10000, "操作成功") def test05_modify_emp(self): # 调用修改员工 response_modify = self.emp_api.modify_emp(app.EMPID, "new_tom", headers=app.HEADERS) logging.info("修改员工结果为:{}".format(response_modify.json())) # 断言结果:响应状态码,success,code,message assert_common_utils(self, response_modify, 200, True, 10000, "操作成功") def test06_delete_emp(self): # 调用删除员工 response_delete = self.emp_api.delete_emp(app.EMPID, app.HEADERS) logging.info("删除员工的结果为:{}".format(response_delete.json())) # 断言结果:响应状态码,success,code,message assert_common_utils(self, response_delete, 200, True, 10000, "操作成功")
class TestEmployee(unittest.TestCase): # 初始化unittest的函数 def setUp(self): # 实例化EmployeeApi self.emp_api = EmployeeApi() def tearDown(self): pass # # 创建测试函数 # def test01_emp_management(self): # # 调用登陆 # response = self.emp_api.login("13800000002", "123456") # # 打印登陆结果 # logging.info("员工模块的登陆结果为:{}".format(response.json())) # # 取出令牌,并拼接成以Bearer 开头的字符串 # token = "Bearer " + response.json().get('data') # logging.info("取出的令牌为:{}".format(token)) # # # 设置员工模块所需要的请求头 # headers = {"Content-Type": "application/json", "Authorization": token} # logging.info("员工模块请求头为:{}".format(headers)) # # # 调用添加员工 # response_add_emp = self.emp_api.add_emp("王健林sup99perstar95374", # "16782323675", # headers) # logging.info("添加员工接口的结果为:{}".format(response_add_emp.json())) # # 断言结果:响应状态码,success,code,message # assert_common_utils(self, response_add_emp, 200, True, 10000,"操作成功") # # # 由于添加员工成功后,还需要保存员工ID给后续的查询、修改、删除员工使用,所以我们需要保存员工ID # emp_id = response_add_emp.json().get("data").get("id") # logging.info("保存的员工ID为:{}".format(emp_id)) # # # 调用查询员工 # response_query = self.emp_api.query_emp(emp_id, headers=headers) # logging.info("查询员工的结果为:{}".format(response_query.json())) # # 断言结果:响应状态码,success,code,message # assert_common_utils(self, response_query, 200, True, 10000, "操作成功") # # # 调用修改员工 # response_modify = self.emp_api.modify_emp(emp_id, "new_tom", headers=headers) # logging.info("修改员工结果为:{}".format(response_modify.json())) # # 断言结果:响应状态码,success,code,message # assert_common_utils(self, response_modify, 200, True, 10000, "操作成功") # # # # 调用删除员工 # response_delete = self.emp_api.delete_emp(emp_id, headers=headers) # logging.info("删除员工的结果为:{}".format(response_delete.json())) # # 断言结果:响应状态码,success,code,message # assert_common_utils(self, response_delete, 200, True, 10000, "操作成功") def test02_login_success(self): # 调用登陆 response = self.emp_api.login("13800000002", "123456") # 打印登陆结果 logging.info("员工模块的登陆结果为:{}".format(response.json())) # 取出令牌,并拼接成以Bearer 开头的字符串 token = "Bearer " + response.json().get('data') logging.info("取出的令牌为:{}".format(token)) # 设置员工模块所需要的请求头 headers = {"Content-Type": "application/json", "Authorization": token} # 保存请求头到app.py中的HEADERS中 app.HEADERS = headers self.HEADERS = headers logging.info("员工模块请求头为:{}".format(app.HEADERS)) def test03_add_emp(self): logging.info("保存在类方法的属性值{}".format(app.HEADERS)) # 调用添加员工 response_add_emp = self.emp_api.add_emp("王健林sup99perstar35376", "16732323666", app.HEADERS) logging.info("添加员工接口的结果为:{}".format(response_add_emp.json())) # 断言结果:响应状态码,success,code,message assert_common_utils(self, response_add_emp, 200, True, 10000, "操作成功") # 由于添加员工成功后,还需要保存员工ID给后续的查询、修改、删除员工使用,所以我们需要保存员工ID emp_id = response_add_emp.json().get("data").get("id") # 保存员工ID到app.py的EMPID中 app.EMPID = emp_id logging.info("保存的员工ID为:{}".format(app.EMPID)) def test04_query_emp(self): # 调用查询员工 response_query = self.emp_api.query_emp(app.EMPID, headers=app.HEADERS) logging.info("查询员工的结果为:{}".format(response_query.json())) # 断言结果:响应状态码,success,code,message assert_common_utils(self, response_query, 200, True, 10000, "操作成功") def test05_modify_emp(self): # 调用修改员工 response_modify = self.emp_api.modify_emp(app.EMPID, "new_tom", headers=app.HEADERS) logging.info("修改员工结果为:{}".format(response_modify.json())) # 建立连接 username:readuser # password:iHRM_user_2019 with DBUtils() as db: # 执行SQL语句 # 根据添加员工返回的id查询数据库中员工表的username,这样就能获取到修改之后的数据 sql = "select username from bs_user where id = {};".format( app.EMPID) logging.info("要查询的SQL语句为:{}".format(sql)) # 执行查询的sql语句 db.execute(sql) # 获取返回结果 result = db.fetchone() logging.info("SQL查询出来的结果为:{}".format(result)) # ('new_tom',) # 断言修改结果是否正确 # 注意:如果是用fetchall()取出的数据,那么取出result时,需要有两个下标result[0][0] self.assertEqual("new_tom", result[0]) # 断言结果:响应状态码,success,code,message assert_common_utils(self, response_modify, 200, True, 10000, "操作成功") def test06_delete_emp(self): # 调用删除员工 response_delete = self.emp_api.delete_emp(app.EMPID, app.HEADERS) logging.info("删除员工的结果为:{}".format(response_delete.json())) # 断言结果:响应状态码,success,code,message assert_common_utils(self, response_delete, 200, True, 10000, "操作成功")
class TestEmployee(unittest.TestCase): def setUp(self): # 实例化对象 self.emp_api = EmployeeApi() # 1.登录 def test01_login_success(self): response = self.emp_api.login("13800000002", "123456") # 获取令牌 token = "Bearer " + response.json().get("data") # 设置请求头,令牌放入请求头中 headers = {"Content-Type": "application/json", "Authorization": token} app.Header = headers # 1. 添加员工 # 数据驱动 emp_data = read_add_emp(app.absolute_path + "/data/emp.json") @parameterized.expand(emp_data) def test02_add_emp(self, username, mobile, status_code, success, code, message): add_emp_info = [username, mobile] response_add = self.emp_api.add_emp(add_emp_info[0], add_emp_info[1], app.Header) # 断言 assert_fn(self, response_add, status_code, success, code, message) print(response_add.json()) # 获取新增员工ID emp_id = response_add.json().get("data").get("id") app.Emp_id = emp_id # 2. 查询员工 emp_data = read_search_emp(app.absolute_path + "/data/emp.json") @parameterized.expand(emp_data) def test03_search_emp(self, status_code, success, code, message): response_search = self.emp_api.search_emp(app.Emp_id, app.Header) # 断言 assert_fn(self, response_search, status_code, success, code, message) print(response_search.json()) # 与数据库比对--调用utils里面的方法 with DbUtils("182.92.81.159", "readuser", "iHRM_user_2019", "ihrm") as db: sql = "select username from bs_user where id = {}".format(app.Emp_id) db.execute(sql) result = db.fetchone() self.assertEqual("jojo", result[0]) # 3.修改员工 emp_data = read_modify_emp(app.absolute_path + "/data/emp.json") @parameterized.expand(emp_data) def test04_modify_emp(self, username, status_code, success, code, message): response_modify = self.emp_api.modify_emp(app.Emp_id, username, app.Header) # 断言 assert_fn(self, response_modify, status_code, success, code, message) print(response_modify.json()) # 与数据库比对 with DbUtils("182.92.81.159", "readuser", "iHRM_user_2019", "ihrm") as db: sql = "select username from bs_user where id = {}".format(app.Emp_id) db.execute(sql) result = db.fetchone() self.assertEqual("new_jojo", result[0]) # 4.删除员工 emp_data = read_delete_emp(app.absolute_path + "/data/emp.json") @parameterized.expand(emp_data) def test05_delete_emp(self, status_code, success, code, message): response_delete = self.emp_api.delete_emp(app.Emp_id, app.Header) # 断言 assert_fn(self, response_delete, status_code, success, code, message) print(response_delete.json()) # 与数据库比对 with DbUtils("182.92.81.159", "readuser", "iHRM_user_2019", "ihrm") as db: sql = "select * from bs_user where id = {}".format(app.Emp_id) db.execute(sql) result = db.fetchone() self.assertEqual(None, result)
class TestEmployee(unittest.TestCase): # 初始化unittest的函数 def setUp(self): # 实例化EmployeeApi self.emp_handle = EmployeeApi() def tearDown(self): pass def test_01_login(self): # 调用登陆 response = self.emp_handle.login("13800000002", "123456") # 打印登陆结果 logging.info("员工模块的登陆结果为:{}".format(response.json())) # 取出令牌,并拼接成以Bearer 开头的字符串 token = "Bearer " + response.json().get('data') logging.info("取出的令牌为:{}".format(token)) # 设置员工模块所需要的请求头 headers = {"Content-Type": "application/json", "Authorization": token} # 保存请求头到app.py中的HEADERS中 app.HEADERS = headers self.HEADERS = headers logging.info("员工模块请求头为:{}".format(self.HEADERS)) def test_02_add_emp(self): # 调用添加员工 response_add_emp = self.emp_handle.add_emp("王健林suar35376", "16732323667", app.HEADERS) logging.info("添加员工接口的结果为:{}".format(response_add_emp.json())) # 断言:响应状态码,success,code,message assert_utils(self, response_add_emp, 200, True, 10000, "操作成功") # 由于添加员工成功后,还需要保存员工ID给后续的查询、修改、删除员工使用,所以我们需要保存员工ID emp_id = response_add_emp.json().get("data").get("id") app.EMPID = emp_id logging.info("保存的员工ID为:{}".format(app.EMPID)) def test_03_query_emp(self): # 调用查询员工 response_query = self.emp_handle.query_emp(app.EMPID, headers=app.HEADERS) logging.info("查询员工的结果为:{}".format(response_query.json())) # 断言:响应状态码,success,code,message assert_utils(self, response_query, 200, True, 10000, "操作成功") def test_04_modify_emp(self): # 调用修改员工 response_modify = self.emp_handle.modify_emp(app.EMPID, "new_tom", headers=app.HEADERS) logging.info("修改员工结果为:{}".format(response_modify.json())) # 建立连接 username:readuser # password:iHRM_user_2019 conn = pymysql.connect(host='182.92.81.159', user='******', password='******', database='ihrm') # 获取游标 cursor = conn.cursor() # 执行SQL语句 # 根据添加员工返回的id查询数据库中员工表的username,这样就能获取到修改之后的数据 sql = "select username from bs_user where id = {}".format(app.EMPID) logging.info("要查询的SQL语句为:{}".format(sql)) # 执行查询的sql语句 cursor.execute(sql) # 获取返回结果 result = cursor.fetchall() logging.info("SQL查询出来的结果为:{}".format(result)) # ('new_tom',) # 断言修改结果是否正确 # 注意:如果是用fetchall()取出的数据,那么取出result时,需要有两个下表result[0][0] self.assertEqual("new_tom", result[0][0]) # 关闭连接 cursor.close() # 关闭游标 conn.close() # 断言:响应状态码,success,code,message assert_utils(self, response_modify, 200, True, 10000, "操作成功") def test_05_delete_emp(self): # 调用删除员工 response_delete = self.emp_handle.delete_emp(app.EMPID, app.HEADERS) logging.info("删除员工的结果为:{}".format(response_delete.json())) # 断言:响应状态码,success,code,message assert_utils(self, response_delete, 200, True, 10000, "操作成功")