3
3
import pytest
4
4
5
5
6
+ pytest_plugins = 'pytester'
7
+
6
8
class UnixFS (object ):
7
9
"""
8
10
Wrapper to os functions to simulate a Unix file system, used for testing
@@ -19,7 +21,7 @@ def ls(cls, path):
19
21
20
22
21
23
@pytest .fixture
22
- def check_unix_fs_mocked (tmpdir , mock ):
24
+ def check_unix_fs_mocked (tmpdir , mocker ):
23
25
"""
24
26
performs a standard test in a UnixFS, assuming that both `os.remove` and
25
27
`os.listdir` have been mocked previously.
@@ -40,7 +42,7 @@ def check(mocked_rm, mocked_ls):
40
42
assert UnixFS .ls (str (tmpdir )) == ['bar.txt' ]
41
43
mocked_ls .assert_called_once_with (str (tmpdir ))
42
44
43
- mock .stopall ()
45
+ mocker .stopall ()
44
46
45
47
assert UnixFS .ls (str (tmpdir )) == ['foo.txt' ]
46
48
UnixFS .rm (str (file_name ))
@@ -49,43 +51,63 @@ def check(mocked_rm, mocked_ls):
49
51
return check
50
52
51
53
52
- def mock_using_patch_object (mock ):
53
- return mock .patch .object (os , 'remove' ), mock .patch .object (os , 'listdir' )
54
+ def mock_using_patch_object (mocker ):
55
+ return mocker .patch .object (os , 'remove' ), mocker .patch .object (os , 'listdir' )
54
56
55
57
56
- def mock_using_patch (mock ):
57
- return mock .patch ('os.remove' ), mock .patch ('os.listdir' )
58
+ def mock_using_patch (mocker ):
59
+ return mocker .patch ('os.remove' ), mocker .patch ('os.listdir' )
58
60
59
61
60
- def mock_using_patch_multiple (mock ):
62
+ def mock_using_patch_multiple (mocker ):
61
63
from pytest_mock import mock_module
62
64
63
- r = mock .patch .multiple ('os' , remove = mock_module .DEFAULT ,
64
- listdir = mock_module .DEFAULT )
65
+ r = mocker .patch .multiple ('os' , remove = mock_module .DEFAULT ,
66
+ listdir = mock_module .DEFAULT )
65
67
return r ['remove' ], r ['listdir' ]
66
68
67
69
68
70
@pytest .mark .parametrize ('mock_fs' , [mock_using_patch_object , mock_using_patch ,
69
71
mock_using_patch_multiple ],
70
72
)
71
- def test_mock_patches (mock_fs , mock , check_unix_fs_mocked ):
73
+ def test_mock_patches (mock_fs , mocker , check_unix_fs_mocked ):
72
74
"""
73
75
Installs mocks into `os` functions and performs a standard testing of
74
76
mock functionality. We parametrize different mock methods to ensure
75
77
all (intended, at least) mock API is covered.
76
78
"""
77
- mock_fs (mock )
78
- mocked_rm , mocked_ls = mock_fs (mock )
79
+ # mock it twice on purpose to ensure we unmock it correctly later
80
+ mock_fs (mocker )
81
+ mocked_rm , mocked_ls = mock_fs (mocker )
79
82
check_unix_fs_mocked (mocked_rm , mocked_ls )
80
83
81
84
82
- def test_mock_patch_dict (mock ):
85
+ def test_mock_patch_dict (mocker ):
83
86
"""
84
87
Testing
85
88
:param mock:
86
89
"""
87
90
x = {'original' : 1 }
88
- mock .patch .dict (x , values = [('new' , 10 )], clear = True )
91
+ mocker .patch .dict (x , values = [('new' , 10 )], clear = True )
89
92
assert x == {'new' : 10 }
90
- mock .stopall ()
93
+ mocker .stopall ()
91
94
assert x == {'original' : 1 }
95
+
96
+
97
+ def test_mock_fixture_is_deprecated (testdir ):
98
+ """
99
+ Test that a warning emitted when using deprecated "mock" fixture.
100
+ """
101
+ testdir .makepyfile ('''
102
+ import warnings
103
+ import os
104
+ warnings.simplefilter('always')
105
+
106
+ def test_foo(mock, tmpdir):
107
+ mock.patch('os.listdir', return_value=['mocked'])
108
+ assert os.listdir(str(tmpdir)) == ['mocked']
109
+ mock.stopall()
110
+ assert os.listdir(str(tmpdir)) == []
111
+ ''' )
112
+ result = testdir .runpytest ('-s' )
113
+ result .stderr .fnmatch_lines (['*"mock" fixture has been deprecated*' ])
0 commit comments