Oscar Bray
Oscar Bray

Reputation: 23

Pytest error in teardown when using logging

When I run my test if I cancel it using ctrl-c (keyboard interrupt) after it has yielded, it should continue running the code after the yield statement. However if I run it with logging enabled:

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

It causes an error:

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

It does this after it logs the message "It crashes here", here is the code.

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)

If I run it without logging after the yield or just with logging disabled it runs fine and the teardown finishes properly.

Upvotes: 2

Views: 2927

Answers (1)

user78110
user78110

Reputation:

this is an actual pytest bug, i believe https://github.com/pytest-dev/pytest/pull/4487 fixes it (its just been merged into master)

but thanks to the bug report made in https://github.com/pytest-dev/pytest/issues/4500 we now have a minimal reproducer for that issue which cna be added as a regression test

for additional clairty please verify with pytest from master if the issue is resolved, i'll follow up with adding the test to the testsuite when i find time

thanks :+1:

Upvotes: 1

Related Questions