Esempio n. 1
0
 def __init__(self):
     self.conn = pymysql.connect(host=obj_yaml.read_yaml("mysql", "host"),
                                 user=obj_yaml.read_yaml("mysql", "user"),
                                 password=obj_yaml.read_yaml(
                                     "mysql", "password"),
                                 db=obj_yaml.read_yaml("mysql", "db"),
                                 charset='utf8',
                                 cursorclass=pymysql.cursors.DictCursor)
     self.cursor = self.conn.cursor()
Esempio n. 2
0
    def create_user_roles(region, reg_name, type=1):
        url = "http://api.lemonban.com/futureloan/member/register"
        # 创建mysql对象
        obj_mysql = HandleMysql()
        # 创建请求对象
        obj = HandleRequest()
        # 添加请求头
        obj.update_headers(obj_yaml.read_yaml("register", "headers"))
        # 得到未注册的手机号
        phone = obj_mysql.create_new_mobilephone()
        params = {"mobile_phone": phone, "pwd": "12345678", "type": type, "reg_name": reg_name}
        # 发送请求,
        user = obj.send_request(url, params=params).json()
        data = {
            region: {
                "id": user['data']['id'],
                "reg_name": reg_name,
                "mobile_phone": phone,
                "pwd": "12345678",
                "type": type,
                "leave_amount": 0
            }
        }

        HandleYaml.writer_yaml(data, HandlePath().get_path("configs", "user.yaml"))
        obj.close()
        obj_mysql.close()
Esempio n. 3
0
 def __init__(self, sheet_name, file_name=None):
     if file_name is None:
         # 不传文件名,默认读取datas目录下的文件
         self.file_name = HandlePath().get_path(
             "datas", obj_yaml.read_yaml("excel", "file_name"))
     else:
         self.file_name = file_name
     self.sheet_name = sheet_name
Esempio n. 4
0
 def judge_mobile_exist(self, mobilephone):
     sql = obj_yaml.read_yaml("mysql", "select_member")
     # args传值类型type args: tuple, list or dict(源码)
     # 如果在数据库中能查到,说明不为空,返回True
     if self.select_sql(sql, args=[mobilephone]):
         return True
     else:
         return False
Esempio n. 5
0
 def test_register(self, case):
     # 获取url
     url = obj_yaml.read_yaml("api", "url") + case.url
     # 通过随机生成的手机号来替换Excel文件中的手机号
     new_data = DynamicParameter.dynamic_parameter(case.data)
     # 把字符串格式的字典获取 通过对象.属性名
     expected = eval(case.expected)
     # 发送请求,得到校验后的实际运行结果
     res = self.obj.send_request(url, params=new_data)
     # 转换为json
     result = res.json()
     # 根据excel文件,确定行号与case_id之间的关系
     row = case.case_id + 1
     try:
         # 回写实际结果到actual列,response对象.text转换为文本,这行要写在断言之前,不然断言不过的没办法回写实际结果
         self.read_writer.writer_excel(row=row, column=obj_yaml.read_yaml("msg", "column_actual"), value=res.text)
         # 断言比较 期望与实际结果
         self.assertEqual(expected["code"], result["code"], case.title)
     except AssertionError as e:
         # 输出日志到文件
         log.error(f"用例: {case.title}   结果:未通过\n具体异常为:{e}\n")
         # 如果异常,回写“未通过”数据到Excel文件
         self.read_writer.writer_excel(row=row, column=obj_yaml.read_yaml("msg", "column"),
                                       value=obj_yaml.read_yaml("msg", "value_fail"))
         # 因为异常被处理,用例执行都是成功(即不会发生断言异常),所以要往外抛出发生的异常
         raise e
     else:
         # 输出日志到文件
         log.info(f"用例: {case.title}   结果:通过")
         # 如果正常,回写“通过”数据到Excel文件
         self.read_writer.writer_excel(row=row, column=obj_yaml.read_yaml("msg", "column"),
                                       value=obj_yaml.read_yaml("msg", "value_pass"))
Esempio n. 6
0
    def get_log(cls):
        # 定义一个日志收集器
        my_log = logging.getLogger(obj_yaml.read_yaml("log", "name_collector"))
        # 设置日志收集器的级别
        my_log.setLevel(obj_yaml.read_yaml("log", "level_collector"))

        # 设置日志输出的格式
        formater = logging.Formatter(obj_yaml.read_yaml("log", "formater"))

        # 定义一个日志输出到控制台的输出渠道
        control = logging.StreamHandler()
        # 设置输出渠道的日志级别
        control.setLevel(obj_yaml.read_yaml("log", "level_control"))
        # 添加日志输出的格式
        control.setFormatter(formater)
        # 将输出渠道添加到日志收集器中
        my_log.addHandler(control)

        # 定义一个日志输出到文件的输出渠道
        path = logging.FileHandler(HandlePath().get_path(
            "logs", obj_yaml.read_yaml("log", "name_file")),
                                   encoding="UTF-8")
        # 设置输出渠道的日志级别
        path.setLevel(obj_yaml.read_yaml("log", "level_file"))
        # 添加日志输出的格式
        path.setFormatter(formater)
        # 将输出渠道添加到日志收集器中
        my_log.addHandler(path)
        # 返回log对象
        return my_log
