Ejemplo n.º 1
0
class TestIHRMEmployee(unittest.TestCase):
    def setUp(self):
        self.login_api = LoginApi()
        self.emp_api = EmployeeApi()

    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并保存到全局变量
        app.HEADERS = {
            "Content-Type": "application/json",
            "Authorization": token
        }
        logging.info("保存到全局变量中的请求头为:{}".format(app.HEADERS))

    emp_path = app.BASE_DIR + "/data/emp_data.json"

    @parameterized.expand(read_emp_data(emp_path, 'add_emp'))
    def test02_add_emp_success(self, username, mobile, success, code, message,
                               http_code):
        # 发送添加员工的接口请求
        response = self.emp_api.add_emp(username, mobile, app.HEADERS)
        logging.info("添加员工得结果为:{}".format(response.json()))
        app.EMP_ID = response.json().get('data').get('id')
        logging.info("保存到全局变量的员工ID为:{}".format(app.EMP_ID))
        assert_common(self, http_code, success, code, message, response)

    @parameterized.expand(read_emp_data(emp_path, "query_emp"))
    def test03_query_emp_success(self, success, code, message, http_code):
        response = self.emp_api.query_emp(app.EMP_ID, app.HEADERS)
        logging.info("查询员工得信息为:{}".format(response.json()))
        assert_common(self, http_code, success, code, message, response)

    @parameterized.expand(read_emp_data(emp_path, "modify_emp"))
    def test04_modify_emp_success(self, username, success, code, message,
                                  http_code):
        data = {"username": username}
        response = self.emp_api.revise_emp(app.EMP_ID, app.HEADERS, data)
        logging.info("修改员工得信息为:{}".format(response.json()))
        assert_common(self, http_code, success, code, message, response)

    @parameterized.expand(read_emp_data(emp_path, "delete_emp"))
    def test05_delete_emp_success(self, success, code, message, http_code):
        response = self.emp_api.delete_emp(app.EMP_ID, app.HEADERS)
        logging.info("删除员工得信息为:{}".format(response.json()))
        assert_common(self, http_code, success, code, message, response)
Ejemplo n.º 2
0
class TestIHRMEmployeeParams(unittest.TestCase):

    # 初始化 unittest 的函数
    def setUp(self):
        # 实例化登录
        self.login_api = LoginApi()
        # 实例化员工
        self.emp_api = EmployeeApi()

    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)

    # 定义员工模块的文件路径
    emp_filepath = app.BASE_PATH + "/data/emp_data.json"

    # 添加员工
    # 参数化
    @parameterized.expand(read_emp_data(emp_filepath, "add_emp"))
    def test02_add_emp(self, username, mobile, success, code, message,
                       http_code):
        logging.info("app.HEADERS的值为:{}".format(app.HEADERS))
        # 发送添加员工接口请求
        response = self.emp_api.add_emp(username, mobile, app.HEADERS)
        # 打印添加员工的结果
        logging.info("添加员工的结果为:{}".format(response.json()))
        # 提取员工中的令牌,并把令牌保存到全局变量中
        app.EMP_ID = response.json().get("data").get("id")
        # 打印保存的员工的ID
        logging.info("保存到全局变量的员工ID为{}".format(app.EMP_ID))
        # 断言
        assert_common(self, http_code, success, code, message, response)

    # 查询员工
    # 参数化
    @parameterized.expand(read_emp_data(emp_filepath, "query_emp"))
    def test03_query_emp(self, success, code, message, http_code):
        # 发送查询员工的接口请求
        response = self.emp_api.query_emp(app.EMP_ID, app.HEADERS)
        # 打印查询员工的数据
        logging.info("查询员工的结果为:{}".format(response.json()))
        # 断言
        assert_common(self, http_code, success, code, message, response)

    # 修改员工
    # 参数化
    @parameterized.expand(read_emp_data(emp_filepath, "modify_emp"))
    def test04_modify_emp(self, username, success, code, message, http_code):
        # 发送修改员工的接口请求
        response = self.emp_api.modify_emp(app.EMP_ID, {"username": username},
                                           app.HEADERS)
        # 打印修改员工的结果
        logging.info("修改员工的结果为:{}".format(response.json()))
        # 断言
        assert_common(self, http_code, success, code, message, response)

    # 删除员工
    # 参数化
    @parameterized.expand(read_emp_data(emp_filepath, "delete_emp"))
    def test05_delete_emp(self, success, code, message, http_code):
        # 发送删除员工的接口请求
        response = self.emp_api.delete_emp(app.EMP_ID, app.HEADERS)
        # 打印删除员工的结果
        logging.info("删除员工的结果为:{}".format(response.json()))
        # 断言
        assert_common(self, http_code, success, code, message, response)
