Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion changelog/4487.bugfix.rst

This file was deleted.

1 change: 1 addition & 0 deletions changelog/4500.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
When a fixture yields and a log call is made after the test runs, and, if the test is interrupted, capture attributes are ``None``.
56 changes: 45 additions & 11 deletions testing/test_capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,14 +302,14 @@ def test_logging_and_immediate_setupteardown(self, testdir):
"""\
import logging
def setup_function(function):
logging.warn("hello1")
logging.warning("hello1")

def test_logging():
logging.warn("hello2")
logging.warning("hello2")
assert 0

def teardown_function(function):
logging.warn("hello3")
logging.warning("hello3")
assert 0
"""
)
Expand All @@ -328,14 +328,14 @@ def test_logging_and_crossscope_fixtures(self, testdir):
"""\
import logging
def setup_module(function):
logging.warn("hello1")
logging.warning("hello1")

def test_logging():
logging.warn("hello2")
logging.warning("hello2")
assert 0

def teardown_module(function):
logging.warn("hello3")
logging.warning("hello3")
assert 0
"""
)
Expand All @@ -354,7 +354,7 @@ def test_conftestlogging_is_shown(self, testdir):
"""\
import logging
logging.basicConfig()
logging.warn("hello435")
logging.warning("hello435")
"""
)
# make sure that logging is still captured in tests
Expand All @@ -375,7 +375,7 @@ def test_conftestlogging_and_test_logging(self, testdir):
"""\
def test_hello():
import logging
logging.warn("hello433")
logging.warning("hello433")
assert 0
"""
)
Expand All @@ -385,6 +385,40 @@ def test_hello():
assert "something" not in result.stderr.str()
assert "operation on closed file" not in result.stderr.str()

def test_logging_after_cap_stopped(self, testdir):
testdir.makeconftest(
"""\
import pytest
import logging

log = logging.getLogger(__name__)

@pytest.fixture
def log_on_teardown():
yield
log.warning('Logging on teardown')
"""
)
# make sure that logging is still captured in tests
p = testdir.makepyfile(
"""\
def test_hello(log_on_teardown):
import logging
logging.warning("hello433")
assert 1
raise KeyboardInterrupt()
"""
)
result = testdir.runpytest_subprocess(p, "--log-cli-level", "info")
assert result.ret != 0
result.stdout.fnmatch_lines(
["*WARNING*hello433*", "*WARNING*Logging on teardown*"]
)
assert (
"AttributeError: 'NoneType' object has no attribute 'resume_capturing'"
not in result.stderr.str()
)


class TestCaptureFixture(object):
@pytest.mark.parametrize("opt", [[], ["-s"]])
Expand Down Expand Up @@ -1300,13 +1334,13 @@ def test_capturing_and_logging_fundamentals(testdir, method):
Capture=capture.%s)
cap.start_capturing()

logging.warn("hello1")
logging.warning("hello1")
outerr = cap.readouterr()
print("suspend, captured %%s" %%(outerr,))
logging.warn("hello2")
logging.warning("hello2")

cap.pop_outerr_to_orig()
logging.warn("hello3")
logging.warning("hello3")

outerr = cap.readouterr()
print("suspend2, captured %%s" %% (outerr,))
Expand Down