Skip to main content
 首页 » 编程设计

python之Nose 测试 : how to show customized logging

2025年02月15日10jackei

我有以下 python 文件 fct.py。如果我直接在 python 或 ipython 中运行 test1() 或 test2(),我会得到:

In [315]: test1() 
fct - fct.py[line:9] 2017-01-02 17:23:22,992 : DEBUG :  debug output 
 
In [316]: test2() 
fct - fct.py[line:9] 2017-01-02 17:23:26,393 : DEBUG :  debug output     

如果我跑

nosetests -v fct.py 

我得到:

test1 logging ... ok 
test2 logging ... ok 
Ran 2 tests in 0.000s 
 
OK     

没有日志输出“调试输出”。

如何在 nosetests 中获取日志消息“调试输出”?我阅读了 nosetests -h 和谷歌,但似乎找不到解决方案。感谢您的帮助或指点。

''' fct.py '''  
import logging 
 
LOGGER = logging.getLogger(__name__) 
LOGGER.addHandler(logging.NullHandler()) 
 
def fct(): 
    ''' fct logging ''' 
    LOGGER.debug(" debug output") 
 
def test1(): 
    ''' test1 logging ''' 
    fct() 
    assert 1==1 
 
def test2(): 
    ''' test2 logging ''' 
    logging.basicConfig(level=logging.DEBUG) 
    fct() 
    assert 1==1 

请您参考如下方法:

nosetests --nologcapture fct.py 

会成功的