From 517b8bc69e3c1c0c0e94d697900648fff1e440b0 Mon Sep 17 00:00:00 2001 From: Pedro Algarvio Date: Mon, 3 Dec 2018 11:52:33 +0000 Subject: [PATCH 1/4] Stop using the deprecated `\.warn()` log handler method --- testing/test_capture.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/testing/test_capture.py b/testing/test_capture.py index 47aba70d4..c113f0304 100644 --- a/testing/test_capture.py +++ b/testing/test_capture.py @@ -302,14 +302,14 @@ class TestLoggingInteraction(object): """\ 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 """ ) @@ -328,14 +328,14 @@ class TestLoggingInteraction(object): """\ 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 """ ) @@ -354,7 +354,7 @@ class TestLoggingInteraction(object): """\ import logging logging.basicConfig() - logging.warn("hello435") + logging.warning("hello435") """ ) # make sure that logging is still captured in tests @@ -375,7 +375,7 @@ class TestLoggingInteraction(object): """\ def test_hello(): import logging - logging.warn("hello433") + logging.warning("hello433") assert 0 """ ) @@ -1300,13 +1300,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,)) From 14024c7fc13a1bb60c7971e418caec919a5276ee Mon Sep 17 00:00:00 2001 From: Pedro Algarvio Date: Mon, 3 Dec 2018 12:12:26 +0000 Subject: [PATCH 2/4] Test case for #4500 bug and respective fix #4487 --- testing/test_capture.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/testing/test_capture.py b/testing/test_capture.py index c113f0304..2e1912f2e 100644 --- a/testing/test_capture.py +++ b/testing/test_capture.py @@ -385,6 +385,35 @@ class TestLoggingInteraction(object): 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"]]) From c1bdb07b2f2e905e6ec6258fb3638663bb2805e8 Mon Sep 17 00:00:00 2001 From: Pedro Algarvio Date: Mon, 3 Dec 2018 12:15:04 +0000 Subject: [PATCH 3/4] Fix change log entry --- changelog/4487.bugfix.rst | 1 - changelog/4500.bugfix.rst | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) delete mode 100644 changelog/4487.bugfix.rst create mode 100644 changelog/4500.bugfix.rst diff --git a/changelog/4487.bugfix.rst b/changelog/4487.bugfix.rst deleted file mode 100644 index 3ba8d4731..000000000 --- a/changelog/4487.bugfix.rst +++ /dev/null @@ -1 +0,0 @@ -During teardown of the python process, and on rare occasions, capture attributes can be ``None`` while trying to resume global capture. diff --git a/changelog/4500.bugfix.rst b/changelog/4500.bugfix.rst new file mode 100644 index 000000000..b84b6b117 --- /dev/null +++ b/changelog/4500.bugfix.rst @@ -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``. From 6da8befc74b9671123e861bcb343179fd5aa5edf Mon Sep 17 00:00:00 2001 From: Pedro Algarvio Date: Mon, 3 Dec 2018 12:58:22 +0000 Subject: [PATCH 4/4] Black suggestions --- testing/test_capture.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/testing/test_capture.py b/testing/test_capture.py index 2e1912f2e..d44b58ee0 100644 --- a/testing/test_capture.py +++ b/testing/test_capture.py @@ -411,8 +411,13 @@ class TestLoggingInteraction(object): ) 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() + 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):