Use monkeypatch instead of mocker in some tests.

See #660.
This commit is contained in:
Florian Bruhin 2015-05-18 23:32:01 +02:00
parent beb970d7d5
commit 98d1fca220
7 changed files with 50 additions and 47 deletions

View File

@ -378,10 +378,10 @@ class TestIsEditable:
webelem.config = old_config
@pytest.fixture
def stubbed_config(self, config_stub, mocker):
def stubbed_config(self, config_stub, monkeypatch):
"""Fixture to create a config stub with an input section."""
config_stub.data = {'input': {}}
mocker.patch('qutebrowser.browser.webelem.config', new=config_stub)
monkeypatch.setattr('qutebrowser.browser.webelem.config', config_stub)
return config_stub
def test_input_plain(self):

View File

@ -883,11 +883,12 @@ class TestCommand:
"""Test Command."""
@pytest.fixture(autouse=True)
def setup(self, mocker, stubs):
def setup(self, monkeypatch, stubs):
self.t = configtypes.Command()
cmd_utils = stubs.FakeCmdUtils({'cmd1': stubs.FakeCommand("desc 1"),
'cmd2': stubs.FakeCommand("desc 2")})
mocker.patch('qutebrowser.config.configtypes.cmdutils', new=cmd_utils)
monkeypatch.setattr('qutebrowser.config.configtypes.cmdutils',
cmd_utils)
def test_validate_empty(self):
"""Test validate with an empty string."""

View File

@ -55,10 +55,10 @@ def fake_keyconfig():
@pytest.fixture
def mock_timer(mocker, stubs):
def mock_timer(monkeypatch, stubs):
"""Mock the Timer class used by the usertypes module with a stub."""
mocker.patch('qutebrowser.keyinput.basekeyparser.usertypes.Timer',
new=stubs.FakeTimer)
monkeypatch.setattr('qutebrowser.keyinput.basekeyparser.usertypes.Timer',
stubs.FakeTimer)
class TestSplitCount:
@ -206,11 +206,11 @@ class TestKeyChain:
assert self.kp._keystring == ''
def test_ambiguous_keychain(self, fake_keyevent_factory, config_stub,
mocker):
monkeypatch):
"""Test ambiguous keychain."""
config_stub.data = CONFIG
mocker.patch('qutebrowser.keyinput.basekeyparser.config',
new=config_stub)
monkeypatch.setattr('qutebrowser.keyinput.basekeyparser.config',
config_stub)
timer = self.kp._ambiguous_timer
assert not timer.isActive()
# We start with 'a' where the keychain gives us an ambiguous result.

View File

@ -49,13 +49,14 @@ class TestsNormalKeyParser:
# pylint: disable=protected-access
@pytest.yield_fixture(autouse=True)
def setup(self, mocker, stubs, config_stub):
def setup(self, monkeypatch, stubs, config_stub):
"""Set up mocks and read the test config."""
mocker.patch('qutebrowser.keyinput.basekeyparser.usertypes.Timer',
new=stubs.FakeTimer)
monkeypatch.setattr(
'qutebrowser.keyinput.basekeyparser.usertypes.Timer',
stubs.FakeTimer)
config_stub.data = CONFIG
mocker.patch('qutebrowser.keyinput.modeparsers.config',
new=config_stub)
monkeypatch.setattr('qutebrowser.keyinput.modeparsers.config',
config_stub)
objreg.register('key-config', fake_keyconfig)
self.kp = modeparsers.NormalKeyParser(0)

View File

