Python 为什么当我运行pytest时,日志记录程序不进行传播

Python 为什么当我运行pytest时,日志记录程序不进行传播,python,logging,pytest,Python,Logging,Pytest,我无法从单元测试中获取日志输出,原因似乎是在运行pytest时,除了根日志记录器之外,所有日志记录器都已将propagate设置为false。我可以使用pytest和python test\u file.py运行以下文件。在前者中logging.getLogger('one').propagate==True,而在后者中logging.getLogger('one').propagate==False。为什么会这样 test_file.py import logging def test_fu

我无法从单元测试中获取日志输出,原因似乎是在运行pytest时,除了根日志记录器之外,所有日志记录器都已将propagate设置为false。我可以使用
pytest
python test\u file.py
运行以下文件。在前者中
logging.getLogger('one').propagate==True
,而在后者中
logging.getLogger('one').propagate==False
。为什么会这样

test_file.py

import logging

def test_function():
  print()
  print('root', logging.getLogger().propagate)
  print('one', logging.getLogger('one').propagate)

if __name__ == '__main__':
  test_function()

我如何让我所有的伐木者传播?搜索互联网只会发现关于如何关闭传播的问题,就好像大多数人都有与我相反的经历一样。

这不是pytest的问题。我在cpython和pytest源代码中搜索了任何可以这样做的东西,但没有找到任何东西。然后我仔细查看了getLogger代码,发现有人在调用logging.setLoggerClass函数,并将logger类替换为设置为false的子类。我在setLoggerClass上设置了一个断点,发现ROS2提供的pytest插件正在执行此操作。我在pytest.ini中添加了以下内容,现在一切都很好

[pytest]
addopts = -p no:launch -p no:launch_ros