本文整理汇总了Python中mock.Mock.reset_mock方法的典型用法代码示例。如果您正苦于以下问题:Python Mock.reset_mock方法的具体用法?Python Mock.reset_mock怎么用?Python Mock.reset_mock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mock.Mock
的用法示例。
在下文中一共展示了Mock.reset_mock方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_cached_parent
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import reset_mock [as 别名]
def test_cached_parent():
class HasParent(XBlock):
pass
runtime = Mock()
block = HasParent(runtime, DictFieldData({}), Mock())
# block has no parent yet, and we don't need to call the runtime to find
# that out.
assert_equals(block.get_parent(), None)
assert not runtime.get_block.called
# Set a parent id for the block. Get the parent. Now we have one, and we
# used runtime.get_block to get it.
block.parent = "some_parent_id"
parent = block.get_parent()
assert_not_equals(parent, None)
assert runtime.get_block.called_with("some_parent_id")
# Get the parent again. It will be the same parent, and we didn't call the
# runtime.
runtime.reset_mock()
parent2 = block.get_parent()
assert parent2 is parent
assert not runtime.get_block.called
示例2: test01_send_mailing
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import reset_mock [as 别名]
def test01_send_mailing(self):
# Mock the SMTP connection
mock_smtp = Mock()
def mock_get_connection(*args, **kargs):
return mock_smtp
send.get_connection = mock_get_connection
# Capture sent emails
self.msg_list = None
def send_messages(msg_list):
self.msg_list = msg_list
mock_smtp.send_messages.side_effect = send_messages
send.send_mailing()
mock_smtp.open.assert_called_once()
mock_smtp.close.assert_called_once()
mock_smtp.reset_mock()
self.assertEqual(len(self.msg_list), 1)
msg_formatted = str(self.msg_list[0].message())
self.assertTrue('From: Card Stories <[email protected]>' in msg_formatted)
self.assertTrue('Content-Type: multipart/mixed' in msg_formatted)
self.assertTrue('Content-Type: text/plain' in msg_formatted)
self.assertTrue('Content-Type: text/html' in msg_formatted)
self.assertTrue('Content-Type: image/jpeg' in msg_formatted)
示例3: testCall
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import reset_mock [as 别名]
def testCall(self):
mock = Mock()
self.assertTrue(isinstance(mock.return_value, Mock),
"Default return_value should be a Mock")
result = mock()
self.assertEqual(mock(), result,
"different result from consecutive calls")
mock.reset_mock()
ret_val = mock(sentinel.Arg)
self.assertTrue(mock.called, "called not set")
self.assertEqual(mock.call_count, 1, "call_count incoreect")
self.assertEqual(mock.call_args, ((sentinel.Arg,), {}),
"call_args not set")
self.assertEqual(mock.call_args_list, [((sentinel.Arg,), {})],
"call_args_list not initialised correctly")
mock.return_value = sentinel.ReturnValue
ret_val = mock(sentinel.Arg, key=sentinel.KeyArg)
self.assertEqual(ret_val, sentinel.ReturnValue,
"incorrect return value")
self.assertEqual(mock.call_count, 2, "call_count incorrect")
self.assertEqual(mock.call_args,
((sentinel.Arg,), {'key': sentinel.KeyArg}),
"call_args not set")
self.assertEqual(mock.call_args_list, [
((sentinel.Arg,), {}),
((sentinel.Arg,), {'key': sentinel.KeyArg})
],
"call_args_list not set")
示例4: test_new_mode_callback
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import reset_mock [as 别名]
def test_new_mode_callback(self):
"""Create a Controller object, call on_new_mode_online method and
check that the callback fires when set_composite_mode is called
"""
self.setup_server()
self.setup_controller()
self.log.info("setting callback")
test_cb = Mock(side_effect=self.quit_mainloop)
self.controller.on_new_mode_online(test_cb)
self.log.info("starting test-sources")
self.setup_video_sources(count=2)
self.log.info("waiting for initial callback with default-mode"
"COMPOSITE_DUAL_EQUAL")
self.run_mainloop(timeout=5)
test_cb.assert_called_once_with(Controller.COMPOSITE_DUAL_EQUAL)
test_cb.reset_mock()
self.log.info("setting new composite mode COMPOSITE_NONE")
assert self.controller.set_composite_mode(Controller.COMPOSITE_NONE)
self.run_mainloop(timeout=5)
self.log.info("waiting for callback with new mode COMPOSITE_NONE")
test_cb.assert_called_once_with(Controller.COMPOSITE_NONE)
示例5: test_pool_restart_relaod_modules
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import reset_mock [as 别名]
def test_pool_restart_relaod_modules(self):
consumer = Consumer()
consumer.controller = _WC(app=current_app)
consumer.controller.pool.restart = Mock()
panel = self.create_panel(consumer=consumer)
panel.app = self.app
_import = panel.app.loader.import_from_cwd = Mock()
_reload = Mock()
with patch.dict(sys.modules, {'foo': None}):
panel.handle('pool_restart', {'modules': ['foo'],
'reload': False,
'reloader': _reload})
self.assertTrue(consumer.controller.pool.restart.called)
self.assertFalse(_reload.called)
self.assertFalse(_import.called)
_import.reset_mock()
_reload.reset_mock()
consumer.controller.pool.restart.reset_mock()
panel.handle('pool_restart', {'modules': ['foo'],
'reload': True,
'reloader': _reload})
self.assertTrue(consumer.controller.pool.restart.called)
self.assertTrue(_reload.called)
self.assertFalse(_import.called)
示例6: test_canInstall
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import reset_mock [as 别名]
def test_canInstall(self, mock_canInstall):
posix = self.get_obj()
entry = lxml.etree.Element("Path", name="test", type="file")
# first, test superclass canInstall failure
mock_canInstall.return_value = False
self.assertFalse(posix.canInstall(entry))
mock_canInstall.assert_called_with(posix, entry)
# next, test fully_specified failure
posix.logger.error.reset_mock()
mock_canInstall.reset_mock()
mock_canInstall.return_value = True
mock_fully_spec = Mock()
mock_fully_spec.return_value = False
posix._handlers[entry.get("type")].fully_specified = \
mock_fully_spec
self.assertFalse(posix.canInstall(entry))
mock_canInstall.assert_called_with(posix, entry)
mock_fully_spec.assert_called_with(entry)
self.assertTrue(posix.logger.error.called)
# finally, test success
posix.logger.error.reset_mock()
mock_canInstall.reset_mock()
mock_fully_spec.reset_mock()
mock_fully_spec.return_value = True
self.assertTrue(posix.canInstall(entry))
mock_canInstall.assert_called_with(posix, entry)
mock_fully_spec.assert_called_with(entry)
self.assertFalse(posix.logger.error.called)
示例7: test_machine
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import reset_mock [as 别名]
def test_machine(monkeypatch):
# Make sure requesting the machine works, is read-only and produces a
# deprecation warning.
from network_tester import experiment
mock_system_info = Mock()
mock_machine = Mock()
mock_build_machine = Mock(return_value=mock_machine)
mock_mc = Mock()
monkeypatch.setattr(experiment, "build_machine", mock_build_machine)
e = Experiment(mock_mc)
e.system_info = mock_system_info
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
# Machine object should be returned built from the system info.
assert not mock_build_machine.called
assert e.machine is mock_machine
mock_build_machine.called_once_with(mock_system_info)
# Should be flagged as deprecated
assert len(w) == 1
assert issubclass(w[0].category, DeprecationWarning)
# Machine should be regenerated every time (i.e. is explicitly read-only)
mock_build_machine.reset_mock()
assert e.machine is mock_machine
mock_build_machine.called_once_with(mock_system_info)
示例8: test_validate_list_calls_validate_for_each_schema_data_combo
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import reset_mock [as 别名]
def test_validate_list_calls_validate_for_each_schema_data_combo(self):
def mock_return(schema, data, path):
if not isinstance(data, schema):
raise Invalid("")
return data
mocked = Mock(side_effect=mock_return)
with patch.object(Schema, "_validate", mocked):
Schema._validate_list([int, unicode], ["a", "b"], [])
mocked.assert_has_calls([
call(int, "a", [0]),
call(unicode, "a", [0]),
call(int, "b", [1]),
call(unicode, "b", [1])
])
mocked.reset_mock()
with self.assertRaises(InvalidGroup):
Schema._validate_list([int, unicode], ["a", 1, None], [])
mocked.assert_has_calls([
call(int, "a", [0]),
call(unicode, "a", [0]),
call(int, 1, [1]),
call(int, None, [2]),
call(unicode, None, [2])
])
mocked.reset_mock()
Schema._validate_list([int, unicode], [1, 2], [])
mocked.assert_has_calls([
call(int, 1, [0]),
call(int, 2, [1]),
])
示例9: test_when_base_returns_function_value_when_passed_a_function
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import reset_mock [as 别名]
def test_when_base_returns_function_value_when_passed_a_function(self):
mocked = Mock(return_value=8)
self.assertEqual(_when_base(None, mocked), 8)
mocked.assert_called_once_with(None)
mocked.reset_mock()
self.assertEqual(_when_base("abc", mocked), 8)
mocked.assert_called_once_with("abc")
示例10: test_pool_restart_relaod_modules
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import reset_mock [as 别名]
def test_pool_restart_relaod_modules(self):
consumer = Consumer()
consumer.pool.restart = Mock()
panel = self.create_panel(consumer=consumer)
panel.app = self.app
_import = panel.app.loader.import_from_cwd = Mock()
_reload = Mock()
with patch.dict(sys.modules, {"foo": None}):
panel.handle("pool_restart", {"imports": ["foo"],
"reload_imports": False,
"reload": _reload})
self.assertTrue(consumer.pool.restart.called)
self.assertFalse(_reload.called)
self.assertFalse(_import.called)
_import.reset_mock()
_reload.reset_mock()
consumer.pool.restart.reset_mock()
panel.handle("pool_restart", {"imports": ["foo"],
"reload_imports": True,
"reload": _reload})
self.assertTrue(consumer.pool.restart.called)
self.assertTrue(_reload.called)
self.assertFalse(_import.called)
示例11: test_validate_dict_appends_the_key_to_the_current_path
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import reset_mock [as 别名]
def test_validate_dict_appends_the_key_to_the_current_path(self):
# TODO: Should we be checking this, or just the output?
# TODO: If we do decide to keep this, it should be combined with above
mocked = Mock(return_value={})
with patch.object(Schema, "_validate", mocked):
schema = {"a": int, "b": int, "c": int}
data = {"a": 1, "b": 2, "c": 3}
Schema._validate_dict(schema, data, [])
mocked.assert_has_calls([
call(int, 1, ["a"]),
call(int, 2, ["b"]),
call(int, 3, ["c"])
], any_order=True)
mocked.reset_mock()
Schema._validate_dict(schema, data, ["f"])
mocked.assert_has_calls([
call(int, 1, ["f", "a"]),
call(int, 2, ["f", "b"]),
call(int, 3, ["f", "c"])
], any_order=True)
mocked.reset_mock()
Schema._validate_dict(schema, data, ["f", "g", 1])
mocked.assert_has_calls([
call(int, 1, ["f", "g", 1, "a"]),
call(int, 2, ["f", "g", 1, "b"]),
call(int, 3, ["f", "g", 1, "c"])
], any_order=True)
示例12: test_passphrase
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import reset_mock [as 别名]
def test_passphrase(self, mock_get_passphrases):
pkc = self.get_obj()
cfp = Mock()
cfp.has_section.return_value = False
cfp.has_option.return_value = False
Bcfg2.Server.Plugins.Cfg.CfgPrivateKeyCreator.SETUP = Mock()
Bcfg2.Server.Plugins.Cfg.CfgPrivateKeyCreator.SETUP.cfp = cfp
self.assertIsNone(pkc.passphrase)
cfp.has_section.assert_called_with("sshkeys")
cfp.reset_mock()
cfp.has_section.return_value = True
self.assertIsNone(pkc.passphrase)
cfp.has_section.assert_called_with("sshkeys")
cfp.has_option.assert_called_with("sshkeys", "passphrase")
cfp.reset_mock()
cfp.get.return_value = "test"
mock_get_passphrases.return_value = dict(test="foo", test2="bar")
cfp.has_option.return_value = True
self.assertEqual(pkc.passphrase, "foo")
cfp.has_section.assert_called_with("sshkeys")
cfp.has_option.assert_called_with("sshkeys", "passphrase")
cfp.get.assert_called_with("sshkeys", "passphrase")
mock_get_passphrases.assert_called_with(Bcfg2.Server.Plugins.Cfg.CfgPrivateKeyCreator.SETUP)
示例13: test_diff
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import reset_mock [as 别名]
def test_diff(self, mock_time):
content1 = "line1\nline2"
content2 = "line3"
self.now = 1345640723
def time_rv():
self.now += 1
return self.now
mock_time.side_effect = time_rv
rv = ["line1", "line2", "line3"]
func = Mock()
func.return_value = rv
self.assertItemsEqual(self.ptool._diff(content1, content2, func), rv)
func.assert_called_with(["line1", "line2"], ["line3"])
func.reset_mock()
mock_time.reset_mock()
def time_rv():
self.now += 5
return self.now
mock_time.side_effect = time_rv
def slow_diff(content1, content2):
for i in range(1, 10):
yield "line%s" % i
func.side_effect = slow_diff
self.assertFalse(self.ptool._diff(content1, content2, func), rv)
func.assert_called_with(["line1", "line2"], ["line3"])
示例14: test_context_manager
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import reset_mock [as 别名]
def test_context_manager(self):
m = Mock()
class CM1(object):
def __enter__(self):
m.cm1.enter()
return self
def __exit__(self, type, obj, tb):
m.cm1.exit(type, obj)
return True
class CM2Context(object): pass
class CM2(object):
def __enter__(self):
m.cm2.enter()
return CM2Context()
def __exit__(self, type, obj, tb):
m.cm2.exit(type, obj)
@requires(CM1)
def func1(obj):
m.func1(type(obj))
@requires(CM1, CM2, CM2Context)
def func2(obj1, obj2, obj3):
m.func2(type(obj1),
type(obj2),
type(obj3))
runner = Runner(
CM1,
CM2,
func1,
func2,
)
runner()
compare([
call.cm1.enter(),
call.cm2.enter(),
call.func1(CM1),
call.func2(CM1, CM2, CM2Context),
call.cm2.exit(None, None),
call.cm1.exit(None, None)
], m.mock_calls)
# now check with an exception
m.reset_mock()
m.func2.side_effect = e = Exception()
runner()
compare([
call.cm1.enter(),
call.cm2.enter(),
call.func1(CM1),
call.func2(CM1, CM2, CM2Context),
call.cm2.exit(Exception, e),
call.cm1.exit(Exception, e)
], m.mock_calls)
示例15: test15_poll_notification
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import reset_mock [as 别名]
def test15_poll_notification(self):
args = { 'type': ['tabs'],
'action': ['poll'],
'player_id': [13],
'game_id': [20],
'modified': [10000000000000000] }
# Fake poll controlled from the test
poll = defer.Deferred()
mock_poll_tabs = Mock()
mock_poll_tabs.return_value = poll
orig_service_poll_tabs = self.service.poll_tabs
self.service.poll_tabs = mock_poll_tabs
mock_listener = Mock()
self.service.listen().addCallback(mock_listener)
self.service.poll(args)
mock_listener.assert_called_once_with({'player_id': 13, 'type': 'poll_start'})
mock_listener.reset_mock()
self.service.listen().addCallback(mock_listener)
poll.callback(args)
mock_listener.assert_called_once_with({'player_id': 13, 'type': 'poll_end'})
self.service.poll_tabs = orig_service_poll_tabs