本文整理汇总了Python中unittest.mock.assert_called_once_with函数的典型用法代码示例。如果您正苦于以下问题:Python assert_called_once_with函数的具体用法?Python assert_called_once_with怎么用?Python assert_called_once_with使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了assert_called_once_with函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: test_existing_cache_with_unrelated_data
def test_existing_cache_with_unrelated_data(self):
section = Section('test-section')
filedict = {}
# Start with some unrelated cache values. Those are ignored as they are
# never hit during a cache lookup.
cache = {CustomTasksBear: {b'123456': [100, 101, 102]}}
task_args = -1, -2, -3
bear = CustomTasksBear(section, filedict, tasks=[task_args])
with unittest.mock.patch.object(bear, 'analyze',
wraps=bear.analyze) as mock:
# First time we have a cache miss.
results = self.execute_run({bear}, cache)
mock.assert_called_once_with(*task_args)
self.assertEqual(results, list(task_args))
self.assertEqual(len(cache), 1)
self.assertIn(CustomTasksBear, cache)
cache_values = next(iter(cache.values()))
self.assertEqual(len(cache_values), 2)
# The unrelated data is left untouched.
self.assertIn(b'123456', cache_values)
self.assertEqual(cache_values[b'123456'], [100, 101, 102])
开发者ID:Nosferatul,项目名称:coala,代码行数:25,代码来源:CoreTest.py
示例2: test_reject_ignores_rejected_price_lists
def test_reject_ignores_rejected_price_lists(self, mock):
self.price_list.reject(self.user)
admin.reject(None, self.request_mock, SubmittedPriceList.objects.all())
mock.assert_called_once_with(
self.request_mock,
messages.INFO,
'0 price list(s) have been rejected.'
)
开发者ID:18F,项目名称:calc,代码行数:8,代码来源:test_admin.py
示例3: test_retire_ignores_retired_price_lists
def test_retire_ignores_retired_price_lists(self, mock):
admin.retire(None, self.request_mock,
SubmittedPriceList.objects.all())
mock.assert_called_once_with(
self.request_mock,
messages.INFO,
'0 price list(s) have been retired and removed from CALC.'
)
开发者ID:18F,项目名称:calc,代码行数:8,代码来源:test_admin.py
示例4: test_with_sourcing
def test_with_sourcing(self, commands, config_stub, patch_editor):
assert config_stub.val.content.javascript.enabled
mock = patch_editor('c.content.javascript.enabled = False')
commands.config_edit()
mock.assert_called_once_with(unittest.mock.ANY)
assert not config_stub.val.content.javascript.enabled
开发者ID:blyxxyz,项目名称:qutebrowser,代码行数:8,代码来源:test_configcommands.py
示例5: test_approve_ignores_approved_price_lists
def test_approve_ignores_approved_price_lists(self, mock):
self.price_list.approve(self.user)
admin.approve(None, self.request_mock,
SubmittedPriceList.objects.all())
mock.assert_called_once_with(
self.request_mock,
messages.INFO,
'0 price list(s) have been approved and added to CALC.'
)
开发者ID:18F,项目名称:calc,代码行数:9,代码来源:test_admin.py
示例6: test_unicast_removes_for_true_result
def test_unicast_removes_for_true_result(self):
mock = unittest.mock.Mock()
mock.return_value = True
obj = object()
nh = TagDispatcher()
nh.add_callback("tag", mock)
nh.unicast("tag", obj)
with self.assertRaises(KeyError):
nh.unicast("tag", obj)
mock.assert_called_once_with(obj)
开发者ID:horazont,项目名称:aioxmpp,代码行数:12,代码来源:test_callbacks.py
示例7: test_cache
def test_cache(self):
section = Section('test-section')
filedict = {}
cache = {}
task_args = 10, 11, 12
bear = CustomTasksBear(section, filedict, tasks=[task_args])
with unittest.mock.patch.object(bear, 'analyze',
wraps=bear.analyze) as mock:
# First time we have a cache miss.
results = self.execute_run({bear}, cache)
mock.assert_called_once_with(*task_args)
self.assertEqual(results, list(task_args))
self.assertEqual(len(cache), 1)
self.assertEqual(next(iter(cache.keys())), CustomTasksBear)
self.assertEqual(len(next(iter(cache.values()))), 1)
# All following times we have a cache hit (we don't modify the
# cache in between).
for i in range(3):
mock.reset_mock()
results = self.execute_run({bear}, cache)
self.assertFalse(mock.called)
self.assertEqual(results, list(task_args))
self.assertEqual(len(cache), 1)
self.assertIn(CustomTasksBear, cache)
self.assertEqual(len(next(iter(cache.values()))), 1)
task_args = 500, 11, 12
bear = CustomTasksBear(section, filedict, tasks=[task_args])
with unittest.mock.patch.object(bear, 'analyze',
wraps=bear.analyze) as mock:
# Invocation with different args should add another cache entry,
# and invoke analyze() again because those weren't cached before.
results = self.execute_run({bear}, cache)
mock.assert_called_once_with(*task_args)
self.assertEqual(results, list(task_args))
self.assertEqual(len(cache), 1)
self.assertIn(CustomTasksBear, cache)
self.assertEqual(len(next(iter(cache.values()))), 2)
mock.reset_mock()
results = self.execute_run({bear}, cache)
self.assertFalse(mock.called)
self.assertEqual(results, list(task_args))
self.assertEqual(len(cache), 1)
self.assertIn(CustomTasksBear, cache)
self.assertEqual(len(next(iter(cache.values()))), 2)
开发者ID:Nosferatul,项目名称:coala,代码行数:52,代码来源:CoreTest.py
示例8: test_assert_called_once_with
def test_assert_called_once_with(self):
mock = Mock()
mock()
mock.assert_called_once_with()
mock()
self.assertRaises(AssertionError, mock.assert_called_once_with)
mock.reset_mock()
self.assertRaises(AssertionError, mock.assert_called_once_with)
mock('foo', 'bar', baz=2)
mock.assert_called_once_with('foo', 'bar', baz=2)
mock.reset_mock()
mock('foo', 'bar', baz=2)
self.assertRaises(AssertionError, lambda : mock.assert_called_once_with('bob', 'bar', baz=2))
开发者ID:johndpope,项目名称:sims4-ai-engine,代码行数:13,代码来源:testmock.py
示例9: test_connect_async
def test_connect_async(self):
signal = AdHocSignal()
mock = unittest.mock.MagicMock()
fun = functools.partial(mock)
signal.connect(fun, AdHocSignal.ASYNC_WITH_LOOP(None))
signal.fire()
mock.assert_not_called()
run_coroutine(asyncio.sleep(0))
mock.assert_called_once_with()
开发者ID:horazont,项目名称:aioxmpp,代码行数:14,代码来源:test_callbacks.py
示例10: test_assert_called_once_with
def test_assert_called_once_with(self):
mock = Mock()
mock()
# Will raise an exception if it fails
mock.assert_called_once_with()
mock()
self.assertRaises(AssertionError, mock.assert_called_once_with)
mock.reset_mock()
self.assertRaises(AssertionError, mock.assert_called_once_with)
mock("foo", "bar", baz=2)
mock.assert_called_once_with("foo", "bar", baz=2)
mock.reset_mock()
mock("foo", "bar", baz=2)
self.assertRaises(AssertionError, lambda: mock.assert_called_once_with("bob", "bar", baz=2))
开发者ID:GaloisInc,项目名称:echronos,代码行数:19,代码来源:testmock.py
示例11: test_no_cache
def test_no_cache(self):
# Two runs without using the cache shall always run analyze() again.
section = Section('test-section')
filedict = {}
task_args = 3, 4, 5
bear = CustomTasksBear(section, filedict, tasks=[task_args])
with unittest.mock.patch.object(bear, 'analyze',
wraps=bear.analyze) as mock:
# By default, omitting the cache parameter shall mean "no cache".
results = self.execute_run({bear})
mock.assert_called_once_with(*task_args)
self.assertEqual(results, list(task_args))
mock.reset_mock()
results = self.execute_run({bear})
mock.assert_called_once_with(*task_args)
self.assertEqual(results, list(task_args))
mock.reset_mock()
# Passing None for cache shall disable it too explicitly.
results = self.execute_run({bear}, None)
mock.assert_called_once_with(*task_args)
self.assertEqual(results, list(task_args))
开发者ID:Nosferatul,项目名称:coala,代码行数:27,代码来源:CoreTest.py
示例12: test_assert_called_once_with_function_spec
def test_assert_called_once_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_once_with(1, 2, 3)
mock.assert_called_once_with(a=1, b=2, c=3)
self.assertRaises(AssertionError, mock.assert_called_once_with, 1, b=3, c=2)
# Expected call doesn't match the spec's signature
with self.assertRaises(AssertionError) as cm:
mock.assert_called_once_with(e=8)
self.assertIsInstance(cm.exception.__cause__, TypeError)
# Mock called more than once => always fails
mock(4, 5, 6)
self.assertRaises(AssertionError, mock.assert_called_once_with, 1, 2, 3)
self.assertRaises(AssertionError, mock.assert_called_once_with, 4, 5, 6)
开发者ID:chayao2015,项目名称:kbengine,代码行数:18,代码来源:testmock.py
示例13: test_no_source
def test_no_source(self, commands, mocker):
mock = mocker.patch('qutebrowser.config.configcommands.editor.'
'ExternalEditor._start_editor', autospec=True)
commands.config_edit(no_source=True)
mock.assert_called_once_with(unittest.mock.ANY)
开发者ID:blyxxyz,项目名称:qutebrowser,代码行数:5,代码来源:test_configcommands.py
示例14: test_perform_system_call
def test_perform_system_call(self, mock):
cmd = 'universal'
perform_system_command(cmd)
mock.assert_called_once_with(cmd)
开发者ID:gitter-badger,项目名称:universal-1,代码行数:6,代码来源:test_util.py
示例15: MagicMock
oo.func1 = MagicMock()
print(oo.func1("POKEMON !!").split() + 33)
print(oo.func2())
# Il est possible d'avoir un historique de ce qui a été utilisé avec les mocks.
print(oo.func1.mock_calls)
# Il est possible de vérifier si un appel a bien été fait :
oo.func1.assert_called_with("POKEMON !!")
# oo.func1.assert_called_with("Mario !") Provoquera une erreur.
# Patch
# permet de modifier le comportement de ce qui existe pour les tests.
from unittest.mock import patch
"""
with patch.object(builtins, "open", mock_open(read_data="wololo") as mock:
#with patch('__main__.open', mock_open(read_data='wololo'), create=True) as mock:
with open('zefile') as h:
result = h.read()
mock.assert_called_once_with('zefile')
assert result == 'wololo'
""" ### Ne fonctionne pas
# Il est possible de patcher un bout de module :
@patch('os.listdir')
def ah(mock):
import os
print(os.listdir('.'))
# l'objet mock initial est aussi passé en param automatiquement
开发者ID:guillaume-havard,项目名称:Tests_py,代码行数:31,代码来源:testsunitaires.py
注:本文中的unittest.mock.assert_called_once_with函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论