Example #1
0
 def connect(self,
             host,
             port,
             user,
             password,
             database,
             charset="utf8",
             cursorclass=DictCursor,
             **kwargs):
     """
     连接数据库
     :param host: 主机地址
     :param port: 端口号
     :param user: 用户名
     :param password: 密码
     :param database: 数据库名称
     :param charset: 字符编码
     :param cursorclass: 设置输出格式为字典格式
     :param kwargs:
     :return: True/False
     """
     try:
         self.conn = pymysql.connect(host=host,
                                     port=port,
                                     user=user,
                                     password=password,
                                     database=database,
                                     charset=charset,
                                     cursorclass=cursorclass,
                                     **kwargs)
     except Exception as e:
         logger.error("连接数据库失败 {}".format(e))
         raise e
     self.cur = self.conn.cursor()
     return self
Example #2
0
def rm_log():
    """
    清除2天前的日志
    :return:
    """
    try:
        # 获取log目录下的log文件
        log_list = os.listdir(PyConfig.log_path)
    except FileNotFoundError as e:
        logger.error("文件路径错误")
        raise e
    length = len(log_list)
    while length > 0:
        for i in log_list:
            # 获取log文件的绝对路径
            path = r"%s\%s" % (PyConfig.log_path, i)
            # 获取log文件最后修改时的时间戳
            create = os.stat(path).st_mtime
            # 获取当前时间的时间戳
            now = time.time()
            # 进行判断是否大于两天
            if (now - create) / 24 / 60 / 60 > 2:
                try:
                    os.remove(path)
                except Exception as e:
                    logger.error("删除日志失败")
                    raise e
            length -= 1
        logger.info("没有2天前的日志了")
Example #3
0
 def test_register(self, init_req, data_info):
     # 对excel中的固定的数据进行替换
     data_info["data"] = replace_data("user_info", data_info["data"])
     # 对excel中的不固定数据进行替换
     if "*phone*" in data_info["data"]:
         phone = get_phone()
         data_info["data"] = data_info["data"].replace("*phone*", phone)
     # 发送请求
     result = init_req.run_main(method=data_info['method'],
                                url=self.host + data_info["url"],
                                data=json.loads(data_info["data"]))
     # 获取状态码
     code = result.json()["code"]
     try:
         # 进行断言
         # 1.状态码断言
         assert code == str(data_info["expected"])
         # 2.查看数据库是否新增对应的新用户
         # ...
         logger.info("PASS")
         excel_handler.write_excel('registe', data_info["case_id"] + 1, 9,
                                   "pass")
     except AssertionError as e:
         logger.error("FAIL")
         excel_handler.write_excel('registe', data_info["case_id"] + 1, 9,
                                   "fail")
         raise e
Example #4
0
 def write_excel(file_path, sheet_name, row, column, data):
     """
     写入excel
     """
     try:
         wb = openpyxl.load_workbook(file_path)
         sheet = wb[sheet_name]
     except Exception as e:
         logger.error("打开excel失败")
         raise e
     cell = sheet.cell(row, column)
     cell.value = data
     wb.save(file_path)
     logger.info("写入excel {}成功,数据:{}".format(sheet_name, data))
Example #5
0
 def execute(self, sql):
     """
     执行sql,主要用于写入操作
     :param sql: sql语句
     :return: True/False
     """
     if self.conn and self.cur:
         try:
             self.cur.execute(sql)
         except Exception as e:
             logger.error("执行sql失败{}".format(sql))
             raise e
         self.conn.commit()
         logger.info("执行sql成功{}".format(sql))
         return True
Example #6
0
def replace_data(yaml_key, data_info):
    """
    替换测试用例中固定的数据
    :return:
    """
    info = yaml.read_yaml()[yaml_key]
    pattern = "#(.*?)#"
    while re.findall(pattern, data_info):
        key = re.search(pattern, data_info).group(1)
        try:
            data_info = re.sub(pattern, str(info[key]), data_info, 1)
        except Exception as e:
            logger.error("替换excel表中固定的{}数据失败".format(key))
            raise e
    return data_info
Example #7
0
def sub_data(session, data_info):
    """
    利用正则替换哟用例中上下文关联的数据
    :param session:
    :param data_info:
    :return:
    """
    pattern = "@(.*?)@"
    while re.findall(pattern, data_info):
        key = re.search(pattern, data_info).group(1)
        try:
            data_info = re.sub(pattern, str(getattr(Context(session), key)),
                               data_info, 1)
        except Exception as e:
            logger.error("替换excel表中上下关联的{}数据失败".format(key))
            raise e
    return data_info