class TestEmployee2(unittest.TestCase):
    def setUp(self):
        self.login_url = "http://ihrm-test.itheima.net" + "/api/sys/login"
        self.emp_url = "http://ihrm-test.itheima.net" + "/api/sys/user"
        self.emp_api = TestEmployeeApi()
        self.login_api = TestLoginApi()

    # 编写测试用例 先登录
    def test01_login(self):
        response = self.login_api.login(
            {
                "mobile": "13800000002",
                "password": "******"
            }, {"Content-Type": "application/json"})
        logging.info("登录的结果为: {}".format(response.json()))
        # 提取令牌
        token = response.json().get("data")
        # 保存令牌到请求头中
        headers = {
            "Content-Type": "application/json",
            "Authorization": "Bearer " + token
        }
        # 保存headers到全局变量app.py中
        app.HEADERS = headers
        # 打印令牌
        logging.info("保存到全局变量app中的请求头: {}".format(headers))  # 打印令牌

    filename = app.BASE_DIR + "/data/emp_data.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(app.HEADERS, username, mobile)
        logging.info("添加成功:{}".format(response.json()))
        # 提取添加员工中的id
        emp_id = response.json().get("data").get("id")
        # 保存emp_id到全局变量app.py中
        app.EMP_ID = emp_id
        assert_common(http_code, success, code, message, response, self)

    # 查询员工
    @parameterized.expand(read_emp_data(filename, "query_emp"))
    def test03_query_emp(self, http_code, success, code, message):
        # 发送查询员工的请求
        response = self.emp_api.query_emp(app.EMP_ID, app.HEADERS)
        logging.info("查询成功:{}".format(response.json()))
        assert_common(http_code, success, code, message, response, self)

    # 修改员工
    @parameterized.expand(read_emp_data(filename, "modify_emp"))
    def test04_modify_emp(self, username, http_code, success, code, message):
        # 修改员工
        response = self.emp_api.modify_emp(app.EMP_ID, app.HEADERS, username)
        print("修改成功:", response.json())
        assert_common(http_code, success, code, message, response, self)

    # 删除员工
    @parameterized.expand(read_emp_data(filename, "delete_emp"))
    def test05_delete_emp(self, http_code, success, code, message):
        # 删除员工
        response = self.emp_api.delete_emp(app.EMP_ID, app.HEADERS)
        logging.info("删除成功:{}".format(response.json()))
        assert_common(http_code, success, code, message, response, self)
