Skip to content

Commit f7080a3

Browse files
committed
initial version
1 parent eb7c475 commit f7080a3

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

test_pytest_mock.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import os
2+
3+
import pytest
4+
5+
6+
class MockFixture(object):
7+
8+
def __init__(self):
9+
self._patches = []
10+
self.patch = self._PatchWrapper(self._patches)
11+
12+
def stopall(self):
13+
for p in self._patches:
14+
p.stop()
15+
self._patches[:] = []
16+
17+
class _PatchWrapper(object):
18+
19+
def __init__(self, patches):
20+
self._patches = patches
21+
22+
def object(self, *args, **kwargs):
23+
import mock
24+
p = mock.patch.object(*args, **kwargs)
25+
mocked = p.start()
26+
self._patches.append(p)
27+
return mocked
28+
29+
30+
@pytest.yield_fixture
31+
def mock():
32+
result = MockFixture()
33+
yield result
34+
result.stopall()
35+
36+
37+
class UnixFS(object):
38+
39+
@classmethod
40+
def rm(cls, filename):
41+
os.remove(filename)
42+
43+
@classmethod
44+
def ls(cls, path):
45+
return os.listdir(path)
46+
47+
48+
def test_fixture(mock, tmpdir):
49+
mock.patch.object(os, 'remove')
50+
(tmpdir / 'foo.txt').ensure()
51+
UnixFS.rm(tmpdir / 'foo.txt')
52+
os.remove.assert_called_once_with(tmpdir / 'foo.txt')
53+
assert os.path.isfile(str(tmpdir / 'foo.txt'))
54+
mock.stopall()
55+
UnixFS.rm(str(tmpdir / 'foo.txt'))
56+
assert not os.path.isfile(str(tmpdir / 'foo.txt'))

0 commit comments

Comments
 (0)