From 65f21fc8eedf3061dee14940ab189a9f42844804 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Sun, 1 Mar 2015 22:10:16 +0100 Subject: [PATCH] Use unittest.mock.patch for tests. Closes #76. --- qutebrowser/test/browser/test_webelem.py | 36 +++++++------- qutebrowser/test/config/test_configtypes.py | 12 ++--- .../test/keyinput/test_basekeyparser.py | 3 +- qutebrowser/test/misc/test_editor.py | 34 ++++++------- qutebrowser/test/misc/test_readline.py | 49 ++++++++++++------- qutebrowser/test/utils/test_urlutils.py | 37 +++++--------- 6 files changed, 84 insertions(+), 87 deletions(-) diff --git a/qutebrowser/test/browser/test_webelem.py b/qutebrowser/test/browser/test_webelem.py index d898a1d15..f3c0306a3 100644 --- a/qutebrowser/test/browser/test_webelem.py +++ b/qutebrowser/test/browser/test_webelem.py @@ -22,7 +22,7 @@ """Tests for the webelement utils.""" import unittest -import unittest.mock +from unittest import mock import collections.abc from PyQt5.QtCore import QRect, QPoint @@ -46,7 +46,7 @@ def get_webelem(geometry=None, frame=None, null=False, visibility='', tagname: The tag name. classes: HTML classes to be added. """ - elem = unittest.mock.Mock() + elem = mock.Mock() elem.isNull.return_value = null elem.geometry.return_value = geometry elem.webFrame.return_value = frame @@ -458,31 +458,31 @@ class IsEditableTests(unittest.TestCase): elem = get_webelem(tagname='textarea', attributes={'readonly': None}) self.assertFalse(elem.is_editable()) + @mock.patch('qutebrowser.browser.webelem.config', new=stubs.ConfigStub( + {'input': {'insert-mode-on-plugins': True}})) def test_embed_true(self): """Test embed-element with insert-mode-on-plugins true.""" - webelem.config = stubs.ConfigStub({'input': - {'insert-mode-on-plugins': True}}) elem = get_webelem(tagname='embed') self.assertTrue(elem.is_editable()) + @mock.patch('qutebrowser.browser.webelem.config', new=stubs.ConfigStub( + {'input': {'insert-mode-on-plugins': True}})) def test_applet_true(self): """Test applet-element with insert-mode-on-plugins true.""" - webelem.config = stubs.ConfigStub({'input': - {'insert-mode-on-plugins': True}}) elem = get_webelem(tagname='applet') self.assertTrue(elem.is_editable()) + @mock.patch('qutebrowser.browser.webelem.config', new=stubs.ConfigStub( + {'input': {'insert-mode-on-plugins': False}})) def test_embed_false(self): """Test embed-element with insert-mode-on-plugins false.""" - webelem.config = stubs.ConfigStub({'input': - {'insert-mode-on-plugins': False}}) elem = get_webelem(tagname='embed') self.assertFalse(elem.is_editable()) + @mock.patch('qutebrowser.browser.webelem.config', new=stubs.ConfigStub( + {'input': {'insert-mode-on-plugins': False}})) def test_applet_false(self): """Test applet-element with insert-mode-on-plugins false.""" - webelem.config = stubs.ConfigStub({'input': - {'insert-mode-on-plugins': False}}) elem = get_webelem(tagname='applet') self.assertFalse(elem.is_editable()) @@ -496,34 +496,34 @@ class IsEditableTests(unittest.TestCase): elem = get_webelem(tagname='object', attributes={'type': 'image/gif'}) self.assertFalse(elem.is_editable()) + @mock.patch('qutebrowser.browser.webelem.config', new=stubs.ConfigStub( + {'input': {'insert-mode-on-plugins': True}})) def test_object_application(self): """Test object-element with application type.""" - webelem.config = stubs.ConfigStub({'input': - {'insert-mode-on-plugins': True}}) elem = get_webelem(tagname='object', attributes={'type': 'application/foo'}) self.assertTrue(elem.is_editable()) + @mock.patch('qutebrowser.browser.webelem.config', new=stubs.ConfigStub( + {'input': {'insert-mode-on-plugins': False}})) def test_object_application_false(self): """Test object-element with application type but not ...-on-plugins.""" - webelem.config = stubs.ConfigStub({'input': - {'insert-mode-on-plugins': False}}) elem = get_webelem(tagname='object', attributes={'type': 'application/foo'}) self.assertFalse(elem.is_editable()) + @mock.patch('qutebrowser.browser.webelem.config', new=stubs.ConfigStub( + {'input': {'insert-mode-on-plugins': True}})) def test_object_classid(self): """Test object-element with classid.""" - webelem.config = stubs.ConfigStub({'input': - {'insert-mode-on-plugins': True}}) elem = get_webelem(tagname='object', attributes={'type': 'foo', 'classid': 'foo'}) self.assertTrue(elem.is_editable()) + @mock.patch('qutebrowser.browser.webelem.config', new=stubs.ConfigStub( + {'input': {'insert-mode-on-plugins': False}})) def test_object_classid_false(self): """Test object-element with classid but not insert-mode-on-plugins.""" - webelem.config = stubs.ConfigStub({'input': - {'insert-mode-on-plugins': False}}) elem = get_webelem(tagname='object', attributes={'type': 'foo', 'classid': 'foo'}) self.assertFalse(elem.is_editable()) diff --git a/qutebrowser/test/config/test_configtypes.py b/qutebrowser/test/config/test_configtypes.py index 3ff7de7bc..3ed94d991 100644 --- a/qutebrowser/test/config/test_configtypes.py +++ b/qutebrowser/test/config/test_configtypes.py @@ -872,22 +872,16 @@ class PercOrIntTests(unittest.TestCase): self.assertEqual(self.t.transform('1337'), '1337') +@mock.patch('qutebrowser.config.configtypes.cmdutils', new=stubs.FakeCmdUtils( + {'cmd1': stubs.FakeCommand("desc 1"), + 'cmd2': stubs.FakeCommand("desc 2")})) class CommandTests(unittest.TestCase): """Test Command.""" def setUp(self): - self.old_cmdutils = configtypes.cmdutils - commands = { - 'cmd1': stubs.FakeCommand("desc 1"), - 'cmd2': stubs.FakeCommand("desc 2"), - } - configtypes.cmdutils = stubs.FakeCmdUtils(commands) self.t = configtypes.Command() - def tearDown(self): - configtypes.cmdutils = self.old_cmdutils - def test_validate_empty(self): """Test validate with an empty string.""" with self.assertRaises(configexc.ValidationError): diff --git a/qutebrowser/test/keyinput/test_basekeyparser.py b/qutebrowser/test/keyinput/test_basekeyparser.py index 74c4bd75a..971e4e181 100644 --- a/qutebrowser/test/keyinput/test_basekeyparser.py +++ b/qutebrowser/test/keyinput/test_basekeyparser.py @@ -208,9 +208,10 @@ class KeyChainTests(unittest.TestCase): self.kp.execute.assert_called_once_with('ba', self.kp.Type.chain, None) self.assertEqual(self.kp._keystring, '') + @mock.patch('qutebrowser.keyinput.basekeyparser.config', + new=stubs.ConfigStub(CONFIG)) def test_ambigious_keychain(self): """Test ambigious keychain.""" - basekeyparser.config = stubs.ConfigStub(CONFIG) timer = self.kp._ambigious_timer self.assertFalse(timer.isActive()) # We start with 'a' where the keychain gives us an ambigious result. diff --git a/qutebrowser/test/misc/test_editor.py b/qutebrowser/test/misc/test_editor.py index 80603b0f3..eeb6a3ef9 100644 --- a/qutebrowser/test/misc/test_editor.py +++ b/qutebrowser/test/misc/test_editor.py @@ -45,36 +45,36 @@ class ArgTests(unittest.TestCase): def setUp(self): self.editor = editor.ExternalEditor(0) + @mock.patch('qutebrowser.misc.editor.config', new=stubs.ConfigStub( + {'general': {'editor': ['bin'], 'editor-encoding': 'utf-8'}})) def test_simple_start_args(self, _proc_mock): """Test starting editor without arguments.""" - editor.config = stubs.ConfigStub( - {'general': {'editor': ['bin'], 'editor-encoding': 'utf-8'}}) self.editor.edit("") self.editor._proc.start.assert_called_with("bin", []) + @mock.patch('qutebrowser.misc.editor.config', new=stubs.ConfigStub( + {'general': {'editor': ['bin', 'foo', 'bar'], + 'editor-encoding': 'utf-8'}})) def test_start_args(self, _proc_mock): """Test starting editor with static arguments.""" - editor.config = stubs.ConfigStub( - {'general': {'editor': ['bin', 'foo', 'bar'], - 'editor-encoding': 'utf-8'}}) self.editor.edit("") self.editor._proc.start.assert_called_with("bin", ["foo", "bar"]) + @mock.patch('qutebrowser.misc.editor.config', new=stubs.ConfigStub( + {'general': {'editor': ['bin', 'foo', '{}', 'bar'], + 'editor-encoding': 'utf-8'}})) def test_placeholder(self, _proc_mock): """Test starting editor with placeholder argument.""" - editor.config = stubs.ConfigStub( - {'general': {'editor': ['bin', 'foo', '{}', 'bar'], - 'editor-encoding': 'utf-8'}}) self.editor.edit("") filename = self.editor._filename self.editor._proc.start.assert_called_with( "bin", ["foo", filename, "bar"]) + @mock.patch('qutebrowser.misc.editor.config', new=stubs.ConfigStub( + {'general': {'editor': ['bin', 'foo{}bar'], + 'editor-encoding': 'utf-8'}})) def test_in_arg_placeholder(self, _proc_mock): """Test starting editor with placeholder argument inside argument.""" - editor.config = stubs.ConfigStub( - {'general': {'editor': ['bin', 'foo{}bar'], - 'editor-encoding': 'utf-8'}}) self.editor.edit("") self.editor._proc.start.assert_called_with("bin", ["foo{}bar"]) @@ -84,6 +84,8 @@ class ArgTests(unittest.TestCase): @mock.patch('qutebrowser.misc.editor.QProcess', new_callable=stubs.FakeQProcess) +@mock.patch('qutebrowser.misc.editor.config', new=stubs.ConfigStub( + {'general': {'editor': [''], 'editor-encoding': 'utf-8'}})) class FileHandlingTests(unittest.TestCase): """Test creation/deletion of tempfile. @@ -94,8 +96,6 @@ class FileHandlingTests(unittest.TestCase): def setUp(self): self.editor = editor.ExternalEditor(0) - editor.config = stubs.ConfigStub( - {'general': {'editor': [''], 'editor-encoding': 'utf-8'}}) def test_file_handling_closed_ok(self, _proc_mock): """Test file handling when closing with an exitstatus == 0.""" @@ -125,6 +125,8 @@ class FileHandlingTests(unittest.TestCase): @mock.patch('qutebrowser.misc.editor.QProcess', new_callable=stubs.FakeQProcess) +@mock.patch('qutebrowser.misc.editor.config', new=stubs.ConfigStub( + {'general': {'editor': [''], 'editor-encoding': 'utf-8'}})) class TextModifyTests(unittest.TestCase): """Tests to test if the text gets saved/loaded correctly. @@ -136,8 +138,6 @@ class TextModifyTests(unittest.TestCase): def setUp(self): self.editor = editor.ExternalEditor(0) self.editor.editing_finished = mock.Mock() - editor.config = stubs.ConfigStub( - {'general': {'editor': [''], 'editor-encoding': 'utf-8'}}) def _write(self, text): """Write a text to the file opened in the fake editor. @@ -196,6 +196,8 @@ class TextModifyTests(unittest.TestCase): @mock.patch('qutebrowser.misc.editor.QProcess', new_callable=stubs.FakeQProcess) @mock.patch('qutebrowser.misc.editor.message', autospec=True) +@mock.patch('qutebrowser.misc.editor.config', new=stubs.ConfigStub( + {'general': {'editor': [''], 'editor-encoding': 'utf-8'}})) class ErrorMessageTests(unittest.TestCase): """Test if statusbar error messages get emitted correctly. @@ -208,8 +210,6 @@ class ErrorMessageTests(unittest.TestCase): def setUp(self): self.editor = editor.ExternalEditor(0) - editor.config = stubs.ConfigStub( - {'general': {'editor': [''], 'editor-encoding': 'utf-8'}}) def test_proc_error(self, msg_mock, _proc_mock): """Test on_proc_error.""" diff --git a/qutebrowser/test/misc/test_readline.py b/qutebrowser/test/misc/test_readline.py index 0f869c689..d64549a7a 100644 --- a/qutebrowser/test/misc/test_readline.py +++ b/qutebrowser/test/misc/test_readline.py @@ -31,23 +31,26 @@ from qutebrowser.misc import readline from qutebrowser.test import stubs +@mock.patch('qutebrowser.misc.readline.QApplication', + new_callable=stubs.FakeQApplication) class NoneWidgetTests(unittest.TestCase): """Tests when the focused widget is None.""" def setUp(self): - readline.QApplication = stubs.FakeQApplication() - readline.QApplication.focusWidget = mock.Mock(return_value=None) self.bridge = readline.ReadlineBridge() - def test_none(self): + def test_none(self, qapp): """Test if there are no exceptions when the widget is None.""" + qapp.focusWidget = mock.Mock(return_value=None) for name, method in inspect.getmembers(self.bridge, inspect.ismethod): with self.subTest(name=name): if name.startswith('rl_'): method() +@mock.patch('qutebrowser.misc.readline.QApplication', + new_callable=stubs.FakeQApplication) class ReadlineBridgeTest(unittest.TestCase): """Tests for readline bridge.""" @@ -55,55 +58,63 @@ class ReadlineBridgeTest(unittest.TestCase): def setUp(self): self.qle = mock.Mock() self.qle.__class__ = QLineEdit - readline.QApplication.focusWidget = mock.Mock(return_value=self.qle) self.bridge = readline.ReadlineBridge() def _set_selected_text(self, text): """Set the value the fake QLineEdit should return for selectedText.""" self.qle.configure_mock(**{'selectedText.return_value': text}) - def test_rl_backward_char(self): + def test_rl_backward_char(self, qapp): """Test rl_backward_char.""" + qapp.focusWidget = mock.Mock(return_value=self.qle) self.bridge.rl_backward_char() self.qle.cursorBackward.assert_called_with(False) - def test_rl_forward_char(self): + def test_rl_forward_char(self, qapp): """Test rl_forward_char.""" + qapp.focusWidget = mock.Mock(return_value=self.qle) self.bridge.rl_forward_char() self.qle.cursorForward.assert_called_with(False) - def test_rl_backward_word(self): + def test_rl_backward_word(self, qapp): """Test rl_backward_word.""" + qapp.focusWidget = mock.Mock(return_value=self.qle) self.bridge.rl_backward_word() self.qle.cursorWordBackward.assert_called_with(False) - def test_rl_forward_word(self): + def test_rl_forward_word(self, qapp): """Test rl_forward_word.""" + qapp.focusWidget = mock.Mock(return_value=self.qle) self.bridge.rl_forward_word() self.qle.cursorWordForward.assert_called_with(False) - def test_rl_beginning_of_line(self): + def test_rl_beginning_of_line(self, qapp): """Test rl_beginning_of_line.""" + qapp.focusWidget = mock.Mock(return_value=self.qle) self.bridge.rl_beginning_of_line() self.qle.home.assert_called_with(False) - def test_rl_end_of_line(self): + def test_rl_end_of_line(self, qapp): """Test rl_end_of_line.""" + qapp.focusWidget = mock.Mock(return_value=self.qle) self.bridge.rl_end_of_line() self.qle.end.assert_called_with(False) - def test_rl_delete_char(self): + def test_rl_delete_char(self, qapp): """Test rl_delete_char.""" + qapp.focusWidget = mock.Mock(return_value=self.qle) self.bridge.rl_delete_char() self.qle.del_.assert_called_with() - def test_rl_backward_delete_char(self): + def test_rl_backward_delete_char(self, qapp): """Test rl_backward_delete_char.""" + qapp.focusWidget = mock.Mock(return_value=self.qle) self.bridge.rl_backward_delete_char() self.qle.backspace.assert_called_with() - def test_rl_unix_line_discard(self): + def test_rl_unix_line_discard(self, qapp): """Set a selected text, delete it, see if it comes back with yank.""" + qapp.focusWidget = mock.Mock(return_value=self.qle) self._set_selected_text("delete test") self.bridge.rl_unix_line_discard() self.qle.home.assert_called_with(True) @@ -112,8 +123,9 @@ class ReadlineBridgeTest(unittest.TestCase): self.bridge.rl_yank() self.qle.insert.assert_called_with("delete test") - def test_rl_kill_line(self): + def test_rl_kill_line(self, qapp): """Set a selected text, delete it, see if it comes back with yank.""" + qapp.focusWidget = mock.Mock(return_value=self.qle) self._set_selected_text("delete test") self.bridge.rl_kill_line() self.qle.end.assert_called_with(True) @@ -122,8 +134,9 @@ class ReadlineBridgeTest(unittest.TestCase): self.bridge.rl_yank() self.qle.insert.assert_called_with("delete test") - def test_rl_unix_word_rubout(self): + def test_rl_unix_word_rubout(self, qapp): """Set a selected text, delete it, see if it comes back with yank.""" + qapp.focusWidget = mock.Mock(return_value=self.qle) self._set_selected_text("delete test") self.bridge.rl_unix_word_rubout() self.qle.cursorWordBackward.assert_called_with(True) @@ -132,8 +145,9 @@ class ReadlineBridgeTest(unittest.TestCase): self.bridge.rl_yank() self.qle.insert.assert_called_with("delete test") - def test_rl_kill_word(self): + def test_rl_kill_word(self, qapp): """Set a selected text, delete it, see if it comes back with yank.""" + qapp.focusWidget = mock.Mock(return_value=self.qle) self._set_selected_text("delete test") self.bridge.rl_kill_word() self.qle.cursorWordForward.assert_called_with(True) @@ -142,8 +156,9 @@ class ReadlineBridgeTest(unittest.TestCase): self.bridge.rl_yank() self.qle.insert.assert_called_with("delete test") - def test_rl_yank_no_text(self): + def test_rl_yank_no_text(self, qapp): """Test yank without having deleted anything.""" + qapp.focusWidget = mock.Mock(return_value=self.qle) self.bridge.rl_yank() self.assertFalse(self.qle.insert.called) diff --git a/qutebrowser/test/utils/test_urlutils.py b/qutebrowser/test/utils/test_urlutils.py index 3d99f08ad..a1e1f9e9d 100644 --- a/qutebrowser/test/utils/test_urlutils.py +++ b/qutebrowser/test/utils/test_urlutils.py @@ -22,6 +22,7 @@ """Tests for qutebrowser.utils.urlutils.""" import unittest +from unittest import mock from PyQt5.QtCore import QUrl @@ -79,17 +80,11 @@ class SpecialURLTests(unittest.TestCase): self.assertFalse(urlutils.is_special_url(u)) +@mock.patch('qutebrowser.utils.urlutils.config', new=stubs.ConfigStub( + get_config_stub())) class SearchUrlTests(unittest.TestCase): - """Test _get_search_url. - - Attributes: - config: The urlutils.config instance. - """ - - def setUp(self): - self.config = urlutils.config - urlutils.config = stubs.ConfigStub(get_config_stub()) + """Test _get_search_url.""" def test_default_engine(self): """Test default search engine.""" @@ -127,9 +122,6 @@ class SearchUrlTests(unittest.TestCase): self.assertEqual(url.host(), 'www.example.com') self.assertEqual(url.query(), 'q=blub testfoo') - def tearDown(self): - urlutils.config = self.config - class IsUrlTests(unittest.TestCase): @@ -138,9 +130,6 @@ class IsUrlTests(unittest.TestCase): Class attributes: URLS: A list of strings which are URLs. NOT_URLS: A list of strings which aren't URLs. - - Attributes: - config: The urlutils.config instance. """ URLS = ( @@ -168,36 +157,34 @@ class IsUrlTests(unittest.TestCase): 'http:foo:0', ) - def setUp(self): - self.config = urlutils.config - + @mock.patch('qutebrowser.utils.urlutils.config', new=stubs.ConfigStub( + get_config_stub('naive'))) def test_urls(self): """Test things which are URLs.""" - urlutils.config = stubs.ConfigStub(get_config_stub('naive')) for url in self.URLS: with self.subTest(url=url): self.assertTrue(urlutils.is_url(url), url) + @mock.patch('qutebrowser.utils.urlutils.config', new=stubs.ConfigStub( + get_config_stub('naive'))) def test_not_urls(self): """Test things which are not URLs.""" - urlutils.config = stubs.ConfigStub(get_config_stub('naive')) for url in self.NOT_URLS: with self.subTest(url=url): self.assertFalse(urlutils.is_url(url), url) + @mock.patch('qutebrowser.utils.urlutils.config', new=stubs.ConfigStub( + get_config_stub(True))) def test_search_autosearch(self): """Test explicit search with auto-search=True""" - urlutils.config = stubs.ConfigStub(get_config_stub(True)) self.assertFalse(urlutils.is_url('test foo')) + @mock.patch('qutebrowser.utils.urlutils.config', new=stubs.ConfigStub( + get_config_stub(False))) def test_search_no_autosearch(self): """Test explicit search with auto-search=False""" - urlutils.config = stubs.ConfigStub(get_config_stub(False)) self.assertFalse(urlutils.is_url('test foo')) - def tearDown(self): - urlutils.config = self.config - class QurlFromUserInputTests(unittest.TestCase):