接口自动化测试框架实战日志和数据库的封装
log日志的封装import datetime import logging import os from config import Conf from config.Conf import ConfigYaml log_l = { "info": logging.INFO, "debug": logging.DEBUG, "warning": logging.WARNING, "error": logging.ERROR } class Logger(): def __init__(self, log_file, log_name, log_level): self.log_file = log_file self.log_name = log_name self.log_level = log_level #设置logger名称 self.logger = logging.getLogger(self.log_name) self.logger.setLevel(log_l[self.log_level]) formatter = logging.Formatter("%(asctime)s-%(name)s-%(levelname)s-%(message)s") if not self.logger.handlers: # 输出到控制台 self.fh_stream = logging.StreamHandler() self.fh_stream.setLevel(log_l[self.log_level]) self.fh_stream.setFormatter(formatter) self.logger.addHandler(self.fh_stream) #写入文件 self.fh_file = logging.FileHandler(self.log_file) self.fh_file.setLevel(log_l[self.log_level]) self.fh_file.setFormatter(formatter) self.logger.addHandler(self.fh_file) #获取日志文件存储路径 log_path = Conf.get_log_path() current_time = datetime.datetime.now().strftime("%Y-%m-%d") log_extension=ConfigYaml().get_conf_log_extension() #日志文件名称 logfile=os.path.join(log_path,current_time+log_extension) #获取日志级别(放在配置文件里面,方便根据需求修改) loglevel = ConfigYaml().get_conf_log() def my_log(log_name=__file__): return Logger(log_file=logfile,log_name=log_name,log_level=loglevel).logger if __name__ == "__main__": my_log().info("123456") 数据库的封装import pymysql from utils.LogUtil import my_log class MySql(): def __init__(self,host,user,password,database,charset,port): self.log = my_log("database") self.conn = pymysql.connect( host=host, user=user, password=password, database=database, charset=charset, port=port ) self.cursor=self.conn.cursor(pymysql.cursors.DictCursor) def fetchone(self, sql): self.cursor.execute(sql) return self.cursor.fetchone() def fetchall(self, sql): """ 多个查询 """ self.cursor.execute(sql) return self.cursor.fetchall() def exec(self, sql): """ 执行 """ try: if self.conn and self.cursor: self.cursor.execute(sql) self.conn.commit() except Exception as ex: self.conn.rollback() self.log.error("Mysql 执行失败") self.log.error(ex) return False return True # 4、关闭对象 def __del__(self): # 关闭光标对象 if self.cursor is not None: self.cursor.close() # 关闭连接对象 if self.conn is not None: self.cursor.close() if __name__ == "__main__": mysql = MySql("xxxxxxx", "test", "test123456","xxxxxx", charset="utf8", port=7090) res = mysql.fetchall("select username,password from tb_users") # res = mysql.exec("update tb_users set first_name="python" where username = "python"") print("1111",res)
为什么阿里可以频繁的架构调整?(附CEO张勇公开信)11月26日,阿里巴巴集团CEO张勇一纸公开信打破周一市场的平静。阿里巴巴宣布组织架构调整,技术电商等多个核心业务高层变更阿里几乎每一年都会发起一次不小规模的组织架构调整,特别最近
今年秋季MacBookPro将迎来六大变化新款MacBookPro有望让Apple的旗舰笔记本电脑焕然一新,并为未来的发布奠定基础。让我们来看看我们可以在下一代MacBookPro中看到什么,以及这些新功能将如何将笔记本电
苹果电脑怎么查保修期查询?我相信很多朋友刚拥有Mac计算机或笔记本时,不知道自己机器的保修查询方法。macw小编现给大家带来了这个小入门技巧,请跟随小编观看此教程!我们可以到苹果的官方查询网输入序列号,即可
围观中,新的AppleWatch显示屏尺寸和外观早些时候,有媒体报道了即将发布的苹果手表系列7的新细节,包括更大的屏幕尺寸。此前,人们对下一代AppleWatch知之甚少,只知道它显然采用了让人想起iPhone12和iPadPr
如何彻底卸载不需要的Mac屏保想知道怎么删除Mac电脑屏保吗?该怎么删除不喜欢的屏保呢?可能很多新接触mac电脑的小伙伴不是很熟悉,本次小编为您带来几种彻底卸载Mac屏保的方法,想知道的小伙伴千万不要错过哦!欢
MacOS系统无法连接5gwifi的解决办法,你确定不来看看吗?MBP突然连不上5GWifi,但是用Wifi却可以,用手机平板使用5GWIFI一切正常,是什么问题呢?重启路由器重启电脑路由器刷机路由器恢复出厂设置,一番折腾下来,费时又费力,终于
Mac新手必看,这些设置可以让你的Mac更好用如果你刚买了一台新的Mac,不免要对Mac做一些设置,现为你提供一些Mac常用的设置技巧,让你的Mac更加好用。访达设置访达是我们在使用Mac时最常用的应用了,每次打开访达窗口,会
解锁Mac上自带的计算机的隐藏功能不要小看macOs的计算器App只要点按几下,你就能发现它简洁外表下潜藏着的强大功能。轻松转换想在人民币与美元公里与英里之间转换?只要输入一个数值,再在转换菜单中选择单位类别(面积
iphone13价格官网报价已有,快看是否超出预算目前距离Apple预计揭开新款iPhone13的面纱只有几周的时间,自然而然,越来越多的信息通过非官方渠道成为头条新闻。这一次,轮到iPhone13的价格上网了,因为一份报告显示了
新款MacBook带TouchBar如何开机和强制关机新款的MacBook前排的物理按键换成了TouchBar,虽然整体上没太大的变化,但是!但是!如果没有启动,连屏幕都显示不了啊,那怎么开机呢?开启Mac(开机)开启带有TouchB
如何在MacOSMonterey中使用实时文本在2021年6月的WWDC上,Apple在MacOSMonterey中宣布了一项名为LiveText的简洁新功能。这使您可以突出显示任何照片中的文本并以多种方式使用它。您可以复制和