Ejemplo n.º 4
0
class TestIHRMEmployeeParams(unittest.TestCase):
    # 初始化unittest的函数
    def setUp(self):
        # 实例化登录
        self.login_api = LoginApi()
        # 实例化员工
        self.emp_api = EmployeeApi()

    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))

        # 断言
        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"))

    # 定义员工模块的文件路径
    emp_filepath = app.BASE_DIR + "/data/empp_data.json"


    # 参数化
    @parameterized.expand(read_emp_data(emp_filepath, 'add_emp'))
    def test02_add_emp(self,username,mobile,success,code,message,http_code):
        logging.info("app.HEADERS的值是:{}".format(app.HEADERS))
        # 发送添加员工的接口请求
        response = self.emp_api.add_emp(username, mobile, app.HEADERS)
        # 打印添加员工的结果
        logging.info("添加员工的结果为:{}".format(response.json()))
        # 提取员工中的令牌并把员工令牌保存到全局变量中
        app.EMP_ID = response.json().get("data").get("id")
        # 打印保存的员工ID
        logging.info("保存到全局变量的员工的ID为:{}".format(app.EMP_ID))
        # 断言
        self.assertEqual(http_code, response.status_code)
        self.assertEqual(success, response.json().get("success"))
        self.assertEqual(code, response.json().get("code"))
        self.assertIn(message, response.json().get("message"))

    @parameterized.expand(read_emp_data(emp_filepath, "query_emp"))
    def test03_query_emp(self,success,code,message,http_code):
        # 发送查询员工的接口请求:
        response = self.emp_api.query_emp(app.EMP_ID, app.HEADERS)
        # 打印查询员工的数据
        logging.info("查询员工的结果为:{}".format(response.json()))
        # 断言
        self.assertEqual(http_code, response.status_code)
        self.assertEqual(success, response.json().get("success"))
        self.assertEqual(code, response.json().get("code"))
        self.assertIn(message, response.json().get("message"))

    @parameterized.expand(read_emp_data(emp_filepath, "modify_emp"))
    def test04_modify_emp(self,username,success,code,message,http_code):
        # 调用封装的修改员工接口,发送接口请求
        response = self.emp_api.modify_emp(app.EMP_ID,{"username":username},app.HEADERS)
        # 打印数据
        logging.info("修改员工的结果为:{}".format(response.json()))
        # 断言
        self.assertEqual(http_code, response.status_code)
        self.assertEqual(success, response.json().get("success"))
        self.assertEqual(code, response.json().get("code"))
        self.assertIn(message, response.json().get("message"))

    @parameterized.expand(read_emp_data(emp_filepath, "delete_emp"))
    def test05_delete_emp(self,success,code,message,http_code):
        # 调用封装的删除员工接口哦,发送接口请求
        response = self.emp_api.delete_emp(app.EMP_ID, app.HEADERS)
        # 打印删除员工的结果为
        logging.info("删除员工的结果为:{}".format(response.json()))
        # 断言
        self.assertEqual(http_code, response.status_code)
        self.assertEqual(success, response.json().get("success"))
        self.assertEqual(code, response.json().get("code"))
        self.assertIn(message, response.json().get("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)
Ejemplo n.º 6
0
class TestEmployeeParams(unittest.TestCase):

    # 实例化
    @classmethod
    def setUpClass(cls):
        # 实例化登陆模块的封装API
        cls.login_api = LoginApi()
        # 实例化员工模块
        cls.emp_api = EmployeeApi()

    # 实现员工的增删改查
    def test01_login(self):
        # 先调用登陆接口,并登陆成功
        response = self.login_api.login({"mobile":"13800000002",
                                         "password":"******"})
        # 输出登陆的结果
        print("登陆的结果为:", response.json())
        # 把令牌保存并更新HEADERS中
        app.HEADERS["Authorization"] = "Bearer " + response.json().get("data")
        # 输出保存的app.HEADERS,查看结果
        print("更新之后的令牌为:", app.HEADERS)

    # 员工数据文件的路径
    emp_path = app.BASE_DIR + "/data/employee.json"

    @parameterized.expand(read_emp_data(emp_path, "add_emp"))
    def test02_add_emp(self,request_body, success,code,message,status_code):
        # 使用封装的添加员工接口添加员工
        response = self.emp_api.add_emp(request_body, app.HEADERS)
        # 打印添加的结果
        print("添加员工的结果为:", response.json())
        # 断言添加员工
        assert_common_util(self, response, status_code, success, code, message)
        # 提取员工ID,并把员工ID保存到全局变量
        app.EMP_ID = response.json().get("data").get("id")

    @parameterized.expand(read_emp_data(emp_path, "query_emp"))
    def test03_query_emp(self,success,code,message,status_code):
        # 调用封装的查询员工接口,并传给员工ID和请求头给查询员工接口
        response = self.emp_api.query_emp(app.EMP_ID, app.HEADERS)
        # 打印查询员工的结果
        print("查询员工的结果为:", response.json())
        # 断言
        assert_common_util(self, response, status_code, success, code, message)

    @parameterized.expand(read_emp_data(emp_path, "modify_emp"))
    def test04_modify_emp(self,request_body, success,code,message,status_code):
        # 调用修改员工接口,并传递请求体、员工id,请求头,实现修改员工
        response = self.emp_api.modify_emp(app.EMP_ID, request_body, app.HEADERS)
        # 打印修改员工的结果
        print("修改员工的结果", response.json())
        # 断言
        assert_common_util(self, response, status_code, success, code, message)

    @parameterized.expand(read_emp_data(emp_path, "delete_emp"))
    def test05_delete_emp(self,success,code,message,status_code):
        # 调用删除员工接口
        response = self.emp_api.delete_emp(app.EMP_ID, app.HEADERS)
        # 打印删除的结果
        print("删除的结果为:", response.json())
        # 断言
        assert_common_util(self, response, status_code, success, code, message)
Ejemplo n.º 7
0
class TestEmp(unittest.TestCase):
    # 初始化
    def setUp(self):
        self.login_api = loginApi()
        # 实例化empApi对象
        self.emp_api = EmpApi()
        self.emp_url = "http://182.92.81.159" + "/api/sys/user"
        pass

    def tearDown(self):
        pass

    # 编写测试用例: 员工的增删改查
    def test01_login(self):
        # 1.实现登录接口(操作员工的前提)
        jsonData = {"mobile": "13800000002", "password": "******"}
        response = self.login_api.login(jsonData, headers=app.HEADERS)
        # 获取登录接口返回json格式响应数据
        logging.info("响应数据:{}".format(response.json()))
        # 提取响应数据中的令牌,并保存到请求头
        token = response.json().get("data")
        logging.info("令牌是:{}".format(token))
        app.HEADERS = {"Content-Type": "application/json", "Authorization": "Bearer " + token}
        logging.info("请求头是:{}".format(app.HEADERS))

    # 定义员工数据文件的路径
    filename = app.BASE_PATH + "/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("无敌的沙福林达人03","15899009982",app.HEADERS)
        # 打印添加员工的URL
        logging.info("添加员工URL:{}".format(self.emp_api.emp_url))
        # 获取到的员工返回的json数据
        add_result = response.json()
        logging.info("查询添加的员工信息为:{}".format(add_result))
        # 把员工id提取出来,并保存到变量
        app.EMP_ID = add_result.get("data").get("id")
        logging.info("员工id为:{}".format(app.EMP_ID))
        self.assertIn("操作成功", add_result.get("message"))
        # 调用封装的断言模块进行断言,将要进行断言的数据,按照要求传递
        assert_commit_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.实现查询员工接口
        # 把员工ID拼接到URL中,组成查询员工所需要的url
        response = self.emp_api.query_emp(app.EMP_ID,app.HEADERS)
        logging.info("查询员工信息:{}".format(response.json()))
        # 调用封装的断言模块进行断言,将要进行断言的数据,按照要求传递
        assert_commit_utils(self, response, http_code, success, code, message)




    @parameterized.expand(read_emp_data(filename,"modify_emp"))
    def test04_modify_emp(self,username,http_coed,success,code,message):
        # 4 实现修改员工接口
        #   把员工id拼接到url中,组成修改员工所需要的URL
        # 发送修改员工接口请求
        response = self.emp_api.modify_emp(app.EMP_ID,"无敌的沙福da",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)
        cursor.execute(sql)
        # 输出sql语句
        logging.info("打印SQL语句:{}".format(sql))
        # 调试执行的SQL结果
        result = cursor.fetchone()
        logging.info("执行SQL语句查询的结果为:{}".format(result))
        # 关闭游标
        cursor.close()
        # 关闭连接
        conn.close()
        # 断言数据库查询的结果
        self.assertEqual("无敌的沙福da",result[0])

        # 调用封装的断言模块进行断言,将要进行断言的数据,按照要求传递
        assert_commit_utils(self, response, http_coed, 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_commit_utils(self, response, http_code, success, code, message)
class TestIHRMEmployee3(unittest.TestCase):
    # 初始化测试类
    def setUp(self):
        self.login_url = "http://ihrm-test.itheima.net" + "/api/sys/login"
        self.emp_url = "http://ihrm-test.itheima.net" + "/api/sys/user"
        from api.employee_api import TestEmployeeApi
        self.emp_api = TestEmployeeApi()  # 员工实例化
        from api.login_api import TestLoginApi
        self.login_api = TestLoginApi()  # 登录实例化
    # 编写测试函数
    def test01_login(self):
        response = self.login_api.login({"mobile": "13800000002", "password": "******"},
                                        {"Content-Type": "application/json"})
        # 打印登录的数据
        logging.info("登录的结果为:{}".format(response.json()))
        # 提取令牌
        token = response.json().get("data")
        # 保存令牌到请求头当中
        headers = {"Content-Type": "application/json", "Authorization": "Bearer " + token}
        app.HEADERS = headers
        # 打印令牌
        logging.info("请求头中令牌:{}".format(headers))


    # 定义员工模块数据文件路径
    filename = app.PATH + "/data/emp_data.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(app.HEADERS,username,mobile)
        # 打印添加的结果
        logging.info("添加员工的结果为:{}".format(response.json()))
        # 提取添加员工中的id
        emp_id = response.json().get("data").get("id")
        app.EMPID = emp_id # 保存emp_id到全局变量app.py中
        # 断言
        assert_common(http_code, success, code, message, response, self)

    @parameterized.expand(read_emp_data(filename, 'query_emp'))
    # 查询员工
    def test03_query_emp(self,http_code, success, code, message):
        response = self.emp_api.query_emp(app.EMPID, app.HEADERS)
        logging.info("查询员工的结果为:{}".format(response.json()))

        # 断言
        assert_common(http_code, success, code, message, response, self)

    @parameterized.expand(read_emp_data(filename, 'modify_emp'))
    # 修改员工
    def test04_modify_emp(self,username, http_code, success, code, message):
        # 发送修改员工的请求
        response = self.emp_api.modify_emp(app.EMPID,
                                           app.HEADERS,
                                           username)
        # 打印修改的结果
        logging.info("修改员工的结果为:{}".format(response.json()))

        # 断言
        assert_common(http_code, success, code, message, response, self)

    @parameterized.expand(read_emp_data(filename, 'delete_emp'))
    # 删除员工
    def test05_delete_emp(self,http_code, success, code, message):
        # 发送删除员工的请求
        response = self.emp_api.delete_emp(app.EMPID,
                                           app.HEADERS,)
        # 打印删除的结果
        logging.info("删除的结果为:{}".format(response.json()))
        # 断言
        assert_common(http_code, success, code, message, response, self)
Ejemplo n.º 10
0
class TestEmployee(unittest.TestCase):
    # 定义初始化fixture
    def setUp(self):
        # 实例化封装的登录接口
        self.login_api = LoginApi()
        # 实例化封装的员工接口
        self.emp_api = EmployeeApi()

    def tearDown(self):
        pass

    # 实现登录成功的接口
    def test01_login_success(self):
        # 调用封装好的登录的接口发送登录接口请求
        response_login = self.login_api.login(
            {
                "mobile": "13800000002",
                "password": "******"
            }, {"Content-Type": "Application/json"})

        # 打印登录结果
        logging.info("返回的结果为: {}".format(response_login.json()))
        # 提取登录后的令牌
        token = "Bearer " + response_login.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)

    # 定义员工模块的文件路径
    file_path = app.BASE_DIR + "/data/emp.json"

    # 实现添加员工的接口
    # 参数化
    @parameterized.expand(read_emp_data(file_path, 'add_emp'))
    def test02_add_emp(self, username, mobile, success, code, message,
                       http_code):
        # 调用封装好的员工的接口发送添加员工接口请求
        response_add = self.emp_api.add_emp(username, mobile, app.HEADERS)
        # 打印添加员工的结果
        logging.info("添加员工的结果为: {}".format(response_add.json()))
        # 提取添加员工中的id保存到全局变量中
        app.EMP_ID = response_add.json().get("data").get("id")
        # 打印保存导全局变量中的id
        logging.info("保存到全局变量中的id为: {}".format(app.EMP_ID))
        # 断言
        assert_common(self, http_code, success, code, message, response)

    # 实现查询员工的接口
    # 参数化
    @parameterized.expand(read_emp_data(file_path, 'select_emp'))
    def test03_select_emp(self, success, code, message, http_code):
        # 调用封装好的员工的接口发送查询员工接口请求
        response_select = self.emp_api.select_emp(app.EMP_ID, app.HEADERS)
        # 打印查询员工的结果
        logging.info("查询员工的结果为: {}".format(response_select.json()))
        # 断言
        assert_common(self, http_code, success, code, message, response)

    # 实现修改员工的接口
    # 参数化
    @parameterized.expand(read_emp_data(file_path, 'modify_emp'))
    def test04_modify_emp(self, username, success, code, message, http_code):
        # 调用封装好的员工的接口发送修改员工接口请求
        response_modify = self.emp_api.modify_emp(username, app.EMP_ID,
                                                  app.HEADERS)
        # 打印修改员工的结果
        logging.info("修改员工的结果为: {}".format(response_modify.json()))
        # 断言
        assert_common(self, http_code, success, code, message, response)

    # 实现删除员工的接口
    # 参数化
    @parameterized.expand(read_emp_data(file_path, 'delete_emp'))
    def test05_delete_emp(self, success, code, message, http_code):
        # 调用封装好的员工的接口发送删除员工接口请求
        delete_modify = self.emp_api.delete_emp(app.EMP_ID, app.HEADERS)
        # 打印删除员工的结果
        logging.info("删除员工的结果为: {}".format(delete_modify.json()))
        # 断言
        assert_common(self, http_code, success, code, message, response)
Ejemplo n.º 11
0
class TestIHRMEmployeeParams02(unittest.TestCase):
    # 初始化unittest的函数
    def setUp(self):
        # 实例化登录
        self.login_api = LoginApi()
        # 实例化部门
        self.emp_api = DepartmentApi()

    def tearDown(self):
        pass

    # 定义登录数据文件的路径
    filepath = app.BASE_DIR + "/data/login_data.json"

    @parameterized.expand(read_login_data(filepath))
    # 实现登录成功的接口
    def test01_login_success(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()))

        # 提取登录返回的令牌
        token = 'Bearer ' + response.json().get('data')
        # 把令牌拼接成HEADERS并保存到全局变量HEADERS,供后续增删改查调用
        app.HEADERS = {"Content-Type": "application/json", "Authorization": token}
        # 打印请求头
        logging.info("保存到全局变量中的请求头为:{}".format(app.HEADERS))

        # 断言
        assert_common(self, http_code, success, code, message, response)

    # 定义部门模块的文件路径
    emp_filepath = app.BASE_DIR + "/data/emp_data002.json"

    # 参数化
    @parameterized.expand(read_emp_data(emp_filepath))
    def test02_add_emp(self, a, b, c, d, e, f, g, h, i, j, k, l, m):
        # 发送添加部门的接口请求
        response = self.emp_api.add_emp(a, b, c, d, app.HEADERS)
        # 打印添加部门的结果
        logging.info("添加部门的结果为:{}".format(response.json()))
        # 提取部门中的令牌并把部门令牌保存到全局变量中
        app.EMP_ID = response.json().get("data").get("id")
        # 打印保存的部门ID
        logging.info("保存到全局变量的部门的ID为:{}".format(app.EMP_ID))
        # 断言
        assert_common(self, h, e, f, g, response)

        # 发送查询部门的接口请求:
        response = self.emp_api.query_emp(app.EMP_ID, app.HEADERS)
        # 打印查询部门的数据
        logging.info("查询部门的结果为:{}".format(response.json()))
        # 断言
        assert_common(self, h, e, f, g, response)

        # 调用封装的修改部门接口,发送接口请求
        response = self.emp_api.modify_emp(m, i, j, k, l, app.HEADERS)
        # 打印数据
        logging.info("修改部门的结果为:{}".format(response.json()))
        # 断言
        assert_common(self, 200, False, 99999, "抱歉,系统繁忙,请稍后重试!", response)

        # 调用封装的删除部门接口哦,发送接口请求
        response = self.emp_api.delete_emp(m, app.HEADERS)
        # 打印删除员工的结果为
        logging.info("删除部门的结果为:{}".format(response.json()))
        # 断言
        assert_common(self, 200, False, 99999, "抱歉,系统繁忙,请稍后重试!", response)
Ejemplo n.º 12
0
class TestEmp(unittest.TestCase):
    def setUp(self):
        self.emp_api = Employee()
        self.login_api = LoginApi()

    # 分离登陆成功用例
    def test02_login_success(self):
        response_login = self.emp_api.login("13800000002", "123456")
        logging.info("员工模块的登陆结果为:{}".format(response_login.json()))
        token = "Bearer " + response_login.json().get("data")
        # 得到请求头数据
        # global headers # 把局部变量变成全局变量,但是其他文件也需要调用,只能配置到app.py里
        headers = {"Content-Type": "application/json", "Authorization": token}
        app.HEADERS = headers
        logging.info("取出的令牌为:{}".format(token))
        logging.info("请求头为:{}".format(headers))
        common_assert(self, response_login, 200, True, 10000, "操作成功")

    # 添加员工
    @parameterized.expand(read_add_emp_data())
    def test03_add_emp(self, case_name, username, mobile, http_code, success,
                       code, message):
        # ('添加员工', 'Rose', '13099998888', 200, True, 10000, '操作成功')

        # mobile1 = "179"+time.strftime("%d%H%M%S")
        # name1 ="rose"+time.strftime("%d%H%M%S")
        response_add_emp = self.emp_api.add_emp(username, mobile, app.HEADERS)
        logging.info("添加员工的结果是:{}".format(response_add_emp.json()))
        emp_id = response_add_emp.json().get("data").get("id")
        app.EMP_ID = emp_id
        logging.info("添加员工的id是{}".format(app.EMP_ID))
        common_assert(self, response_add_emp, http_code, success, code,
                      message)

#"find_emp":{"case_name":"查询员工","http_code":200,"success":true,"code":10000,"message":"操作成功"},

    @parameterized.expand(read_emp_data('find_emp'))
    def test04_find_emp(self, case_name, http_code, success, code, message):
        response_find_emp = self.emp_api.find_emp(
            app.EMP_ID, app.HEADERS)  #小写headers有个同名模块,容易进坑
        logging.info("查询员工的结果为:{}".format(response_find_emp.json()))
        common_assert(self, response_find_emp, http_code, success, code,
                      message)

    # "modify_emp": {"case_name": "修改员工", "new_name": "apple", "http_code": 200, "success": true, "code": 10000,
    #                "message": "操作成功"},
    @parameterized.expand(read_emp_data('modify_emp'))
    def test05_modify_emp(self, case_name, new_name, http_code, success, code,
                          message):
        response_modify_emp = self.emp_api.modify_emp(app.EMP_ID, new_name,
                                                      app.HEADERS)
        logging.info("修改员工的结果为:{}".format(response_modify_emp.json()))
        common_assert(self, response_modify_emp, http_code, success, code,
                      message)

        with DBUtils() as db:
            sql = 'select username from bs_user where id ={}'.format(
                app.EMP_ID)
            db.execute(sql)
            result = db.fetchone()
            self.assertEqual("apple", result[0])
            logging.info("数据库查询结果为:{}".format(result[0]))

        # # 连接数据库
        # 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)
        # cursor.execute(sql)
        # result = cursor.fetchone()
        # self.assertEqual("happy",result[0])

    # "del_emp": {"case_name": "删除员工", "http_code": 200, "success": true, "code": 10000, "message": "操作成功"}
    @parameterized.expand(read_emp_data('del_emp'))
    def test06_del_emp(self, case_name, http_code, success, code, message):
        response_del_emp = self.emp_api.del_emp(app.EMP_ID, app.HEADERS)
        logging.info("删除员工的结果为:{}".format(response_del_emp.json()))
        common_assert(self, response_del_emp, http_code, success, code,
                      message)
        self.assertEqual(None, response_del_emp.json().get("data"))
Ejemplo n.º 13
0
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, "操作成功")
Ejemplo n.º 14
0
class TestEmpParams(unittest.TestCase):
    """参数化员工管理模块"""
    def setUp(self):
        self.login_api = LoginApi()
        self.emp_api = EmployeeApi()

    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)

    # 定义员工模块的文件路径
    emp_file_path = app.BASE_DIR + '/data/emp_data.json'

    @parameterized.expand(read_emp_data(emp_file_path, 'add_emp'))
    def test02_add_emp(self, username, password, http_code, success, code,
                       message):
        """添加员工接口"""
        response = self.emp_api.add_emp(username, password, app.HEADERS)
        logging.info('添加员工的结果为:{}'.format(response.json()))
        # 提取员工中的令牌并把员工令牌保存到全局变量中
        app.EMP_ID = response.json().get('data').get('id')
        # 打印保存的员工ID
        logging.info('保存到全局变量中的员工id为:{}'.format(app.EMP_ID))
        assert_common(self, http_code, success, code, message, response)

    @parameterized.expand(read_emp_data(emp_file_path, 'query_emp'))
    def test03_query_emp(self, http_code, success, code, message):
        """查询员工接口"""
        response = self.emp_api.query_emp(app.EMP_ID, app.HEADERS)
        logging.info('查询员工的结果为:{}'.format(response.json()))
        assert_common(self, http_code, success, code, message, response)

    @parameterized.expand(read_emp_data(emp_file_path, 'modify_emp'))
    def test04_modify_emp(self, modify_data, http_code, success, code,
                          message):
        """修改员工接口"""
        response = self.emp_api.modify_emp(app.EMP_ID, app.HEADERS,
                                           modify_data)
        logging.info('修改员工的结果为:{}'.format(response.json()))
        assert_common(self, http_code, success, code, message, response)

    @parameterized.expand(read_emp_data(emp_file_path, 'delete_emp'))
    def test05_delete_emp(self, http_code, success, code, message):
        """删除员工接口"""
        response = self.emp_api.delete_emp(app.EMP_ID, app.HEADERS)
        logging.info('删除员工的结果为:{}'.format(response.json()))
        assert_common(self, http_code, success, code, message, response)
