MCPcopy Create free account
hub / github.com/python/cpython / test_autospec_mock

Method test_autospec_mock

Lib/test/test_unittest/testmock/testmock.py:237–264  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

235
236
237 def test_autospec_mock(self):
238 class A(object):
239 class B(object):
240 C = None
241
242 with mock.patch.object(A, 'B'):
243 with self.assertRaisesRegex(InvalidSpecError,
244 "Cannot autospec attr 'B' from target <MagicMock spec='A'"):
245 create_autospec(A).B
246 with self.assertRaisesRegex(InvalidSpecError,
247 "Cannot autospec attr 'B' from target 'A'"):
248 mock.patch.object(A, 'B', autospec=True).start()
249 with self.assertRaisesRegex(InvalidSpecError,
250 "Cannot autospec attr 'C' as the patch target "):
251 mock.patch.object(A.B, 'C', autospec=True).start()
252 with self.assertRaisesRegex(InvalidSpecError,
253 "Cannot spec attr 'B' as the spec "):
254 mock.patch.object(A, 'B', spec=A.B).start()
255 with self.assertRaisesRegex(InvalidSpecError,
256 "Cannot spec attr 'B' as the spec_set "):
257 mock.patch.object(A, 'B', spec_set=A.B).start()
258 with self.assertRaisesRegex(InvalidSpecError,
259 "Cannot spec attr 'B' as the spec_set "):
260 mock.patch.object(A, 'B', spec_set=A.B).start()
261 with self.assertRaisesRegex(InvalidSpecError, "Cannot spec a Mock object."):
262 mock.Mock(A.B)
263 with mock.patch('builtins.open', mock.mock_open()):
264 mock.mock_open() # should still be valid with open() mocked
265
266 def test_create_autospec_wraps_class(self):
267 """Autospec a class with wraps & test if the call is passed to the

Callers

nothing calls this directly

Calls 4

create_autospecFunction · 0.90
assertRaisesRegexMethod · 0.80
mock_openMethod · 0.80
startMethod · 0.45

Tested by

no test coverage detected