Esempio n. 7
0
class RegisterTestCase(unittest.TestCase):
    # 定义两个类属性
    read_writer = HandleExcel(obj_yaml.read_yaml("excel", "sheet_name"))
    cases = read_writer.read_excel_by_obj()

    @classmethod
    def setUpClass(cls):
        # 创建请求对象
        cls.obj = HandleRequest()
        # 添加请求头
        cls.obj.update_headers(obj_yaml.read_yaml("api", "headers"))

    @classmethod
    def tearDownClass(cls):
        cls.obj.close()

    # ddt会自己遍历cases,每遍历一次,会把对象传给case
    @data(*cases)
    def test_register(self, case):
        # 获取url
        url = obj_yaml.read_yaml("api", "url") + case.url
        # 通过随机生成的手机号来替换Excel文件中的手机号
        new_data = DynamicParameter.dynamic_parameter(case.data)
        # 把字符串格式的字典获取 通过对象.属性名
        expected = eval(case.expected)
        # 发送请求,得到校验后的实际运行结果
        res = self.obj.send_request(url, params=new_data)
        # 转换为json
        result = res.json()
        # 根据excel文件,确定行号与case_id之间的关系
        row = case.case_id + 1
        try:
            # 回写实际结果到actual列,response对象.text转换为文本,这行要写在断言之前,不然断言不过的没办法回写实际结果
            self.read_writer.writer_excel(row=row, column=obj_yaml.read_yaml("msg", "column_actual"), value=res.text)
            # 断言比较 期望与实际结果
            self.assertEqual(expected["code"], result["code"], case.title)
        except AssertionError as e:
            # 输出日志到文件
            log.error(f"用例: {case.title}   结果:未通过\n具体异常为:{e}\n")
            # 如果异常,回写“未通过”数据到Excel文件
            self.read_writer.writer_excel(row=row, column=obj_yaml.read_yaml("msg", "column"),
                                          value=obj_yaml.read_yaml("msg", "value_fail"))
            # 因为异常被处理,用例执行都是成功(即不会发生断言异常),所以要往外抛出发生的异常
            raise e
        else:
            # 输出日志到文件
            log.info(f"用例: {case.title}   结果:通过")
            # 如果正常,回写“通过”数据到Excel文件
            self.read_writer.writer_excel(row=row, column=obj_yaml.read_yaml("msg", "column"),
                                          value=obj_yaml.read_yaml("msg", "value_pass"))
Esempio n. 8
0
    def test_register(self, case):
        # 获取url
        url = obj_yaml.read_yaml("api", "url") + case.url
        # 参数化Excel中的data列要在获取loan_id之后
        new_data = DynamicParameter.dynamic_parameter(case.data)
        # 发送请求,得到校验后的实际运行结果
        # 审核接口的请求方式为patch,加上请求方法
        res = self.obj.send_request(url, method=case.method, params=new_data)
        # 转换为json
        result = res.json()
        # 根据excel文件,确定行号与case_id之间的关系
        row = case.case_id + 1
        try:
            # 回写实际结果到actual列,response对象.text转换为文本,这行要写在断言之前,不然断言不过的没办法回写实际结果
            self.read_writer.writer_excel(row=row,
                                          column=obj_yaml.read_yaml(
                                              "msg", "column_actual"),
                                          value=res.text)
            # 断言比较 期望与实际结果
            self.assertEqual(case.expected, result["code"], case.title)
        except AssertionError as e:
            # 输出日志到文件
            log.error(f"用例: {case.title}   结果:未通过\n具体异常为:{e}\n")

            # 如果异常,回写“未通过”数据到Excel文件
            self.read_writer.writer_excel(
                row=row,
                column=obj_yaml.read_yaml("msg", "column"),
                value=obj_yaml.read_yaml("msg", "value_fail"))
            # 因为异常被处理,用例执行都是成功(即不会发生断言异常),所以要往外抛出发生的异常
            raise e
        else:
            if 'token_info' in res.text:
                # 取登录后的token
                token = result['data']['token_info']['token']
                # 更新请求头
                new_headers = {"Authorization": "Bearer " + token}
                self.obj.update_headers(new_headers)
            # 得到loan_id
            if case.sql:
                # Excel文件中存在sql列,则参数化sql
                sql = DynamicParameter.dynamic_parameter(case.sql)
                # 发送请求前,查loan表得到标的Id,执行完sql,得到的是一个dict
                loan_id = self.obj_mysql.select_sql(sql).get('id')
                # 通过setattr设置loan_id,作为属性值赋给DynamicParameter
                # 动态创建属性的机制, 来解决接口依赖的问题
                setattr(DynamicParameter, "loan_id", loan_id)
            # 输出日志到文件
            log.info(f"用例: {case.title}   结果:通过")
            # 如果正常,回写“通过”数据到Excel文件
            self.read_writer.writer_excel(
                row=row,
                column=obj_yaml.read_yaml("msg", "column"),
                value=obj_yaml.read_yaml("msg", "value_pass"))
