Skip to content

Commit 39a75b6

Browse files
committed
improved code docs
1 parent 5ed066c commit 39a75b6

File tree

2 files changed

+52
-18
lines changed

2 files changed

+52
-18
lines changed

pytest_mock.py

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,67 @@
11
import pytest
2+
import mock as mock_module
23

34

45
class MockFixture(object):
6+
"""
7+
Fixture that provides the same interface to functions in the mock module,
8+
ensuring that they are uninstalled at the end of each test.
9+
"""
10+
511
def __init__(self):
6-
self._patches = []
12+
self._patches = [] # list of mock._patch objects
713
self.patch = self._Patcher(self._patches)
814

915
def stopall(self):
16+
"""
17+
Stop all patchers started by this fixture. Can be safely called multiple
18+
times.
19+
"""
1020
for p in self._patches:
1121
p.stop()
1222
self._patches[:] = []
1323

1424
class _Patcher(object):
25+
"""
26+
Object to provide the same interface as mock.patch, mock.patch.object,
27+
etc. We need this indirection to keep the same API of the mock package.
28+
"""
29+
1530
def __init__(self, patches):
1631
self._patches = patches
1732

18-
def object(self, *args, **kwargs):
19-
import mock
20-
21-
p = mock.patch.object(*args, **kwargs)
33+
def _start_patch(self, mock_func, *args, **kwargs):
34+
"""Patches something by calling the given function from the mock
35+
module, registering the patch to stop it later and returns the
36+
mock object resulting from the mock call.
37+
"""
38+
p = mock_func(*args, **kwargs)
2239
mocked = p.start()
2340
self._patches.append(p)
2441
return mocked
2542

43+
def object(self, *args, **kwargs):
44+
"""API to mock.patch.object"""
45+
return self._start_patch(mock_module.patch.object, *args, **kwargs)
2646

27-
def multiple(self, *args, **kwargs):
28-
import mock
2947

30-
p = mock.patch.multiple(*args, **kwargs)
31-
mocked = p.start()
32-
self._patches.append(p)
33-
return mocked
48+
def multiple(self, *args, **kwargs):
49+
"""API to mock.patch.multiple"""
50+
return self._start_patch(mock_module.patch.multiple, *args,
51+
**kwargs)
3452

3553

3654
def __call__(self, *args, **kwargs):
37-
import mock
38-
39-
p = mock.patch(*args, **kwargs)
40-
mocked = p.start()
41-
self._patches.append(p)
42-
return mocked
55+
"""API to mock.patch"""
56+
return self._start_patch(mock_module.patch, *args, **kwargs)
4357

4458

4559
@pytest.yield_fixture
4660
def mock():
61+
"""
62+
return an object that has the same interface to the `mock` module, but
63+
takes care of automatically undoing all patches after each test method.
64+
"""
4765
result = MockFixture()
4866
yield result
4967
result.stopall()

test_pytest_mock.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22

33
import pytest
44

5+
56
class UnixFS(object):
7+
"""
8+
Wrapper to os functions to simulate a Unix file system, used for testing
9+
the mock fixture.
10+
"""
11+
612
@classmethod
713
def rm(cls, filename):
814
os.remove(filename)
@@ -14,6 +20,11 @@ def ls(cls, path):
1420

1521
@pytest.fixture
1622
def check_unix_fs_mocked(tmpdir, mock):
23+
"""
24+
performs a standard test in a UnixFS, assuming that both `os.remove` and
25+
`os.listdir` have been mocked previously.
26+
"""
27+
1728
def check(mocked_rm, mocked_ls):
1829
assert mocked_rm is os.remove
1930
assert mocked_ls is os.listdir
@@ -56,6 +67,11 @@ def mock_using_patch_multiple(mock):
5667
@pytest.mark.parametrize('mock_fs', [mock_using_patch_object, mock_using_patch,
5768
mock_using_patch_multiple],
5869
)
59-
def test_fixture(mock_fs, mock, check_unix_fs_mocked):
70+
def test_mock_fixture(mock_fs, mock, check_unix_fs_mocked):
71+
"""
72+
Installs mocks into `os` functions and performs a standard testing of
73+
mock functionality. We parametrize different mock methods to ensure
74+
all (intended, at least) mock API is covered.
75+
"""
6076
mocked_rm, mocked_ls = mock_fs(mock)
6177
check_unix_fs_mocked(mocked_rm, mocked_ls)

0 commit comments

Comments
 (0)