首页 > 解决方案 > 使用日志记录时拆解中的 Pytest 错误

问题描述

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

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

它会导致错误:

AttributeError: 'NoneType' object has no attribute 'resume_capturing'

它在记录消息“它在此处崩溃”后执行此操作,这是代码。

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)

如果我在产量后不记录或仅禁用记录就运行它,它运行良好并且拆解正确完成。

标签: pythonloggingpytest

解决方案


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

但是由于https://github.com/pytest-dev/pytest/issues/4500中的错误报告,我们现在有一个最小的复制器,可以作为回归测试添加

如果问题得到解决,请通过 master 的 pytest 进行验证,如果问题得到解决,我会在找到时间时将测试添加到测试套件中。

谢谢:+1:


推荐阅读