Skip to content

Commit 1ad4d7e

Browse files
committed
adding support for patch.multiple
1 parent f139592 commit 1ad4d7e

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

test_pytest_mock.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55

66
class MockFixture(object):
7-
87
def __init__(self):
98
self._patches = []
109
self.patch = self._Patcher(self._patches)
@@ -27,6 +26,15 @@ def object(self, *args, **kwargs):
2726
return mocked
2827

2928

29+
def multiple(self, *args, **kwargs):
30+
import mock
31+
32+
p = mock.patch.multiple(*args, **kwargs)
33+
mocked = p.start()
34+
self._patches.append(p)
35+
return mocked
36+
37+
3038
def __call__(self, *args, **kwargs):
3139
import mock
3240

@@ -56,13 +64,23 @@ def ls(cls, path):
5664
@pytest.fixture
5765
def check_unix_fs_mocked(tmpdir, mock):
5866
def check(mocked_rm, mocked_ls):
67+
assert mocked_rm is os.remove
68+
assert mocked_ls is os.listdir
69+
5970
file_name = tmpdir / 'foo.txt'
6071
file_name.ensure()
72+
6173
UnixFS.rm(str(file_name))
62-
os.remove.assert_called_once_with(str(file_name))
6374
mocked_rm.assert_called_once_with(str(file_name))
6475
assert os.path.isfile(str(file_name))
76+
77+
mocked_ls.return_value = ['bar.txt']
78+
assert UnixFS.ls(str(tmpdir)) == ['bar.txt']
79+
mocked_ls.assert_called_once_with(str(tmpdir))
80+
6581
mock.stopall()
82+
83+
assert UnixFS.ls(str(tmpdir)) == ['foo.txt']
6684
UnixFS.rm(str(file_name))
6785
assert not os.path.isfile(str(file_name))
6886

@@ -77,7 +95,15 @@ def mock_using_patch(mock):
7795
return mock.patch('os.remove'), mock.patch('os.listdir')
7896

7997

80-
@pytest.mark.parametrize('mock_fs', [mock_using_patch_object, mock_using_patch],
98+
def mock_using_patch_multiple(mock):
99+
from mock import DEFAULT
100+
101+
r = mock.patch.multiple('os', remove=DEFAULT, listdir=DEFAULT)
102+
return r['remove'], r['listdir']
103+
104+
105+
@pytest.mark.parametrize('mock_fs', [mock_using_patch_object, mock_using_patch,
106+
mock_using_patch_multiple],
81107
)
82108
def test_fixture(mock_fs, mock, check_unix_fs_mocked):
83109
mocked_rm, mocked_ls = mock_fs(mock)

0 commit comments

Comments
 (0)