From 2b12b4bfced02d4f646e8dcdc046bf18e854e434 Mon Sep 17 00:00:00 2001 From: nvbn Date: Thu, 7 May 2015 13:42:52 +0200 Subject: [PATCH] Improve tests with `mocker` --- requirements.txt | 1 + tests/test_conf.py | 6 ++---- tests/test_main.py | 32 +++++++++++++------------------- tests/test_shells.py | 31 ++++++++++++------------------- 4 files changed, 28 insertions(+), 42 deletions(-) diff --git a/requirements.txt b/requirements.txt index 625bed64..1a6161ba 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ pytest mock +pytest-mock diff --git a/tests/test_conf.py b/tests/test_conf.py index 3363a5e2..1fde6a67 100644 --- a/tests/test_conf.py +++ b/tests/test_conf.py @@ -14,10 +14,8 @@ def test_default(enabled, rules, result): @pytest.fixture -def load_source(monkeypatch): - mock = Mock() - monkeypatch.setattr('thefuck.conf.load_source', mock) - return mock +def load_source(mocker): + return mocker.patch('thefuck.conf.load_source') @pytest.fixture diff --git a/tests/test_main.py b/tests/test_main.py index 44b4beef..fe82cfaa 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -6,15 +6,15 @@ from thefuck import main, conf, types from tests.utils import Rule, Command -def test_load_rule(monkeypatch): +def test_load_rule(mocker): match = object() get_new_command = object() - load_source = Mock() - load_source.return_value = Mock(match=match, - get_new_command=get_new_command, - enabled_by_default=True, - priority=900) - monkeypatch.setattr('thefuck.main.load_source', load_source) + load_source = mocker.patch( + 'thefuck.main.load_source', + return_value=Mock(match=match, + get_new_command=get_new_command, + enabled_by_default=True, + priority=900)) assert main.load_rule(Path('/rules/bash.py')) \ == Rule('bash', match, get_new_command, priority=900) load_source.assert_called_once_with('bash', '/rules/bash.py') @@ -22,10 +22,8 @@ def test_load_rule(monkeypatch): class TestGetRules(object): @pytest.fixture(autouse=True) - def glob(self, monkeypatch): - mock = Mock(return_value=[]) - monkeypatch.setattr('thefuck.main.Path.glob', mock) - return mock + def glob(self, mocker): + return mocker.patch('thefuck.main.Path.glob', return_value=[]) def _compare_names(self, rules, names): return [r.name for r in rules] == names @@ -118,10 +116,8 @@ class TestGetMatchedRule(object): class TestRunRule(object): @pytest.fixture(autouse=True) - def confirm(self, monkeypatch): - mock = Mock(return_value=True) - monkeypatch.setattr('thefuck.main.confirm', mock) - return mock + def confirm(self, mocker): + return mocker.patch('thefuck.main.confirm', return_value=True) def test_run_rule(self, capsys): main.run_rule(Rule(get_new_command=lambda *_: 'new-command'), @@ -147,10 +143,8 @@ class TestRunRule(object): class TestConfirm(object): @pytest.fixture - def stdin(self, monkeypatch): - mock = Mock(return_value='\n') - monkeypatch.setattr('sys.stdin.read', mock) - return mock + def stdin(self, mocker): + return mocker.patch('sys.stdin.read', return_value='\n') def test_when_not_required(self, capsys): assert main.confirm('command', None, Mock(require_confirmation=False)) diff --git a/tests/test_shells.py b/tests/test_shells.py index 5b2748e5..b00cfab4 100644 --- a/tests/test_shells.py +++ b/tests/test_shells.py @@ -1,20 +1,15 @@ import pytest -from mock import Mock, MagicMock from thefuck import shells @pytest.fixture -def builtins_open(monkeypatch): - mock = MagicMock() - monkeypatch.setattr('six.moves.builtins.open', mock) - return mock +def builtins_open(mocker): + return mocker.patch('six.moves.builtins.open') @pytest.fixture -def isfile(monkeypatch): - mock = Mock(return_value=True) - monkeypatch.setattr('os.path.isfile', mock) - return mock +def isfile(mocker): + return mocker.patch('os.path.isfile', return_value=True) class TestGeneric(object): @@ -32,9 +27,8 @@ class TestGeneric(object): @pytest.mark.usefixtures('isfile') class TestBash(object): @pytest.fixture(autouse=True) - def Popen(self, monkeypatch): - mock = Mock() - monkeypatch.setattr('thefuck.shells.Popen', mock) + def Popen(self, mocker): + mock = mocker.patch('thefuck.shells.Popen') mock.return_value.stdout.read.return_value = ( b'alias l=\'ls -CF\'\n' b'alias la=\'ls -A\'\n' @@ -52,16 +46,15 @@ class TestBash(object): def test_put_to_history(self, builtins_open): shells.Bash().put_to_history('ls') - builtins_open.return_value.__enter__.return_value.\ + builtins_open.return_value.__enter__.return_value. \ write.assert_called_once_with('ls\n') @pytest.mark.usefixtures('isfile') class TestZsh(object): @pytest.fixture(autouse=True) - def Popen(self, monkeypatch): - mock = Mock() - monkeypatch.setattr('thefuck.shells.Popen', mock) + def Popen(self, mocker): + mock = mocker.patch('thefuck.shells.Popen') mock.return_value.stdout.read.return_value = ( b'l=\'ls -CF\'\n' b'la=\'ls -A\'\n' @@ -77,9 +70,9 @@ class TestZsh(object): def test_to_shell(self): assert shells.Zsh().to_shell('pwd') == 'pwd' - def test_put_to_history(self, builtins_open, monkeypatch): - monkeypatch.setattr('thefuck.shells.time', - lambda: 1430707243.3517463) + def test_put_to_history(self, builtins_open, mocker): + mocker.patch('thefuck.shells.time', + return_value=1430707243.3517463) shells.Zsh().put_to_history('ls') builtins_open.return_value.__enter__.return_value. \ write.assert_called_once_with(': 1430707243:0;ls\n') \ No newline at end of file