From a4586150301bf6210ac648c3c1e93d8024cd4ba9 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Thu, 25 Oct 2018 11:16:26 +0200 Subject: [PATCH] Further simplify caplog.messages --- tests/unit/browser/test_pdfjs.py | 5 ++--- tests/unit/browser/test_qutescheme.py | 6 +++--- tests/unit/browser/test_signalfilter.py | 3 +-- tests/unit/config/test_config.py | 3 +-- tests/unit/keyinput/test_basekeyparser.py | 3 +-- tests/unit/misc/test_ipc.py | 4 +--- tests/unit/misc/test_sessions.py | 3 +-- tests/unit/test_app.py | 3 +-- tests/unit/utils/test_debug.py | 8 +++----- tests/unit/utils/test_error.py | 4 +--- tests/unit/utils/test_log.py | 6 ++---- tests/unit/utils/test_standarddir.py | 3 +-- tests/unit/utils/test_utils.py | 3 +-- tests/unit/utils/test_version.py | 6 ++---- 14 files changed, 21 insertions(+), 39 deletions(-) diff --git a/tests/unit/browser/test_pdfjs.py b/tests/unit/browser/test_pdfjs.py index 884ba159b..7fa1a8f6c 100644 --- a/tests/unit/browser/test_pdfjs.py +++ b/tests/unit/browser/test_pdfjs.py @@ -154,9 +154,8 @@ class TestResources: match="Path 'web/test' not found"): pdfjs.get_pdfjs_res_and_path('web/test') - assert len(caplog.records) == 1 - msg = caplog.messages[0] - assert msg == 'OSError while reading PDF.js file: Message' + expected = 'OSError while reading PDF.js file: Message' + assert caplog.messages == [expected] @pytest.mark.parametrize('path, expected', [ diff --git a/tests/unit/browser/test_qutescheme.py b/tests/unit/browser/test_qutescheme.py index d8353d7ae..0741f38db 100644 --- a/tests/unit/browser/test_qutescheme.py +++ b/tests/unit/browser/test_qutescheme.py @@ -203,9 +203,9 @@ class TestPDFJSHandler: with caplog.at_level(logging.WARNING, 'misc'): with pytest.raises(qutescheme.NotFoundError): qutescheme.data_for_url(QUrl('qute://pdfjs/no/file.html')) - assert len(caplog.records) == 1 - assert (caplog.messages[0] == - 'pdfjs resource requested but not found: /no/file.html') + + expected = 'pdfjs resource requested but not found: /no/file.html' + assert caplog.messages == [expected] def test_viewer_page(self, data_tmpdir): """Load the /web/viewer.html page.""" diff --git a/tests/unit/browser/test_signalfilter.py b/tests/unit/browser/test_signalfilter.py index e98207f15..8ea800e23 100644 --- a/tests/unit/browser/test_signalfilter.py +++ b/tests/unit/browser/test_signalfilter.py @@ -86,9 +86,8 @@ def test_logging(caplog, objects, tabbed_browser_stubs, index_of, verb): with caplog.at_level(logging.DEBUG, logger='signals'): objects.signaller.signal.emit('foo') - assert len(caplog.records) == 1 expected_msg = "{}: filtered_signal('foo') (tab {})".format(verb, index_of) - assert caplog.messages[0] == expected_msg + assert caplog.messages == [expected_msg] @pytest.mark.parametrize('index_of', [0, 1]) diff --git a/tests/unit/config/test_config.py b/tests/unit/config/test_config.py index 6438f8b98..672faf04a 100644 --- a/tests/unit/config/test_config.py +++ b/tests/unit/config/test_config.py @@ -388,9 +388,8 @@ class TestConfig: conf._set_value(opt, 'never') assert blocker.args == ['tabs.show'] - assert len(caplog.records) == 1 expected_message = 'Config option changed: tabs.show = never' - assert caplog.messages[0] == expected_message + assert caplog.messages == [expected_message] def test_set_value_no_backend(self, monkeypatch, conf): """Make sure setting values when the backend is still unknown works.""" diff --git a/tests/unit/keyinput/test_basekeyparser.py b/tests/unit/keyinput/test_basekeyparser.py index 25b470040..764c83e1f 100644 --- a/tests/unit/keyinput/test_basekeyparser.py +++ b/tests/unit/keyinput/test_basekeyparser.py @@ -60,8 +60,7 @@ class TestDebugLog: def test_log(self, keyparser, caplog): keyparser._debug_log('foo') - assert len(caplog.records) == 1 - assert caplog.messages[0] == 'foo' + assert caplog.messages == ['foo'] def test_no_log(self, keyparser, caplog): keyparser.do_log = False diff --git a/tests/unit/misc/test_ipc.py b/tests/unit/misc/test_ipc.py index ce4698c7d..93b6581b8 100644 --- a/tests/unit/misc/test_ipc.py +++ b/tests/unit/misc/test_ipc.py @@ -698,8 +698,6 @@ class TestSendOrListen: with pytest.raises(ipc.Error): ipc.send_or_listen(args) - assert len(caplog.records) == 1 - error_msgs = [ 'Handling fatal misc.ipc.{} with --no-err-windows!'.format( exc_name), @@ -709,7 +707,7 @@ class TestSendOrListen: 'post_text: Maybe another instance is running but frozen?', 'exception text: {}'.format(exc_msg), ] - assert caplog.messages[0] == '\n'.join(error_msgs) + assert caplog.messages == ['\n'.join(error_msgs)] @pytest.mark.posix(reason="Flaky on Windows") def test_error_while_listening(self, qlocalserver_mock, caplog, args): diff --git a/tests/unit/misc/test_sessions.py b/tests/unit/misc/test_sessions.py index 6c6ea669b..e2676b4e7 100644 --- a/tests/unit/misc/test_sessions.py +++ b/tests/unit/misc/test_sessions.py @@ -215,9 +215,8 @@ class TestSave: with caplog.at_level(logging.ERROR): sess_man.save(str(session_path), last_window=True) - assert len(caplog.records) == 1 msg = "last_window_session is None while saving!" - assert caplog.messages[0] == msg + assert caplog.messages == [msg] assert not session_path.exists() def test_last_window_session(self, sess_man, tmpdir): diff --git a/tests/unit/test_app.py b/tests/unit/test_app.py index a4f78a5ab..3d7555bc4 100644 --- a/tests/unit/test_app.py +++ b/tests/unit/test_app.py @@ -35,6 +35,5 @@ def test_on_focus_changed_issue1484(monkeypatch, qapp, caplog): buf = QBuffer() app.on_focus_changed(buf, buf) - assert len(caplog.records) == 1 expected = "on_focus_changed called with non-QWidget {!r}".format(buf) - assert caplog.messages[0] == expected + assert caplog.messages == [expected] diff --git a/tests/unit/utils/test_debug.py b/tests/unit/utils/test_debug.py index 2a78c5cba..bb93cbf10 100644 --- a/tests/unit/utils/test_debug.py +++ b/tests/unit/utils/test_debug.py @@ -41,8 +41,7 @@ def test_log_events(qapp, caplog): obj = EventObject() qapp.sendEvent(obj, QEvent(QEvent.User)) qapp.processEvents() - assert len(caplog.records) == 1 - assert caplog.messages[0] == 'Event in test_debug.EventObject: User' + assert caplog.messages == ['Event in test_debug.EventObject: User'] class SignalObject(QObject): @@ -74,9 +73,8 @@ def test_log_signals(caplog, signal_obj): signal_obj.signal1.emit() signal_obj.signal2.emit('foo', 'bar') - assert len(caplog.records) == 2 - assert caplog.messages[0] == 'Signal in : signal1()' - assert caplog.messages[1] == "Signal in : signal2('foo', 'bar')" + assert caplog.messages == ['Signal in : signal1()', + "Signal in : signal2('foo', 'bar')"] class TestLogTime: diff --git a/tests/unit/utils/test_error.py b/tests/unit/utils/test_error.py index 6b0679d35..42a090c75 100644 --- a/tests/unit/utils/test_error.py +++ b/tests/unit/utils/test_error.py @@ -51,8 +51,6 @@ def test_no_err_windows(caplog, exc, name, exc_text, fake_args): error.handle_fatal_exc(e, fake_args, 'title', pre_text='pre', post_text='post') - assert len(caplog.records) == 1 - expected = [ 'Handling fatal {} with --no-err-windows!'.format(name), '', @@ -61,7 +59,7 @@ def test_no_err_windows(caplog, exc, name, exc_text, fake_args): 'post_text: post', 'exception text: {}'.format(exc_text), ] - assert caplog.messages[0] == '\n'.join(expected) + assert caplog.messages == ['\n'.join(expected)] # This happens on Xvfb for some reason diff --git a/tests/unit/utils/test_log.py b/tests/unit/utils/test_log.py index cefeda62e..a20e335ca 100644 --- a/tests/unit/utils/test_log.py +++ b/tests/unit/utils/test_log.py @@ -270,8 +270,7 @@ class TestHideQtWarning: def test_stub(caplog, suffix, expected): with caplog.at_level(logging.WARNING, 'misc'): log.stub(suffix) - assert len(caplog.records) == 1 - assert caplog.messages[0] == expected + assert caplog.messages == [expected] def test_ignore_py_warnings(caplog): @@ -300,5 +299,4 @@ class TestQtMessageHandler: def test_empty_message(self, caplog): """Make sure there's no crash with an empty message.""" log.qt_message_handler(QtCore.QtDebugMsg, self.Context(), "") - assert len(caplog.records) == 1 - assert caplog.messages[0] == "Logged empty message!" + assert caplog.messages == ["Logged empty message!"] diff --git a/tests/unit/utils/test_standarddir.py b/tests/unit/utils/test_standarddir.py index 8edcf41b8..5962c8f22 100644 --- a/tests/unit/utils/test_standarddir.py +++ b/tests/unit/utils/test_standarddir.py @@ -288,8 +288,7 @@ class TestInitCacheDirTag: mocker.patch('builtins.open', side_effect=OSError) with caplog.at_level(logging.ERROR, 'init'): standarddir._init_cachedir_tag() - assert len(caplog.records) == 1 - assert caplog.messages[0] == 'Failed to create CACHEDIR.TAG' + assert caplog.messages == ['Failed to create CACHEDIR.TAG'] assert not tmpdir.listdir() diff --git a/tests/unit/utils/test_utils.py b/tests/unit/utils/test_utils.py index 5359dc675..e25ecfe4a 100644 --- a/tests/unit/utils/test_utils.py +++ b/tests/unit/utils/test_utils.py @@ -469,9 +469,8 @@ class TestPreventExceptions: ret = self.func_raising() # pylint: enable=assignment-from-no-return assert ret == 42 - assert len(caplog.records) == 1 expected = 'Error in test_utils.TestPreventExceptions.func_raising' - assert caplog.messages[0] == expected + assert caplog.messages == [expected] @utils.prevent_exceptions(42) def func_not_raising(self): diff --git a/tests/unit/utils/test_version.py b/tests/unit/utils/test_version.py index 60d8c4590..c21ea0624 100644 --- a/tests/unit/utils/test_version.py +++ b/tests/unit/utils/test_version.py @@ -294,8 +294,7 @@ class TestGitStr: commit_file_mock.return_value = '0deadcode' with caplog.at_level(logging.ERROR, 'misc'): assert version._git_str() == '0deadcode' - assert len(caplog.records) == 1 - assert caplog.messages[0] == "Error while getting git path" + assert caplog.messages == ["Error while getting git path"] def _has_git(): @@ -456,8 +455,7 @@ def test_release_info(files, expected, caplog, monkeypatch): with caplog.at_level(logging.ERROR, 'misc'): assert version._release_info() == expected if files is None: - assert len(caplog.records) == 1 - assert caplog.messages[0] == "Error while reading fake-file." + assert caplog.messages == ["Error while reading fake-file."] @pytest.mark.parametrize('equal', [True, False])