Esempio n. 9
0
    def test_register(self, case):
        # 获取url
        url = obj_yaml.read_yaml("api", "url") + case.url
        # 通过随机生成的手机号来替换Excel文件中的手机号
        new_data = DynamicParameter.dynamic_parameter(case.data)

        # 发送请求,得到校验后的实际运行结果
        res = self.obj.send_request(url, params=new_data)
        # 转换为json
        result = res.json()
        # 自己设计的Excel中第二条为登录成功的用例
        if case.case_id == 2:
            # 取登录后的token
            token = result['data']['token_info']['token']
            # 更新请求头
            new_headers = {"Authorization": "Bearer " + token}
            self.obj.update_headers(new_headers)
Esempio n. 10
0
    def test_register(self, case):
        # 获取url
        url = obj_yaml.read_yaml("api", "url") + case.url
        # 通过随机生成的手机号来替换Excel文件中的手机号
        new_data = DynamicParameter.dynamic_parameter(case.data)
        if case.sql:
            # Excel文件中存在sql列,则参数化sql
            sql = DynamicParameter.dynamic_parameter(case.sql)
            # 发送请求前,查member表得到充值前的金额,转化为float
            leave_amount_before = float(self.obj_mysql.select_sql(sql)['leave_amount'])
            # 保留2位小数
            leave_amount_before = round(leave_amount_before, 2)

        # 发送请求,得到校验后的实际运行结果
        res = self.obj.send_request(url, params=new_data)
        # 转换为json
        result = res.json()
        # 自己设计的Excel中第二条为登录成功的用例
        if case.case_id == 2:
            # 取登录后的token
            token = result['data']['token_info']['token']
            # 更新请求头
            new_headers = {"Authorization":"Bearer "+token}
            self.obj.update_headers(new_headers)
Esempio n. 11
0
 def setUpClass(cls):
     # 创建请求对象
     cls.obj = HandleRequest()
     # 添加请求头
     cls.obj.update_headers(obj_yaml.read_yaml("api", "headers"))
Esempio n. 12
0
class AddTestCase(unittest.TestCase):
    # 定义两个类属性
    read_writer = HandleExcel("add")
    cases = read_writer.read_excel_by_obj()

    @classmethod
    def setUpClass(cls):
        # 创建请求对象
        cls.obj = HandleRequest()
        # 添加请求头
        cls.obj.update_headers(obj_yaml.read_yaml("api", "headers"))
        cls.obj_mysql = HandleMysql()

    @classmethod
    def tearDownClass(cls):
        cls.obj.close()
        cls.obj_mysql.close()

    # ddt会自己遍历cases,每遍历一次,会把对象传给case
    @data(*cases)
    def test_register(self, case):
        # 获取url
        url = obj_yaml.read_yaml("api", "url") + case.url
        # 通过随机生成的手机号来替换Excel文件中的手机号
        new_data = DynamicParameter.dynamic_parameter(case.data)

        # 发送请求,得到校验后的实际运行结果
        res = self.obj.send_request(url, params=new_data)
        # 转换为json
        result = res.json()
        # 自己设计的Excel中第二条为登录成功的用例
        if case.case_id == 2:
            # 取登录后的token
            token = result['data']['token_info']['token']
            # 更新请求头
            new_headers = {"Authorization": "Bearer " + token}
            self.obj.update_headers(new_headers)
        # 根据excel文件,确定行号与case_id之间的关系
        row = case.case_id + 1
        try:
            # 回写实际结果到actual列,response对象.text转换为文本,这行要写在断言之前,不然断言不过的没办法回写实际结果
            self.read_writer.writer_excel(row=row,
                                          column=obj_yaml.read_yaml(
                                              "msg", "column_actual"),
                                          value=res.text)
            # 断言比较 期望与实际结果
            self.assertEqual(case.expected, result["code"], case.title)
        except AssertionError as e:
            # 输出日志到文件
            log.error(f"用例: {case.title}   结果:未通过\n具体异常为:{e}\n")
            # 如果异常,回写“未通过”数据到Excel文件
            self.read_writer.writer_excel(
                row=row,
                column=obj_yaml.read_yaml("msg", "column"),
                value=obj_yaml.read_yaml("msg", "value_fail"))
            # 因为异常被处理,用例执行都是成功(即不会发生断言异常),所以要往外抛出发生的异常
            raise e
        else:
            # 输出日志到文件
            log.info(f"用例: {case.title}   结果:通过")
            # 如果正常,回写“通过”数据到Excel文件
            self.read_writer.writer_excel(
                row=row,
                column=obj_yaml.read_yaml("msg", "column"),
                value=obj_yaml.read_yaml("msg", "value_pass"))