Ejemplo n.º 15
0
class TestEmpioyee(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        # 初始化
        cls.employee_api = EmployeeApi()
        cls.login_api = LoginApi()

    # 测试用例 登陆成功
    def test01_login_success(self):
        # 调用登陆
        response = self.login_api.login_ihrm(
            {
                "mobile": "13800000002",
                "password": "******"
            }, {"Content-Type": "application/json"})
        # 打印登陆结果
        logging.info(response.json())
        assert_common(self, 200, 10000, True, '操作成功', response)
        # 获取令牌
        token = 'Bearer ' + response.json().get('data')
        # 将令牌拼接成headers
        app.HEADERS = {
            "Content-Type": "application/json",
            "Authorization": token
        }
        # 打印请求头
        logging.info(app.HEADERS)

    filepath2 = app.BASE_DIR + "/data/employee_data.json"

    # 添加员工
    @parameterized.expand(read_emp_data(filepath2, "add_emp"))
    def test02_add_emp(self, username, mobile, success, code, message,
                       http_code):
        # 调用添加员工接口
        response = self.employee_api.add_emp_api(username, mobile, app.HEADERS)
        # 打印结果
        logging.info(response.json())
        # 提取员工id
        app.EMP_ID = response.json().get('data').get('id')
        # 打印员工id
        logging.info(app.EMP_ID)
        assert_common(self, http_code, code, success, message, response)

    # 查询员工接口
    @parameterized.expand(read_emp_data(filepath2, "query_emp"))
    def test03_query_emp(self, success, code, message, http_code):
        # 调用查询员工接口
        response = self.employee_api.query_emp_api(app.EMP_ID, app.HEADERS)
        # 打印内容
        logging.info(response.json())
        assert_common(self, http_code, code, success, message, response)

    @parameterized.expand(read_emp_data(filepath2, "modify_emp"))
    def test04_modify_emp(self, username, success, code, message, http_code):
        # 调用修改员工接口
        response = self.employee_api.modify_emp_api(app.EMP_ID,
                                                    {"username": username},
                                                    app.HEADERS)
        # 打印内容
        logging.info(response.json())
        assert_common(self, http_code, code, success, message, response)

    @parameterized.expand(read_emp_data(filepath2, "delete_emp"))
    def test05_delete_emp(self, success, code, message, http_code):
        # 调用修改员工接口
        response = self.employee_api.delete_emp_api(app.EMP_ID, app.HEADERS)
        # 打印内容
        logging.info(response.json())
        assert_common(self, http_code, code, success, message, response)