使用日志记录时在teardown中出现Pytest错误

2024-05-05 15:09:27 发布

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

当我运行我的测试时,如果我在测试产生后使用ctrl-c(键盘中断)取消它,它应该在yield语句之后继续运行代码。但是,如果我在启用日志记录的情况下运行它:

pytest test_logging_error.py --log-cli-level info

它会导致错误:

^{pr2}$

它在记录消息“It crash here”后执行此操作,代码如下。在

from time import sleep
import pytest
import logging


@pytest.fixture(scope="module")
def connection():
    numbers = []
    for i in range(10):
        numbers.append(i)
    yield numbers
    logging.info("It crashes here")
    for i in range(10):
        print("foo")


def test_error(connection):
    logging.info("Running a test")
    sleep(10000)

如果我在产量之后不进行日志记录或者只是在禁用日志记录的情况下运行它,它运行得很好,并且拆卸操作也会正常完成。在


Tags: 代码testimportinfoherepytestloggingdef
1条回答
网友
1楼 · 发布于 2024-05-05 15:09:27

这是一个实际的pytest错误,我相信https://github.com/pytest-dev/pytest/pull/4487修复了它(它刚刚被合并到master中)

但是多亏了https://github.com/pytest-dev/pytest/issues/4500中的bug报告,我们现在有了一个针对该问题的最小复制器,可以将其作为回归测试添加进来

对于额外的clairty,请与master的pytest进行验证,如果问题得到解决,我会在有时间的时候继续将测试添加到testsuite中

谢谢:+1:

相关问题 更多 >