Esempio n. 13
0
class RechargeTestCase(unittest.TestCase):
    # 定义两个类属性
    read_writer = HandleExcel("recharge")
    cases = read_writer.read_excel_by_obj()

    @classmethod
    def setUpClass(cls):
        # 创建请求对象
        cls.obj = HandleRequest()
        # 添加请求头
        cls.obj.update_headers(obj_yaml.read_yaml("api", "headers"))
        cls.obj_mysql = HandleMysql()

    @classmethod
    def tearDownClass(cls):
        cls.obj.close()
        cls.obj_mysql.close()

    # ddt会自己遍历cases,每遍历一次,会把对象传给case
    @data(*cases)
    def test_register(self, case):
        # 获取url
        url = obj_yaml.read_yaml("api", "url") + case.url
        # 通过随机生成的手机号来替换Excel文件中的手机号
        new_data = DynamicParameter.dynamic_parameter(case.data)
        if case.sql:
            # Excel文件中存在sql列,则参数化sql
            sql = DynamicParameter.dynamic_parameter(case.sql)
            # 发送请求前,查member表得到充值前的金额,转化为float
            leave_amount_before = float(self.obj_mysql.select_sql(sql)['leave_amount'])
            # 保留2位小数
            leave_amount_before = round(leave_amount_before, 2)

        # 发送请求,得到校验后的实际运行结果
        res = self.obj.send_request(url, params=new_data)
        # 转换为json
        result = res.json()
        # 自己设计的Excel中第二条为登录成功的用例
        if case.case_id == 2:
            # 取登录后的token
            token = result['data']['token_info']['token']
            # 更新请求头
            new_headers = {"Authorization":"Bearer "+token}
            self.obj.update_headers(new_headers)
        # 根据excel文件,确定行号与case_id之间的关系
        row = case.case_id + 1
        try:
            # 回写实际结果到actual列,response对象.text转换为文本,这行要写在断言之前,不然断言不过的没办法回写实际结果
            self.read_writer.writer_excel(row=row, column=obj_yaml.read_yaml("msg", "column_actual"), value=res.text)
            # 断言比较 期望与实际结果
            self.assertEqual(case.expected, result["code"], case.title)
            # 如果code断言成功, 且case.sql不为空, 则获取充值之后的金额
            if case.sql:
                # 从响应结果中获取充值后余额
                leave_amount_after = round(float(self.obj_mysql.select_sql(sql)['leave_amount']),2)
                # 获取差值,调试出来的,要保留两位小数断言才准确
                leave_amount = round(float(leave_amount_after - leave_amount_before),2)
                # 获取充值金额,通过json转换为python识别的字典格式
                new_data = json.loads(new_data)
                # 断言比较 充值金额与  充值后金额-充值前金额  字典用get方法获取更合适
                self.assertEqual(new_data.get("amount"), leave_amount, case.title)

        except AssertionError as e:
            # 输出日志到文件
            log.error(f"用例: {case.title}   结果:未通过\n具体异常为:{e}\n")
            # 如果异常,回写“未通过”数据到Excel文件
            self.read_writer.writer_excel(row=row, column=obj_yaml.read_yaml("msg", "column"),
                                          value=obj_yaml.read_yaml("msg", "value_fail"))
            # 因为异常被处理,用例执行都是成功(即不会发生断言异常),所以要往外抛出发生的异常
            raise e
        else:
            # 输出日志到文件
            log.info(f"用例: {case.title}   结果:通过")
            # 如果正常,回写“通过”数据到Excel文件
            self.read_writer.writer_excel(row=row, column=obj_yaml.read_yaml("msg", "column"),
                                          value=obj_yaml.read_yaml("msg", "value_pass"))