Add workaround for pytest-capturelog bug.

This should fix the tests on Windows.
See https://bitbucket.org/memedough/pytest-capturelog/issues/7/
This commit is contained in:
Florian Bruhin 2015-08-18 08:02:25 +02:00
parent 1e08a6a202
commit d8734a668c
2 changed files with 51 additions and 0 deletions

View File

@ -33,6 +33,13 @@ from qutebrowser.config import config
from qutebrowser.utils import objreg, usertypes
try:
import pytest_capturelog as capturelog_mod
except ImportError:
# When using pytest for pyflakes/pep8/..., the plugin won't be available
# but conftest.py will still be loaded.
capturelog_mod = None
@pytest.yield_fixture(scope='session', autouse=True)
def fail_on_logging():
@ -298,3 +305,25 @@ def tab_registry(win_registry):
objreg.register('tab-registry', registry, scope='window', window=0)
yield registry
objreg.delete('tab-registry', scope='window', window=0)
@pytest.yield_fixture(autouse=True)
def caplog_bug_workaround(request):
"""WORKAROUND for pytest-capturelog bug.
https://bitbucket.org/memedough/pytest-capturelog/issues/7/
This would lead to LogFailHandler failing after skipped tests as there are
multiple CaptureLogHandlers.
"""
yield
if capturelog_mod is None:
return
root_logger = logging.getLogger()
caplog_handlers = [h for h in root_logger.handlers
if isinstance(h, capturelog_mod.CaptureLogHandler)]
for h in caplog_handlers:
root_logger.removeHandler(h)
h.close()

View File

@ -23,6 +23,7 @@
import logging
import pytest
import pytest_capturelog # pylint: disable=import-error
def test_log_debug():
@ -63,3 +64,24 @@ def test_log_expected_wrong_logger(caplog):
with pytest.raises(pytest.fail.Exception):
with caplog.atLevel(logging.ERROR, logger):
logging.error('foo')
@pytest.fixture
def skipping_fixture():
pytest.skip("Skipping to test caplog workaround.")
def test_caplog_bug_workaround_1(caplog, skipping_fixture):
pass
def test_caplog_bug_workaround_2():
"""Make sure caplog_bug_workaround works correctly after a skipped test.
There should be only one capturelog handler.
"""
caplog_handler = None
for h in logging.getLogger().handlers:
if isinstance(h, pytest_capturelog.CaptureLogHandler):
assert caplog_handler is None
caplog_handler = h