class TestEmp(unittest.TestCase): def setUp(self): self.login_api = EmpApi() def tearDown(self): pass def test01_login_emp_case(self): app.init_logger() reponse = self.login_api.login_auto("13800000002","123456") logging.info(reponse.json()) token = "Bearer " + reponse.json().get("data") logging.info("id:{}".format(token)) #设置请求头 headers = {"Content-Type":"application/json","Authorization":token} app.HEADERS = headers #调用添加员工接口 @parameterized.expand(add_emp) def test02_add_emp(self,username,mobile,http_code,success,code,message): reponse_add_emp = self.login_api.add_emp(username,mobile,app.HEADERS) logging.info("添加员工结果为:{}".format(reponse_add_emp.json())) assert_conmm_utils(self,reponse_add_emp,http_code,success,code,message) emp_id = reponse_add_emp.json().get("data").get("id") app.EMP_ID = emp_id logging.info("保存的员工id:{}".format(reponse_add_emp.json())) #查询员工 @parameterized.expand(index_emp) def test03_index_emp(self,http_code,success,code,message): reponse_index_emp = self.login_api.index_emp(app.EMP_ID,headers=app.HEADERS) logging.info("查询员工结果为:{}".format(reponse_index_emp.json())) assert_conmm_utils(self,reponse_index_emp,http_code,success,code,message) #修改员工 @parameterized.expand(put_emp) def test04_put_emp(self,username,http_code,success,code,message): reponse_set_emp =self.login_api.put_emp(app.EMP_ID,username,headers=app.HEADERS) logging.info("修改员工结果为:{}".format(reponse_set_emp.json())) #连接数据库 with SqlUtils() as db: # conn = pymysql.connect(host="182.92.81.159",user="******",password="******",database="ihrm",) # cursor = conn.cursor() sql = "select username from bs_user where id={};".format(app.EMP_ID) db.execute(sql) logging.info("要查询的语句为:{}".format(sql)) result = db.fetchone() logging.info("查询结果为:{}".format(result)) self.assertEqual(username,result[0]) assert_conmm_utils(self,reponse_set_emp, http_code,success,code,message) #删除员工 @parameterized.expand(delete_emp) def test05_delete_emp(self,http_code,success,code,message): reponse_delete_emp =self.login_api.delete_emp(app.EMP_ID,headers=app.HEADERS) logging.info("删除员工结果为:{}".format(reponse_delete_emp.json())) assert_conmm_utils(self,reponse_delete_emp,http_code,success,code,message)
class TestEmp(unittest.TestCase): # 初始化 def setUp(self): # 实例化封装的登录接口 self.login_api = LoginApi() # 实例化封装的员工接口 self.emp_api = EmpApi() # 定义员工模块的URL self.emp_url = "http://182.92.81.159" + "/api/sys/user" def tearDown(self): pass # 编写测试员工增删改查的案例 def test01_login(self): # 1 实现登录接口 response = self.login_api.login({"mobile": "13800000002", "password": "******"}, app.HEADERS) # 获取登录接口返回的json数据 result = response.json() # 输出登录的结果 logging.info("员工模块登录接口的结果为:{}".format(result)) # 把令牌提取出来,并保存到请求头当中 token = result.get("data") app.HEADERS = {"Content-Type": "application/json", "Authorization": "Bearer " + token} logging.info("登录成功后设置的请求头为:{}".format(app.HEADERS)) # 定义员工数据文件的路径 filename = app.BASE_DIR + "/data/emp.json" @parameterized.expand(read_emp_data(filename, "add_emp")) def test02_add_emp(self, username, mobile, http_code, success, code, message): # 2 实现添加员工接口 response = self.emp_api.add_emp(username, mobile, app.HEADERS) # 打印添加的结果 logging.info("添加员工的结果为:{}".format(response.json())) # 获取添加员工返回的json数据 add_result = response.json() # 把员工id提取出来,并保存到变量当中 app.EMP_ID = add_result.get("data").get("id") # 打印获取的员工ID logging.info("app.EMPID为:{}".format(app.EMP_ID)) # 断言 assert_common_utils(self, response, http_code, success, code, message) @parameterized.expand(read_emp_data(filename, "query_emp")) def test03_query_emp(self, http_code, success, code, message): # 3 实现查询员工接口 # 发送查询员工的接口请求 response = self.emp_api.query_emp(app.EMP_ID, app.HEADERS) # 打印查询员工的结果 logging.info("查询员工的结果为:{}".format(response.json())) # 断言 assert_common_utils(self, response, http_code, success, code, message) @parameterized.expand(read_emp_data(filename, "modify_emp")) def test04_modify_emp(self, username, http_code, success, code, message): # 4 实现修改员工接口 # 发送修改员工接口请求 response = self.emp_api.modify_emp(app.EMP_ID, username, app.HEADERS) # 打印修改员工的结果 logging.info("修改员工的结果为:{}".format(response.json())) # 现在由于修改员工返回的响应数据当中,没有修改的username # 所有我们并不知道修改的username有没有成功 # 那么怎么办? # 我们需要连接到ihrm数据库中,然后按照添加员工返回的员工id查询这个员工id对应的 # username的值,如果数据库的username与修改的username一致,那么就证明修改成功了 # 实际数据:数据库查询出来的数据;预期:修改的数据 # 我们执行的SQL语句,在navicat中是查不到任何数据的,原因是因为执行完毕之后,员工被删除了 # 如果添加员工失败,那么员工ID提取失败,也会导致查询失败 # 导包 import pymysql # 连接数据库 conn = pymysql.connect(host='182.92.81.159', user='******', password='******', database='ihrm') # 获取游标 cursor = conn.cursor() # 定义SQL语句 sql = "select username from bs_user where id={}".format(app.EMP_ID) # 输出SQL语句 logging.info("打印SQL语句:{}".format(sql)) # 执行查询的SQL语句 cursor.execute(sql) # 获取执行结果 result = cursor.fetchone() # 调试打印执行的SQL结果 logging.info("执行SQL语句查询的结果为:{}".format(result)) # 关闭游标 cursor.close() # 关闭连接 conn.close() # 断言数据库查询的结果 self.assertEqual(username, result[0]) # 断言 assert_common_utils(self, response, http_code, success, code, message) @parameterized.expand(read_emp_data(filename, "delete_emp")) def test05_delete_emp(self, http_code, success, code, message): # 5 实现删除员工接口 # 发送删除员工接口请求 response = self.emp_api.delete_emp(app.EMP_ID, app.HEADERS) # 打印删除员工的结果 logging.info("删除员工的结果为:{}".format(response.json())) # 断言 assert_common_utils(self, response, http_code, success, code, message)
class TestEmp(unittest.TestCase): # 初始化 def setUp(self): # 实例化封装的登录接口 self.login_api = LoginApi() # 实例化封装的员工接口 self.emp_api = EmpApi() # 定义员工模块的URL self.emp_url = "http://182.92.81.159" + "/api/sys/user" def tearDown(self): pass # 编写测试员工增删改查的案例 def test01_login(self): # 1 实现登录接口 response = self.login_api.login( { "mobile": "13800000002", "password": "******" }, app.HEADERS) # 获取登录接口返回的json数据 result = response.json() # 输出登录的结果 logging.info("员工模块登录接口的结果为:{}".format(result)) # 把令牌提取出来,并保存到请求头当中 token = result.get("data") app.HEADERS = { "Content-Type": "application/json", "Authorization": "Bearer " + token } logging.info("登录成功后设置的请求头为:{}".format(app.HEADERS)) def test02_add_emp(self): # 2 实现添加员工接口 response = self.emp_api.add_emp("王安SS石0071X", "13123463453", app.HEADERS) # 打印添加的结果 logging.info("添加员工的结果为:{}".format(response.json())) # 获取添加员工返回的json数据 add_result = response.json() # 把员工id提取出来,并保存到变量当中 app.EMP_ID = add_result.get("data").get("id") # 打印获取的员工ID logging.info("app.EMPID为:{}".format(app.EMP_ID)) # 断言 assert_common_utils(self, response, 200, True, 10000, "操作成功") def test03_query_emp(self): # 3 实现查询员工接口 # 发送查询员工的接口请求 response = self.emp_api.query_emp(app.EMP_ID, app.HEADERS) # 打印查询员工的结果 logging.info("查询员工的结果为:{}".format(response.json())) # 断言 assert_common_utils(self, response, 200, True, 10000, "操作成功") def test04_modify_emp(self, username, http_code, success, code, message): # 4 实现修改员工接口 # 发送修改员工接口请求 response = self.emp_api.modify_emp(app.EMP_ID, username, app.HEADERS) # 打印修改员工的结果 logging.info("修改员工的结果为:{}".format(response.json())) # 断言 assert_common_utils(self, response, http_code, success, code, message) def test05_delete_emp(self): # 5 实现删除员工接口 # 发送删除员工接口请求 response = self.emp_api.delete_emp(app.EMP_ID, app.HEADERS) # 打印删除员工的结果 logging.info("删除员工的结果为:{}".format(response.json())) # 断言 assert_common_utils(self, response, 200, True, 10000, "操作成功")
class TestEmp(unittest.TestCase): # 初始化 def setUp(self): self.login_api = LoginApi() self.emp_api = EmpApi() # 定义员工模块的url self.emp_url = "http://182.92.81.159" + "/api/sys/user" def tearDown(self): pass # 编写测试员工增删改查的案例 def test01_test_emp_operation(self): # 1 实现登录接口 response = self.login_api.login( { "mobile": "13800000002", "password": "******" }, headers=app.HEADERS) # 获取登录接口返回的json数据 result = response.json() # 输出登录的结果 logging.info("员工登录接口的结果为:{}".format(result)) # 把令牌提取出来,并保存到请求头当中 token = result.get('data') headers = { "Content-Type": "application/json", "Authorization": "Bearer " + token } # 2 实现添加员工接口 response = self.emp_api.add_emp("卡子666", "13676789883", headers) # 打印添加的结果 logging.info("添加员工的结果为:{}".format(response.json())) # 获取添加员工返回的json数据 add_result = response.json() # 把员工id提取出来,并保存到变量当中 emp_id = add_result.get('data').get('id') # 断言 assert_common_utils(self, response, 200, True, 10000, "操作成功") # 3 实现查询员工接口 # 发生查询员工的接口请求 response = self.emp_api.query_emp(emp_id, headers) # 打印查询员工的结果 logging.info("查询员工的结果为:{}".format(response.json())) # 断言 assert_common_utils(self, response, 200, True, 10000, "操作成功") # 4 实现修改员工接口 # 发生修改员工的接口请求 response = self.emp_api.modify_emp(emp_id, "卡卡卡卡子666", headers) # 打印修改员工的结果 logging.info("修改员工的结果为:{}".format(response.json())) # 导包 import pymysql # 链接数据库 conn = pymysql.connect(host='182.92.81.159', user='******', password='******', database='ihrm') # 获取游标 cursor = conn.cursor() # 执行SQL语句 sql = "select username from bs_user where id={}".format(emp_id) logging.info("打印SQL语句:{}".format(sql)) cursor.execute(sql) result = cursor.fetchone() logging.info("执行SQL语句查询的结果为:{}".format(result)) # 关闭游标 cursor.close() # 关闭连接 conn.close() self.assertEqual("卡卡卡卡子666", result[0]) # 断言 assert_common_utils(self, response, 200, True, 10000, "操作成功") # 5 实现删除员工接口 # 发生删除员工的接口请求 response = self.emp_api.delete_emp(emp_id, headers) # 打印删除员工的结果 logging.info("删除员工的结果为:{}".format(response.json())) # 断言 assert_common_utils(self, response, 200, True, 10000, "操作成功")
class TestEmplyoee(unittest.TestCase): # 初始化 def setUp(self) -> None: # 实例化登录接口 self.login_api = LoginApi() # 实例化员工接口 self.emp_api = EmpApi() self.emp_url = "http://182.92.81.159/api/sys/user" def tearDown(self) -> None: pass # 编写测试员工增删改查的案例 def test01_emp_operation(self): # 登录接口 response = self.login_api.login( { "mobile": "13800000002", "password": "******" }, app.HEADERS) jsonData = response.json() logging.info("打印登录接口响应体数据:{}".format(jsonData)) # 获取响应体中的令牌数据 token = jsonData.get("data") headers = { "Content-Type": "application/json", "Authorization": "Bearer " + token } logging.info("员工模块接口请求头为:{}".format(headers)) # 添加员工 response = self.emp_api.add_emp("珠穆拉囊tyy", "18985641562", headers) # 打印添加的结果 logging.info("添加员工的结果为:{}".format(response.json())) # 获取添加员工返回的json数据 add_result = response.json() # 提取员工id,保存到变量中 emp_id = add_result.get("data").get("id") logging.info("获取到的员工id为:{}".format(emp_id)) # 断言 assert_commen_utils(self, response, 200, True, 10000, "操作成功") # 查询员工 # 发送查询员工接口请求 response = self.emp_api.query_emp(emp_id, headers) # 打印查询结果 logging.info("查询员工的结果为:{}".format(response.json())) # 断言 assert_commen_utils(self, response, 200, True, 10000, "操作成功") # 修改员工 # 发送修改员工接口请求 response = self.emp_api.modify_emp(emp_id, "珠穆拉拉", headers) logging.info("修改员工的结果为:{}".format(response.json())) # 断言 assert_commen_utils(self, response, 200, True, 10000, "操作成功") # 删除员工 delete_emp_url = self.emp_url + "/" + emp_id logging.info("删除员工接口的url为:{}".format(delete_emp_url)) # 发送修改员工接口请求 response = self.emp_api.delete_emp(emp_id, headers) logging.info("删除员工的结果为:{}".format(response.json())) # 断言 assert_commen_utils(self, response, 200, True, 10000, "操作成功")
class TestEmp(unittest.TestCase): # 初始化 def setUp(self) -> None: # 实例化封装的登录接口 self.login_api = LoginApi() # 实例化封装的添加员工接口 self.emp_api = EmpApi() # 定义员工模块的URL self.emp_url = "http://182.92.81.159" + "/api/sys/user" def tearDown(self) -> None: ... # 编写测试员工增删改查用例 def test01_test_emp_operation(self): # 1、实现登录接口 response = self.login_api.login( { "mobile": "13800000002", "password": "******" }, headers=app.headers) # 获取登录接口返回的json数据 result = response.json() # 输出登录的结果 logging.info("员工模块登录接口的结果为:{}".format(result)) # 把令牌提取出来,并保存到请求头当中 token = result.get("data") headers = { "Content-Type": "application/json", "Authorization": "Bearer " + token } logging.info("登录成功后设置的请求头为:{}".format(headers)) # 2、实现添加员工接口 # response = requests.post(self.emp_url, # json={ # "username": "******", # "mobile": "15000000015", # "timeOfEntry": "2020-03-16", # "formOfEmployment": 2, # "departmentName": "snowsnow", # "departmentId": "1226092852421177344", # "correctionTime": "2020-03-15T16:00:00.000Z"}, # headers=headers) response = self.emp_api.add_emp("大白鹅鹅008", "15562000222", headers) # 打印添加的结果 logging.info("添加员工的结果为:{}".format(response.json())) # 获取添加员工返回的json数据 add_result = response.json() # 把员工id提取出来,并保存到变量当中 emp_id = add_result.get("data").get("id") logging.info("获取的员工ID为:{}".format(emp_id)) # 断言 assert_common_uitls(self, response, 200, True, 10000, "操作成功") # 3、实现查询员工接口 # 发送查询员工的接口请求 response = self.emp_api.query_emp(emp_id, headers) # 打印查询员工的结果 logging.info("查询员工接口的结果为:{}".format(response.json())) # 断言 assert_common_uitls(self, response, 200, True, 10000, "操作成功") # 4、实现修改员工接口 # 发送修改员工接口的请求 response = self.emp_api.modify_emp(emp_id, "tom", headers) # 打印修改员工的结果 logging.info("修改员工的结果为:{}".format(response.json())) # 断言 assert_common_uitls(self, response, 200, True, 10000, "操作成功") # 5、实现删除员工接口 # 发送删除员工接口的请求 response = self.emp_api.delete_emp(emp_id, headers) # 打印修改员工的结果 logging.info("修改员工的结果为:{}".format(response.json())) # 断言 assert_common_uitls(self, response, 200, True, 10000, "操作成功")
class TestEmp(unittest.TestCase): # 初始化 def setUp(self): # 实例化封装的登录接口 self.login_api = LoginApi() self.emp_api = EmpApi() # 定义员工模块的URL self.emp_url = "http://182.92.81.159" + "/api/sys/user" def tearDown(self): pass # 编写测试员工增删改查的案例 def test01_test_emp_operation(self): # 1 实现登录接口 response = self.login_api.login( { "mobile": "13800000002", "password": "******" }, headers=app.HEADERS) # 获取登录接口返回的json数据 result = response.json() # 输出登录的结果 logging.info("员工模块登录接口的结果为:{}".format(result)) # 把令牌提取出来,并保存到请求头当中 token = result.get("data") headers = { "Content-Type": "application/json", "Authorization": "Bearer " + token } # 打印拼接完成的请求头 logging.info("登录成功后设置的请求头为:{}".format(headers)) # 2 实现添加员工接口 response = self.emp_api.add_emp("1号选手蔡徐坤", "17643982666", headers) # 打印添加的结果 logging.info("添加员工的结果为:{}".format(response.json())) # 获取添加员工返回的json数据 add_result = response.json() # 把员工id提取出来,并保存到变量当中 emp_id = add_result.get("data").get("id") # 打印获取的员工id logging.info("添加员工的id为:{}".format(emp_id)) # 断言 assert_common_utils(self, response, 200, True, 10000, "操作成功") # 3 实现查询员工接口 # 发送查询员工的接口请求 response = self.emp_api.query_emp(emp_id, headers) # 打印查询员工的结果 logging.info("查询员工的结果为:{}".format(response.json())) # 断言 assert_common_utils(self, response, 200, True, 10000, "操作成功") # 4 实现修改员工接口 # 发送修改员工接口请求 response = self.emp_api.modify_emp(emp_id, "蔡徐坤爱打篮球", headers) # 打印修改员工的结果 logging.info("修改员工的结果为:{}".format(response.json())) # 断言 assert_common_utils(self, response, 200, True, 10000, "操作成功") # 5 实现删除员工接口 # 发送删除员工接口请求 response = self.emp_api.delete_emp(emp_id, headers) # 打印删除员工的结果 logging.info("删除员工的结果为:{}".format(response.json())) # 断言 assert_common_utils(self, response, 200, True, 10000, "操作成功")
class TestEmplyoee(unittest.TestCase): # 初始化 def setUp(self) -> None: # 实例化登录接口 self.login_api = LoginApi() # 实例化员工接口 self.emp_api = EmpApi() self.emp_url = "http://182.92.81.159/api/sys/user" def tearDown(self) -> None: pass # 编写测试员工增删改查的案例 def test01_emp_operation(self): # 登录接口 response = self.login_api.login( { "mobile": "13800000002", "password": "******" }, app.HEADERS) jsonData = response.json() logging.info("打印登录接口响应体数据:{}".format(jsonData)) # 获取响应体中的令牌数据 token = jsonData.get("data") app.HEADERS = { "Content-Type": "application/json", "Authorization": "Bearer " + token } logging.info("员工模块接口请求头为:{}".format(app.HEADERS)) # 数据文件路径 filename = app.BASE_DIR + "/data/emp.json" # 参数化 @parameterized.expand(read_emp_data(filename, "add_emp")) def test02_add_emp(self, username, mobile, http_code, success, code, message): # 添加员工 response = self.emp_api.add_emp(username, mobile, app.HEADERS) # 打印添加的结果 logging.info("添加员工的结果为:{}".format(response.json())) # 获取添加员工返回的json数据 add_result = response.json() # 提取员工id,保存到变量中 app.EMP_ID = add_result.get("data").get("id") logging.info("EMP_ID的值为:{}".format(app.EMP_ID)) # 断言 assert_commen_utils(self, response, http_code, success, code, message) def test03_query_emp(self): # 查询员工 # 发送查询员工接口请求 response = self.emp_api.query_emp(app.EMP_ID, app.HEADERS) # 打印查询结果 logging.info("查询员工的结果为:{}".format(response.json())) # 断言 assert_commen_utils(self, response, 200, True, 10000, "操作成功") def test04_modify_emp(self): # 修改员工 # 发送修改员工接口请求 response = self.emp_api.modify_emp(app.EMP_ID, "珠穆拉拉", app.HEADERS) logging.info("修改员工的结果为:{}".format(response.json())) # 在断言之前执行数据库操作,否则断言失败会无法执行 # 导包 import pymysql # 连接数据库 conn = pymysql.connect(host="182.92.81.159", user="******", password="******", database="ihrm") # 获取游标 cursor = conn.cursor() sql = "select username from bs_user where id={}".format(app.EMP_ID) # 打印执行的SQL语句 logging.info("执行的SQL语句为:{}".format(sql)) # 执行SQL语句 cursor.execute(sql) result = cursor.fetchone() logging.info("执行SQL语句的结果为:{}".format(result)) # 断言执行结果 self.assertEqual("珠穆拉拉", result[0]) # 关闭游标 cursor.close() # 关闭连接 conn.close() # 断言 assert_commen_utils(self, response, 200, True, 10000, "操作成功") def test05_delete_emp(self): # 删除员工 delete_emp_url = self.emp_url + "/" + app.EMP_ID logging.info("删除员工接口的url为:{}".format(delete_emp_url)) # 发送修改员工接口请求 response = self.emp_api.delete_emp(app.EMP_ID, app.HEADERS) logging.info("删除员工的结果为:{}".format(response.json())) # 断言 assert_commen_utils(self, response, 200, True, 10000, "操作成功")