在unittest中使用 logging 模块记录测试数据的方法

(编辑:jimmy 日期: 2024/9/29 浏览:2)

如下所示:

# -*- coding:utf-8 -*-
import sys
import logging
import unittest
import os

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()


class SomeTest(unittest.TestCase):
 def testSomething(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, 17 May 2017 15:04:53 log_test.py[line:19] DEBUG this= 'aaa'
Wed, 17 May 2017 15:04:53 log_test.py[line:20] DEBUG that= 'bbb'

PyDev unittesting: How to capture text logged to a logging.Logger in “Captured Output”

以上这篇在unittest中使用 logging 模块记录测试数据的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

一句话新闻

Windows上运行安卓你用过了吗
在去年的5月23日,借助Intel Bridge Technology以及Intel Celadon两项技术的驱动,Intel为PC用户带来了Android On Windows(AOW)平台,并携手国内软件公司腾讯共同推出了腾讯应用宝电脑版,将Windows与安卓两大生态进行了融合,PC的使用体验随即被带入到了一个全新的阶段。