@ -41,18 +41,18 @@ class TestArg:
"""
@pytest.yield_fixture(autouse=True)
def setup(self, mocker, stubs):
mocker.patch('qutebrowser.misc.editor.QProcess',
new_callable=stubs.FakeQProcess)
def setup(self, monkeypatch, stubs):
monkeypatch.setattr('qutebrowser.misc.editor.QProcess',
stubs.FakeQProcess())
self.editor = editor.ExternalEditor(0)
yield
self.editor._cleanup() # pylint: disable=protected-access
@pytest.fixture
def stubbed_config(self, config_stub, mocker):
def stubbed_config(self, config_stub, monkeypatch):
"""Fixture to create a config stub with an input section."""
config_stub.data = {'input': {}}
mocker.patch('qutebrowser.misc.editor.config', new=config_stub)
monkeypatch.setattr('qutebrowser.misc.editor.config', config_stub)
return config_stub
def test_simple_start_args(self, stubbed_config):
@ -98,14 +98,14 @@ class TestFileHandling:
"""
@pytest.fixture(autouse=True)
def setup(self, mocker, stubs, config_stub):
mocker.patch('qutebrowser.misc.editor.message',
new=stubs.MessageModule())
mocker.patch('qutebrowser.misc.editor.QProcess',
new_callable=stubs.FakeQProcess)
def setup(self, monkeypatch, stubs, config_stub):
monkeypatch.setattr('qutebrowser.misc.editor.message',
stubs.MessageModule())
monkeypatch.setattr('qutebrowser.misc.editor.QProcess',
stubs.FakeQProcess())
config_stub.data = {'general': {'editor': [''],
'editor-encoding': 'utf-8'}}
mocker.patch('qutebrowser.misc.editor.config', config_stub)
monkeypatch.setattr('qutebrowser.misc.editor.config', config_stub)
self.editor = editor.ExternalEditor(0)
def test_file_handling_closed_ok(self):
@ -147,12 +147,12 @@ class TestModifyTests:
"""
@pytest.fixture(autouse=True)
def setup(self, mocker, stubs, config_stub):
mocker.patch('qutebrowser.misc.editor.QProcess',
new_callable=stubs.FakeQProcess)
def setup(self, monkeypatch, stubs, config_stub):
monkeypatch.setattr('qutebrowser.misc.editor.QProcess',
stubs.FakeQProcess())
config_stub.data = {'general': {'editor': [''],
'editor-encoding': 'utf-8'}}
mocker.patch('qutebrowser.misc.editor.config', new=config_stub)
monkeypatch.setattr('qutebrowser.misc.editor.config', config_stub)
self.editor = editor.ExternalEditor(0)
self.editor.editing_finished = mock.Mock()
@ -219,14 +219,14 @@ class TestErrorMessage:
"""
@pytest.yield_fixture(autouse=True)
def setup(self, mocker, stubs, config_stub):
mocker.patch('qutebrowser.misc.editor.QProcess',
new_callable=stubs.FakeQProcess)
mocker.patch('qutebrowser.misc.editor.message',
new=stubs.MessageModule())
def setup(self, monkeypatch, stubs, config_stub):
monkeypatch.setattr('qutebrowser.misc.editor.QProcess',
stubs.FakeQProcess())
monkeypatch.setattr('qutebrowser.misc.editor.message',
stubs.MessageModule())
config_stub.data = {'general': {'editor': [''],
'editor-encoding': 'utf-8'}}
mocker.patch('qutebrowser.misc.editor.config', new=config_stub)
monkeypatch.setattr('qutebrowser.misc.editor.config', config_stub)
self.editor = editor.ExternalEditor(0)
yield
self.editor._cleanup() # pylint: disable=protected-access

View File

@ -31,10 +31,11 @@ from qutebrowser.misc import readline
@pytest.fixture
def mocked_qapp(mocker, stubs):
def mocked_qapp(monkeypatch, stubs):
"""Fixture that mocks readline.QApplication and returns it."""
return mocker.patch('qutebrowser.misc.readline.QApplication',
new_callable=stubs.FakeQApplication)
stub = stubs.FakeQApplication()
monkeypatch.setattr('qutebrowser.misc.readline.QApplication', stub)
return stub
class TestNoneWidget:

View File

@ -81,10 +81,10 @@ class TestSearchUrl:
"""Test _get_search_url."""
@pytest.fixture(autouse=True)
def mock_config(self, config_stub, mocker):
def mock_config(self, config_stub, monkeypatch):
"""Fixture to patch urlutils.config with a stub."""
init_config_stub(config_stub)
mocker.patch('qutebrowser.utils.urlutils.config', config_stub)
monkeypatch.setattr('qutebrowser.utils.urlutils.config', config_stub)
def test_default_engine(self):
"""Test default search engine."""
@ -159,24 +159,24 @@ class TestIsUrl:
)
@pytest.mark.parametrize('url', URLS)
def test_urls(self, mocker, config_stub, url):
def test_urls(self, monkeypatch, config_stub, url):
"""Test things which are URLs."""
init_config_stub(config_stub, 'naive')
mocker.patch('qutebrowser.utils.urlutils.config', config_stub)
monkeypatch.setattr('qutebrowser.utils.urlutils.config', config_stub)
assert urlutils.is_url(url), url
@pytest.mark.parametrize('url', NOT_URLS)
def test_not_urls(self, mocker, config_stub, url):
def test_not_urls(self, monkeypatch, config_stub, url):
"""Test things which are not URLs."""
init_config_stub(config_stub, 'naive')
mocker.patch('qutebrowser.utils.urlutils.config', config_stub)
monkeypatch.setattr('qutebrowser.utils.urlutils.config', config_stub)
assert not urlutils.is_url(url), url
@pytest.mark.parametrize('autosearch', [True, False])
def test_search_autosearch(self, mocker, config_stub, autosearch):
def test_search_autosearch(self, monkeypatch, config_stub, autosearch):
"""Test explicit search with auto-search=True."""
init_config_stub(config_stub, autosearch)
mocker.patch('qutebrowser.utils.urlutils.config', config_stub)
monkeypatch.setattr('qutebrowser.utils.urlutils.config', config_stub)
assert not urlutils.is_url('test foo')