diff --git a/tests/conftest.py b/tests/conftest.py index c4d6bb536..c24715c66 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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() diff --git a/tests/test_logfail.py b/tests/test_logfail.py index b869c8cae..1cd7b930d 100644 --- a/tests/test_logfail.py +++ b/tests/test_logfail.py @@ -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