Skip to content
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

Fix set_level side effects #61

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
9 changes: 5 additions & 4 deletions pytest_catchlog/fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
class LogCaptureFixture(object):
"""Provides access and control of log capturing."""

def __init__(self, item):
def __init__(self, item, monkeypatch):
"""Creates a new funcarg."""
self._item = item
self._monkeypatch = monkeypatch

@property
def handler(self):
Expand Down Expand Up @@ -55,7 +56,7 @@ def set_level(self, level, logger=None):
"""

obj = logger and logging.getLogger(logger) or self.handler
obj.setLevel(level)
self._monkeypatch.setattr(obj, 'level', logging._checkLevel(level))

def at_level(self, level, logger=None):
"""Context manager that sets the level for capturing of logs.
Expand Down Expand Up @@ -140,7 +141,7 @@ def atLevel(self, level, logger=None):


@pytest.fixture
def caplog(request):
def caplog(request, monkeypatch):
"""Access and control log capturing.

Captured logs are available through the following methods::
Expand All @@ -149,6 +150,6 @@ def caplog(request):
* caplog.records() -> list of logging.LogRecord instances
* caplog.record_tuples() -> list of (logger_name, level, message) tuples
"""
return CompatLogCaptureFixture(request.node)
return CompatLogCaptureFixture(request.node, monkeypatch)

capturelog = caplog
6 changes: 5 additions & 1 deletion tests/test_fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ def test_fixture_help(testdir):
result.stdout.fnmatch_lines(['*caplog*'])


def test_change_level(caplog):
def test_change_level(caplog, monkeypatch):
caplog.set_level(logging.INFO)
logger.debug('handler DEBUG level')
logger.info('handler INFO level')

assert sublogger.level == logging.NOTSET
caplog.set_level(logging.CRITICAL, logger=sublogger.name)
sublogger.warning('logger WARNING level')
sublogger.critical('logger CRITICAL level')
Expand All @@ -27,6 +28,9 @@ def test_change_level(caplog):
assert 'INFO' in caplog.text
assert 'WARNING' not in caplog.text
assert 'CRITICAL' in caplog.text
assert sublogger.level == logging.CRITICAL
monkeypatch.undo()
assert sublogger.level == logging.NOTSET


def test_with_statement(caplog):
Expand Down