@@ -60,6 +60,15 @@ def ls(cls, path):
60
60
return os .listdir (path )
61
61
62
62
63
+ class TestObject :
64
+ """
65
+ Class that is used for testing create_autospec with child mocks
66
+ """
67
+
68
+ def run (self ) -> str :
69
+ return "not mocked"
70
+
71
+
63
72
@pytest .fixture
64
73
def check_unix_fs_mocked (
65
74
tmpdir : Any , mocker : MockerFixture
@@ -156,7 +165,6 @@ def test_mock_patch_dict_resetall(mocker: MockerFixture) -> None:
156
165
[
157
166
"ANY" ,
158
167
"call" ,
159
- "create_autospec" ,
160
168
"MagicMock" ,
161
169
"Mock" ,
162
170
"mock_open" ,
@@ -185,23 +193,35 @@ def test_mocker_resetall(mocker: MockerFixture) -> None:
185
193
listdir = mocker .patch ("os.listdir" , return_value = "foo" )
186
194
open = mocker .patch ("os.open" , side_effect = ["bar" , "baz" ])
187
195
196
+ mocked_object = mocker .create_autospec (TestObject )
197
+ mocked_object .run .return_value = "mocked"
198
+
188
199
assert listdir ("/tmp" ) == "foo"
189
200
assert open ("/tmp/foo.txt" ) == "bar"
201
+ assert mocked_object .run () == "mocked"
190
202
listdir .assert_called_once_with ("/tmp" )
191
203
open .assert_called_once_with ("/tmp/foo.txt" )
204
+ mocked_object .run .assert_called_once ()
192
205
193
206
mocker .resetall ()
194
207
195
208
assert not listdir .called
196
209
assert not open .called
210
+ assert not mocked_object .called
197
211
assert listdir .return_value == "foo"
198
212
assert list (open .side_effect ) == ["baz" ]
213
+ assert mocked_object .run .return_value == "mocked"
199
214
200
215
mocker .resetall (return_value = True , side_effect = True )
201
216
202
217
assert isinstance (listdir .return_value , mocker .Mock )
203
218
assert open .side_effect is None
204
219
220
+ if sys .version_info >= (3 , 9 ):
221
+ # The reset on child mocks have been implemented in 3.9
222
+ # https://bugs.python.org/issue38932
223
+ assert mocked_object .run .return_value != "mocked"
224
+
205
225
206
226
class TestMockerStub :
207
227
def test_call (self , mocker : MockerFixture ) -> None :
0 commit comments