Esempio n. 1
0
class TestIHRMEmployee(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))

    def test02_add_emp(self):
        logging.info("app.HEADERS的值是:{}".format(app.HEADERS))
        # 发送添加员工的接口请求
        response = self.emp_api.add_emp("祖冲之", "13986350728", app.HEADERS)
        # 打印添加员工的结果
        logging.info("添加员工的结果为:{}".format(response.json()))
        # 提取员工中的令牌并把员工令牌保存到全局变量中
        app.EMP_ID = response.json().get("data").get("id")
        # 打印保存的员工ID
        logging.info("保存到全局变量的员工的ID为:{}".format(app.EMP_ID))

    def test03_query_emp(self):
        # 发送查询员工的接口请求:
        response = self.emp_api.query_emp(app.EMP_ID, app.HEADERS)
        # 打印查询员工的数据
        logging.info("查询员工的结果为:{}".format(response.json()))

    def test04_modify_emp(self):
        response = self.emp_api.modify_emp(app.EMP_ID, {"username": "******"},
                                           app.HEADERS)

        logging.info("修改员工的结果为:{}".format(response.json()))

    def test05_delete_emp(self):
        # 调用封装的删除员工接口哦,发送接口请求
        response = self.emp_api.delete_emp(app.EMP_ID, app.HEADERS)
        # 打印删除员工的结果为
        logging.info("删除员工的结果为:{}".format(response.json()))
        # 断言
        assert_common(self, 200, True, 10000, "操作成功", response)
class TestIHRMEmployeeParams(unittest.TestCase):
    # 初始化unnitte的函数
    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_DIR + "/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)
Esempio n. 3
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)