本文整理汇总了Python中unittest.mock.assert_called_with方法的典型用法代码示例。如果您正苦于以下问题:Python mock.assert_called_with方法的具体用法?Python mock.assert_called_with怎么用?Python mock.assert_called_with使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类unittest.mock
的用法示例。
在下文中一共展示了mock.assert_called_with方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_assert_called_with_method_spec
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import assert_called_with [as 别名]
def test_assert_called_with_method_spec(self):
def _check(mock):
mock(1, b=2, c=3)
mock.assert_called_with(1, 2, 3)
mock.assert_called_with(a=1, b=2, c=3)
self.assertRaises(AssertionError, mock.assert_called_with,
1, b=3, c=2)
mock = Mock(spec=Something().meth)
_check(mock)
mock = Mock(spec=Something.cmeth)
_check(mock)
mock = Mock(spec=Something().cmeth)
_check(mock)
mock = Mock(spec=Something.smeth)
_check(mock)
mock = Mock(spec=Something().smeth)
_check(mock)
示例2: test_before_test_called_before_user_setup
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import assert_called_with [as 别名]
def test_before_test_called_before_user_setup(self):
mock = unittest.mock.Mock()
setattr(asynctest._fail_on._fail_on, "before_test_default", mock)
self.addCleanup(delattr, asynctest._fail_on._fail_on,
"before_test_default")
class TestCase(asynctest.TestCase):
def setUp(self):
self.assertTrue(mock.called)
def runTest(self):
pass
for method in self.run_methods:
with self.subTest(method=method):
case = Test.FooTestCase()
getattr(case, method)()
self.assert_checked("default")
self.assertTrue(mock.called)
mock.assert_called_with(case)
示例3: test_side_effect
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import assert_called_with [as 别名]
def test_side_effect(self):
mock = Mock()
def effect(*args, **kwargs):
raise SystemError('kablooie')
mock.side_effect = effect
self.assertRaises(SystemError, mock, 1, 2, fish=3)
mock.assert_called_with(1, 2, fish=3)
results = [1, 2, 3]
def effect():
return results.pop()
mock.side_effect = effect
self.assertEqual([mock(), mock(), mock()], [3, 2, 1],
"side effect not used correctly")
mock = Mock(side_effect=sentinel.SideEffect)
self.assertEqual(mock.side_effect, sentinel.SideEffect,
"side effect in constructor not used")
def side_effect():
return DEFAULT
mock = Mock(side_effect=side_effect, return_value=sentinel.RETURN)
self.assertEqual(mock(), sentinel.RETURN)
示例4: test_java_exception_side_effect
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import assert_called_with [as 别名]
def test_java_exception_side_effect(self):
import java
mock = Mock(side_effect=java.lang.RuntimeException("Boom!"))
# can't use assertRaises with java exceptions
try:
mock(1, 2, fish=3)
except java.lang.RuntimeException:
pass
else:
self.fail('java exception not raised')
mock.assert_called_with(1,2, fish=3)
示例5: test_assert_called_with
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import assert_called_with [as 别名]
def test_assert_called_with(self):
mock = Mock()
mock()
# Will raise an exception if it fails
mock.assert_called_with()
self.assertRaises(AssertionError, mock.assert_called_with, 1)
mock.reset_mock()
self.assertRaises(AssertionError, mock.assert_called_with)
mock(1, 2, 3, a='fish', b='nothing')
mock.assert_called_with(1, 2, 3, a='fish', b='nothing')
示例6: test_wraps_calls
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import assert_called_with [as 别名]
def test_wraps_calls(self):
real = Mock()
mock = Mock(wraps=real)
self.assertEqual(mock(), real())
real.reset_mock()
mock(1, 2, fish=3)
real.assert_called_with(1, 2, fish=3)
示例7: test_wraps_attributes
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import assert_called_with [as 别名]
def test_wraps_attributes(self):
class Real(object):
attribute = Mock()
real = Real()
mock = Mock(wraps=real)
self.assertEqual(mock.attribute(), real.attribute())
self.assertRaises(AttributeError, lambda: mock.fish)
self.assertNotEqual(mock.attribute, real.attribute)
result = mock.attribute.frog(1, 2, fish=3)
Real.attribute.frog.assert_called_with(1, 2, fish=3)
self.assertEqual(result, Real.attribute.frog())
示例8: test_spec_list_subclass
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import assert_called_with [as 别名]
def test_spec_list_subclass(self):
class Sub(list):
pass
mock = Mock(spec=Sub(['foo']))
mock.append(3)
mock.append.assert_called_with(3)
self.assertRaises(AttributeError, getattr, mock, 'foo')
示例9: test_setting_call
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import assert_called_with [as 别名]
def test_setting_call(self):
mock = Mock()
def __call__(self, a):
return self._mock_call(a)
type(mock).__call__ = __call__
mock('one')
mock.assert_called_with('one')
self.assertRaises(TypeError, mock, 'one', 'two')
示例10: test_060_vm_property_reset
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import assert_called_with [as 别名]
def test_060_vm_property_reset(self):
with unittest.mock.patch('qubes.property.__delete__') as mock:
value = self.call_mgmt_func(b'admin.vm.property.Reset', b'test-vm1',
b'default_user')
mock.assert_called_with(self.vm)
self.assertIsNone(value)
self.app.save.assert_called_once_with()
示例11: test_450_property_reset
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import assert_called_with [as 别名]
def test_450_property_reset(self):
# actual function tested for admin.vm.property.* already
with unittest.mock.patch('qubes.property.__delete__') as mock:
value = self.call_mgmt_func(b'admin.property.Reset', b'dom0',
b'clockvm')
mock.assert_called_with(self.app)
self.assertIsNone(value)
self.app.save.assert_called_once_with()
示例12: test_621_backup_execute_passphrase_service
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import assert_called_with [as 别名]
def test_621_backup_execute_passphrase_service(self, mock_backup):
backup_profile = (
'include:\n'
' - test-vm1\n'
'destination_vm: test-vm1\n'
'destination_path: /home/user\n'
'passphrase_vm: test-vm1\n'
)
@asyncio.coroutine
def service_passphrase(*args, **kwargs):
return (b'pass-from-vm', None)
mock_backup.return_value.backup_do.side_effect = self.dummy_coro
self.vm.run_service_for_stdio = unittest.mock.Mock(
side_effect=service_passphrase)
with tempfile.TemporaryDirectory() as profile_dir:
with open(os.path.join(profile_dir, 'testprofile.conf'), 'w') as \
profile_file:
profile_file.write(backup_profile)
with unittest.mock.patch('qubes.config.backup_profile_dir',
profile_dir):
result = self.call_mgmt_func(b'admin.backup.Execute', b'dom0',
b'testprofile')
self.assertIsNone(result)
mock_backup.assert_called_once_with(
self.app,
{self.vm},
set(),
target_vm=self.vm,
target_dir='/home/user',
compressed=True,
passphrase=b'pass-from-vm')
mock_backup.return_value.backup_do.assert_called_once_with()
self.vm.run_service_for_stdio.assert_called_with(
'qubes.BackupPassphrase+testprofile')
示例13: test_assert_called_with_function_spec
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import assert_called_with [as 别名]
def test_assert_called_with_function_spec(self):
def f(a, b, c, d=None):
pass
mock = Mock(spec=f)
mock(1, b=2, c=3)
mock.assert_called_with(1, 2, 3)
mock.assert_called_with(a=1, b=2, c=3)
self.assertRaises(AssertionError, mock.assert_called_with,
1, b=3, c=2)
# Expected call doesn't match the spec's signature
with self.assertRaises(AssertionError) as cm:
mock.assert_called_with(e=8)
self.assertIsInstance(cm.exception.__cause__, TypeError)
示例14: test_assert_called_with_message
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import assert_called_with [as 别名]
def test_assert_called_with_message(self):
mock = Mock()
self.assertRaisesRegex(AssertionError, 'Not called',
mock.assert_called_with)
示例15: test_assert_called_with_any
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import assert_called_with [as 别名]
def test_assert_called_with_any(self):
m = MagicMock()
m(MagicMock())
m.assert_called_with(mock.ANY)