Skip to content

clear root log handlers between tests #2159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ Piotr Banaszkiewicz
Punyashloka Biswal
Quentin Pradet
Ralf Schmitt
Raphael Deem
Raphael Pierzina
Raquel Alegre
Roberto Polli
Expand Down
5 changes: 5 additions & 0 deletions _pytest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import _pytest._code
import py
import pytest
import logging

try:
from collections import MutableMapping as MappingMixin
except ImportError:
Expand Down Expand Up @@ -86,6 +88,7 @@ def pytest_configure(config):

def wrap_session(config, doit):
"""Skeleton command line program"""

session = Session(config)
session.exitstatus = EXIT_OK
initstate = 0
Expand Down Expand Up @@ -141,6 +144,8 @@ def pytest_collection(session):
return session.perform_collect()

def pytest_runtestloop(session):
for handler in logging.root.handlers[:]:
logging.root.removeHandler(handler)
if (session.testsfailed and
not session.config.option.continue_on_collection_errors):
raise session.Interrupted(
Expand Down
12 changes: 12 additions & 0 deletions testing/test_log1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from io import StringIO
import logging

# This tests that basicConfig settings from test_log2.py don't
# interfere with those from this test
def test_log():
log_stream = StringIO()
logging.basicConfig(stream=log_stream, level=logging.INFO)
log = logging.getLogger()

log.warn('test')
assert log_stream.getvalue() == 'WARNING:root:test\n'
10 changes: 10 additions & 0 deletions testing/test_log2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import logging

# This tests that basicConfig settings from test_log1.py don't
# interfere with those from this test
logging.basicConfig()
log = logging.getLogger()
def test_foo():
foo = 1
log.warn(foo)
assert foo == 1