脚本专栏 
首页 > 脚本专栏 > 浏览文章

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

(编辑:jimmy 日期: 2025/11/15 浏览:3 次 )

如下所示:

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

上一篇:对python自动生成接口测试的示例讲解
下一篇:浅谈python在提示符下使用open打开文件失败的原因及解决方法
一句话新闻
微软与英特尔等合作伙伴联合定义“AI PC”:键盘需配有Copilot物理按键
几个月来,英特尔、微软、AMD和其它厂商都在共同推动“AI PC”的想法,朝着更多的AI功能迈进。在近日,英特尔在台北举行的开发者活动中,也宣布了关于AI PC加速计划、新的PC开发者计划和独立硬件供应商计划。
在此次发布会上,英特尔还发布了全新的全新的酷睿Ultra Meteor Lake NUC开发套件,以及联合微软等合作伙伴联合定义“AI PC”的定义标准。