本文整理匯總了Python中unittest.mock.m方法的典型用法代碼示例。如果您正苦於以下問題:Python mock.m方法的具體用法?Python mock.m怎麽用?Python mock.m使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類unittest.mock
的用法示例。
在下文中一共展示了mock.m方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_dir
# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import m [as 別名]
def test_dir(self):
mock = Mock()
attrs = set(dir(mock))
type_attrs = set([m for m in dir(Mock) if not m.startswith('_')])
# all public attributes from the type are included
self.assertEqual(set(), type_attrs - attrs)
# creates these attributes
mock.a, mock.b
self.assertIn('a', dir(mock))
self.assertIn('b', dir(mock))
# instance attributes
mock.c = mock.d = None
self.assertIn('c', dir(mock))
self.assertIn('d', dir(mock))
# magic methods
mock.__iter__ = lambda s: iter([])
self.assertIn('__iter__', dir(mock))
示例2: test_side_effect_iterator_exceptions
# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import m [as 別名]
def test_side_effect_iterator_exceptions(self):
for Klass in Mock, MagicMock:
iterable = (ValueError, 3, KeyError, 6)
m = Klass(side_effect=iterable)
self.assertRaises(ValueError, m)
self.assertEqual(m(), 3)
self.assertRaises(KeyError, m)
self.assertEqual(m(), 6)
示例3: test_magic_methods_mock_calls
# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import m [as 別名]
def test_magic_methods_mock_calls(self):
for Klass in Mock, MagicMock:
m = Klass()
m.__int__ = Mock(return_value=3)
m.__float__ = MagicMock(return_value=3.0)
int(m)
float(m)
self.assertEqual(m.mock_calls, [call.__int__(), call.__float__()])
self.assertEqual(m.method_calls, [])
示例4: test_attribute_deletion
# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import m [as 別名]
def test_attribute_deletion(self):
# this behaviour isn't *useful*, but at least it's now tested...
for Klass in Mock, MagicMock, NonCallableMagicMock, NonCallableMock:
m = Klass()
original = m.foo
m.foo = 3
del m.foo
self.assertEqual(m.foo, original)
new = m.foo = Mock()
del m.foo
self.assertEqual(m.foo, new)
示例5: test_mock_parents
# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import m [as 別名]
def test_mock_parents(self):
for Klass in Mock, MagicMock:
m = Klass()
original_repr = repr(m)
m.return_value = m
self.assertIs(m(), m)
self.assertEqual(repr(m), original_repr)
m.reset_mock()
self.assertIs(m(), m)
self.assertEqual(repr(m), original_repr)
m = Klass()
m.b = m.a
self.assertIn("name='mock.a'", repr(m.b))
self.assertIn("name='mock.a'", repr(m.a))
m.reset_mock()
self.assertIn("name='mock.a'", repr(m.b))
self.assertIn("name='mock.a'", repr(m.a))
m = Klass()
original_repr = repr(m)
m.a = m()
m.a.return_value = m
self.assertEqual(repr(m), original_repr)
self.assertEqual(repr(m.a()), original_repr)
示例6: test_attach_mock_return_value
# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import m [as 別名]
def test_attach_mock_return_value(self):
classes = Mock, MagicMock, NonCallableMagicMock, NonCallableMock
for Klass in Mock, MagicMock:
for Klass2 in classes:
m = Klass()
m2 = Klass2(name='foo')
m.attach_mock(m2, 'return_value')
self.assertIs(m(), m2)
self.assertIn("name='mock()'", repr(m2))
m2.foo()
self.assertEqual(m.mock_calls, call().foo().call_list())
示例7: test_create_autospec_with_name
# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import m [as 別名]
def test_create_autospec_with_name(self):
m = mock.create_autospec(object(), name='sweet_func')
self.assertIn('sweet_func', repr(m))
#Issue21238
示例8: test_mock_unsafe
# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import m [as 別名]
def test_mock_unsafe(self):
m = Mock()
with self.assertRaises(AttributeError):
m.assert_foo_call()
with self.assertRaises(AttributeError):
m.assret_foo_call()
m = Mock(unsafe=True)
m.assert_foo_call()
m.assret_foo_call()
#Issue21262
示例9: test_assert_not_called
# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import m [as 別名]
def test_assert_not_called(self):
m = Mock()
m.hello.assert_not_called()
m.hello()
with self.assertRaises(AssertionError):
m.hello.assert_not_called()
#Issue21256 printout of keyword args should be in deterministic order
示例10: test_override_tuple_methods
# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import m [as 別名]
def test_override_tuple_methods(self):
c = call.count()
i = call.index(132,'hello')
m = Mock()
m.count()
m.index(132,"hello")
self.assertEqual(m.method_calls[0], c)
self.assertEqual(m.method_calls[1], i)
示例11: test_attach_mock
# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import m [as 別名]
def test_attach_mock(self):
classes = Mock, MagicMock, NonCallableMagicMock, NonCallableMock
for Klass in classes:
for Klass2 in classes:
m = Klass()
m2 = Klass2(name='foo')
m.attach_mock(m2, 'bar')
self.assertIs(m.bar, m2)
self.assertIn("name='mock.bar'", repr(m2))
m.bar.baz(1)
self.assertEqual(m.mock_calls, [call.bar.baz(1)])
self.assertEqual(m.method_calls, [call.bar.baz(1)])
示例12: test_assert_called_with_any
# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import m [as 別名]
def test_assert_called_with_any(self):
m = MagicMock()
m(MagicMock())
m.assert_called_with(mock.ANY)
示例13: test_assert_called
# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import m [as 別名]
def test_assert_called(self):
m = Mock()
with self.assertRaises(AssertionError):
m.hello.assert_called()
m.hello()
m.hello.assert_called()
m.hello()
m.hello.assert_called()
示例14: test_assert_called_once
# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import m [as 別名]
def test_assert_called_once(self):
m = Mock()
with self.assertRaises(AssertionError):
m.hello.assert_called_once()
m.hello()
m.hello.assert_called_once()
m.hello()
with self.assertRaises(AssertionError):
m.hello.assert_called_once()
#Issue21256 printout of keyword args should be in deterministic order
示例15: test_sorted_call_signature
# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import m [as 別名]
def test_sorted_call_signature(self):
m = Mock()
m.hello(name='hello', daddy='hero')
text = "call(daddy='hero', name='hello')"
self.assertEqual(repr(m.hello.call_args), text)
#Issue21270 overrides tuple methods for mock.call objects