在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”
相关推荐
taiyangshenniao 2019-12-02
Testingba工作室 2020-07-04
deadgrape 2020-06-21
上班打发 2020-06-14
HappinessCat 2020-06-10
ITstudied 2020-06-08
woxxoole 2020-06-08
xiaoyezihanghui 2020-06-07
Dipa 2020-06-06
测试自动化顾问 2020-06-04
gaitiangai 2020-06-03
Henryztong 2020-06-02
woxxoole 2020-06-01
abdstime 2020-06-01
today0 2020-06-01
HappinessCat 2020-06-01
RocketJ 2020-06-01
86427019 2020-06-01