pytest log_cli+标准日志记录=中断doctest

2024-05-15 22:50:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我们使用了pytest和python stdlogging,并在doctest中进行了一些测试。我们希望启用log_cli以使ide中的调试测试更容易(让stderr流到“实时”控制台,以便在单步执行时可以看到输出的日志语句)。问题是,“使用logging”(例如存在对logger.info("...")的调用等)之间似乎存在错误/操作

我在文档中没有看到任何其他标志或提及这一点,因此这似乎是一个bug,但希望有一个解决方法

该测试模块通过:

# bugjar.py
"""
>>> dummy()
'retval'
"""
import logging
logger = logging.getLogger(__name__)
def dummy():
    # logger.info("AnInfoLog")  ## un-comment to break test
    return "retval"

但是取消对logger.info(调用的注释(没有其他更改)会导致失败:(除非我从pytest.ini中删除log_cli

002 >>> dummy()
Expected:
    'retval'
Got nothing

这是我的命令行&;相关版本输出:

(venv) $ ./venv/bin/pytest -c ./pytest.ini ./bugjar.py
======================================================================= test session starts ========================================================================
platform linux -- Python 3.8.5, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
rootdir: -omitted-/test-unit, configfile: ./pytest.ini
plugins: forked-1.3.0, xdist-2.2.1, profiling-1.7.0
collected 1 item                                                                                                                                                   

bugjar.py::bugjar
 
bugjar.py::bugjar 
-------------------------------------------------------------------------- live log call ---------------------------------------------------------------------------
INFO     bugjar:bugjar.py:8 AnInfoLog
FAILED                                                                                                                                                       [100%]

============================================================================= FAILURES =============================================================================
_________________________________________________________________________ [doctest] bugjar _________________________________________________________________________
001 
002 >>> dummy()
Expected:
    'retval'
Got nothing

和mypytest.ini(注意,逗号是正确的,通过/失败不受日志文件或其他addopt使用的影响

[pytest]
addopts = --doctest-modules # --profile --profile-svg 
norecursedirs = logs bin tmp* scratch venv* data
# log_file = pytest.log
log_cli = true
log_cli_level=debug

pytest.ini中删除log_cli*会使问题消失

这显然与log_cli在捕获输出以供doctest本身使用时所操作的内容有关,但也与预期的行为无关

我希望我犯了一个错误,或者在bash或IDE shell窗口/调试器中有一个解决方法来获得实时日志输出


Tags: pytestinfologclivenvpytestlogging