Logging 即使禁用日志文件捕获,Pytest也会捕获日志文件
使用Logging 即使禁用日志文件捕获,Pytest也会捕获日志文件,logging,pytest,traceback,Logging,Pytest,Traceback,使用--capture=sys运行pytest应该只捕获stdout/stderr,但是如果我这样做了--tb=auto--capture=sys,则回溯仍然包括只应路由到日志文件的调试语句。即使使用-s,我也会看到相同的行为 我有很多调试语句被记录到一个我不想在控制台上看到的文件中。因此,我将logging.StreamHandler记录器设置为logging.INFO,将logging.FileHandler设置为logging.DEBUG 当我运行测试时,如果出现故障并打印了回溯,如果我使
--capture=sys
运行pytest应该只捕获stdout/stderr,但是如果我这样做了--tb=auto--capture=sys
,则回溯仍然包括只应路由到日志文件的调试语句。即使使用-s
,我也会看到相同的行为
我有很多调试语句被记录到一个我不想在控制台上看到的文件中。因此,我将logging.StreamHandler
记录器设置为logging.INFO
,将logging.FileHandler
设置为logging.DEBUG
当我运行测试时,如果出现故障并打印了回溯,如果我使用--tb=auto
甚至--tb=short
,它总是打印捕获的日志调用
和捕获的标准输出调用
部分。阻止这种情况发生的唯一方法是使用--tb=no
,但随后我会丢失有价值的回溯信息
最简单的示例(我调用了文件test\u basic.py
):
导入日志
logger=logging.getLogger()
logger.setLevel(logging.DEBUG)
fh=logging.FileHandler(filename=“log.txt”)
fh.setLevel(记录.调试)
logger.addHandler(fh)
sh=logging.StreamHandler()
sh.setLevel(logging.INFO)
logger.addHandler(sh)
def test_one():
logger=logging.getLogger(“test_one”)
logger.info(“信息级别的日志记录”)
debug(“调试级别的日志记录”)
断言错误
python-m pytest--tb=auto--capture=sys的输出
==================================================== FAILURES ====================================================
____________________________________________________ test_one ____________________________________________________
def test_one():
logger = logging.getLogger("test_one")
logger.info(f"Logging at INFO level")
logger.debug(f"Logging at DEBUG level")
> assert False
E assert False
test_basic.py:19: AssertionError
---------------------------------------------- Captured stderr call ----------------------------------------------
Logging at INFO level
----------------------------------------------- Captured log call ------------------------------------------------
INFO test_one:test_basic.py:17 Logging at INFO level
DEBUG test_one:test_basic.py:18 Logging at DEBUG level
============================================ 1 failed in 0.09 seconds ============================================
期望输出:
==================================================== FAILURES ====================================================
____________________________________________________ test_one ____________________________________________________
def test_one():
logger = logging.getLogger("test_one")
logger.info(f"Logging at INFO level")
logger.debug(f"Logging at DEBUG level")
> assert False
E assert False
test_basic.py:19: AssertionError
---------------------------------------------- Captured stderr call ----------------------------------------------
Logging at INFO level
============================================ 1 failed in 0.09 seconds ============================================
如果我使用--tb=no
我将除去捕获的日志调用
部分以及所有回溯信息:
============================================== test session starts ===============================================
test_basic.py F [100%]
============================================ 1 failed in 0.07 seconds ============================================
我如何才能实现我的预期行为?答案是--show capture=stdout
然而,有一点让人困惑的是,禁用捕获并不能做到这一点。我希望-s
覆盖--show capture=all
(默认设置)