在unittest中使用 logging 模块记录测试数据的方法
如下所示:
#-*-coding:utf-8-*-
importsys
importlogging
importunittest
importos
reload(sys)
sys.path.append(os.path.dirname(os.path.abspath(__file__))+r'\..')#返回脚本的路径
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s%(filename)s[line:%(lineno)d]%(levelname)s%(message)s',
datefmt='%a,%d%b%Y%H:%M:%S',
filename='log_test.log',
filemode='w')
logger=logging.getLogger()
classSomeTest(unittest.TestCase):
deftestSomething(self):
logger.debug("this=%r",'aaa')
logger.debug("that=%r",'bbb')
#etc.
self.assertEquals(3.14,3.14,'nonono')
if__name__=="__main__":
unittest.main()
生成的日志文件内容如下:
Wed,17May201715:04:53log_test.py[line:19]DEBUGthis='aaa' Wed,17May201715:04:53log_test.py[line:20]DEBUGthat='bbb'
PyDevunittesting:Howtocapturetextloggedtoalogging.Loggerin“CapturedOutput”
以上这篇在unittest中使用logging模块记录测试数据的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。