目前在做自动化接口测试,遇到个问题,就是跑用例的模块内报错时,就会判断该用例执行失败,但是一旦该模块调用其他模块后,在其他模块内报错了,就显示用例通过,是因为我 logging 没封装好导致的嘛?
控制台输出如上图,明明报错了,却还是显示了用例通过了。
这是我的封装的 log 类,有什么毛病嘛
log_path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'logs')
if not os.path.exists(log_path):
os.mkdir(log_path)
class Log:
"""
log日志类
"""
def __init__(self):
self.logname = os.path.join(log_path, '%s.log' % time.strftime('%Y_%m_%d'))
self.logger = logging.getLogger()
self.logger.setLevel(logging.DEBUG)
self.formatter = logging.Formatter('[%(asctime)s]-%(filename)s:%(lineno)d]-%(levelname)s:%(message)s')
def __console(self, level, message):
fh = logging.FileHandler(self.logname, 'a', 'utf-8')
fh.setLevel(logging.DEBUG)
fh.setFormatter(self.formatter)
self.logger.addHandler(fh)
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
ch.setFormatter(self.formatter)
self.logger.addHandler(ch)
if level == 'info':
self.logger.info(message)
elif level == 'debug':
self.logger.debug(message + '\n' + traceback.format_exc())
elif level == 'warning':
self.logger.warning(message + '\n' + traceback.format_exc())
elif level == 'error':
self.logger.error(message + '\n' + traceback.format_exc())
self.logger.removeHandler(ch)
self.logger.removeHandler(fh)
fh.close()
def debug(self, message):
self.__console('debug', message)
def info(self, message):
self.__console('info', message)
def warning(self, message):
self.__console('warning', message)
def error(self, message):
self.__console('error', message)