11import pytest
2+ import mock as mock_module
23
34
45class 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
4660def 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 ()
0 commit comments