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

时间:2022-07-27
本文章向大家介绍在unittest中使用 logging 模块记录测试数据的方法,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

如下所示:

# -*- 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 模块记录测试数据的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持网站事(zalou.cn)。

您可能感兴趣的文章:

  • Python单元测试框架unittest使用方法讲解
  • 解读python logging模块的使用方法
  • Python 单元测试(unittest)的使用小结
  • Python中内置的日志模块logging用法详解
  • Python中的测试模块unittest和doctest的使用教程
  • Python中使用logging模块打印log日志详解
  • Python使用logging模块实现打印log到指定文件的方法