本文整理汇总了Python中tests.mock.patch方法的典型用法代码示例。如果您正苦于以下问题:Python mock.patch方法的具体用法?Python mock.patch怎么用?Python mock.patch使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tests.mock
的用法示例。
在下文中一共展示了mock.patch方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setUp
# 需要导入模块: from tests import mock [as 别名]
# 或者: from tests.mock import patch [as 别名]
def setUp(self):
self.player = Player()
self._get_audio_driver_patcher = mock.patch('pyglet.media.player.get_audio_driver')
self.mock_get_audio_driver = self._get_audio_driver_patcher.start()
self.mock_audio_driver = self.mock_get_audio_driver.return_value
self.mock_audio_driver_player = self.mock_audio_driver.create_audio_player.return_value
self._clock_patcher = mock.patch('pyglet.clock')
self.mock_clock = self._clock_patcher.start()
self._texture_patcher = mock.patch('pyglet.image.Texture.create')
self.mock_texture_create = self._texture_patcher.start()
self.mock_texture = self.mock_texture_create.return_value
# Need to do this as side_effect instead of return_value, or reset_mock will recurse
self.mock_texture.get_transform.side_effect = lambda flip_y: self.mock_texture
示例2: setUp
# 需要导入模块: from tests import mock [as 别名]
# 或者: from tests.mock import patch [as 别名]
def setUp(self):
self.patch_host_config = mock.patch('freight_forwarder.container.host_config')
self.patch_logger = mock.patch('freight_forwarder.container_ship.logger')
self.patch_urlparse = mock.patch('freight_forwarder.container_ship.urlparse')
self.patch_utils = mock.patch('freight_forwarder.container_ship.utils')
self.patch_urllib = mock.patch('freight_forwarder.container_ship.urllib3')
self.patch_docker_client = mock.patch('freight_forwarder.container_ship.docker.Client')
self.patch_container = mock.patch('freight_forwarder.container_ship.Container')
self.patch_image = mock.patch('freight_forwarder.container_ship.Image')
self.mock_host_config = self.patch_host_config.start()
self.mock_logger = self.patch_logger.start()
self.mock_urlparse = self.patch_urlparse.start()
self.mock_utils = self.patch_utils.start()
self.mock_urllib = self.patch_urllib.start()
self.mock_docker_client = self.patch_docker_client.start()
self.mock_container = self.patch_container.start()
self.mock_image = self.patch_image.start()
self.mock_service = mock.Mock(spec=Service)
self.injector = InjectorFactory()
示例3: _mock_console_input
# 需要导入模块: from tests import mock [as 别名]
# 或者: from tests.mock import patch [as 别名]
def _mock_console_input(self, isatty=True):
with patch('streamlink_cli.console.sys.stdin.isatty', return_value=isatty):
mock_console = MagicMock()
mock_console.ask.return_value = "username"
mock_console.askpass.return_value = "password"
yield ConsoleUserInputRequester(mock_console)
示例4: test_load_fail
# 需要导入模块: from tests import mock [as 别名]
# 或者: from tests.mock import patch [as 别名]
def test_load_fail(self, exists_mock):
if is_py2:
patch('__builtin__.open', side_effect=IOError)
else:
patch('streamlink.cache.open', side_effect=IOError)
self.cache._load()
self.assertEqual({}, self.cache._cache)
示例5: test_json_encoding
# 需要导入模块: from tests import mock [as 别名]
# 或者: from tests.mock import patch [as 别名]
def test_json_encoding(self):
json_str = u"{\"test\": \"Α and Ω\"}"
# encode the json string with each encoding and assert that the correct one is detected
for encoding in ["UTF-32BE", "UTF-32LE", "UTF-16BE", "UTF-16LE", "UTF-8"]:
with patch('requests.Response.content', new_callable=PropertyMock) as mock_content:
mock_content.return_value = json_str.encode(encoding)
res = requests.Response()
self.assertEqual(HTTPSession.json(res), {u"test": u"\u0391 and \u03a9"})
示例6: test_json_encoding_override
# 需要导入模块: from tests import mock [as 别名]
# 或者: from tests.mock import patch [as 别名]
def test_json_encoding_override(self):
json_text = u"{\"test\": \"Α and Ω\"}".encode("cp949")
with patch('requests.Response.content', new_callable=PropertyMock) as mock_content:
mock_content.return_value = json_text
res = requests.Response()
res.encoding = "cp949"
self.assertEqual(HTTPSession.json(res), {u"test": u"\u0391 and \u03a9"})
示例7: setUp
# 需要导入模块: from tests import mock [as 别名]
# 或者: from tests.mock import patch [as 别名]
def setUp(self):
self.session_patch = mock.patch('ibm_boto3.Session', autospec=True)
self.Session = self.session_patch.start()
示例8: _patch_directory
# 需要导入模块: from tests import mock [as 别名]
# 或者: from tests.mock import patch [as 别名]
def _patch_directory(self, target):
directory = tempfile.mkdtemp()
self._temporary_directories.append(directory)
patcher = mock.patch(target, directory)
self._patchers.append(patcher)
patcher.start()
return directory
示例9: test_player_silent_audio_driver
# 需要导入模块: from tests import mock [as 别名]
# 或者: from tests.mock import patch [as 别名]
def test_player_silent_audio_driver(player):
with mock.patch('pyglet.media.player.get_audio_driver') as get_audio_driver_mock:
get_audio_driver_mock.return_value = None
source = SilentTestSource(.1)
player.queue(source)
player.play()
player.wait_for_all_events(
1.,
'on_eos',
'on_player_eos')
示例10: test_caching
# 需要导入模块: from tests import mock [as 别名]
# 或者: from tests.mock import patch [as 别名]
def test_caching(self):
"""
Make sure cached values are held.
Return values aren't accurate for blessed, but are sufficient for this test
"""
handw = 'enlighten._terminal._Terminal._height_and_width'
with mock.patch(handw, return_value=(1, 2)):
self.assertEqual(self.terminal._height_and_width(), (1, 2))
with mock.patch(handw, return_value=(5, 6)):
self.assertEqual(self.terminal._height_and_width(), (1, 2))
self.terminal.clear_cache()
self.assertEqual(self.terminal._height_and_width(), (5, 6))
示例11: setUp
# 需要导入模块: from tests import mock [as 别名]
# 或者: from tests.mock import patch [as 别名]
def setUp(self):
self.patch_requests = mock.patch('freight_forwarder.registry.registry_base.requests', autospec=True)
self.patch_requests.start()
self.test_registry = RegistryV1Factory(address="https://registrybasetest.docker.com")
示例12: setUp
# 需要导入模块: from tests import mock [as 别名]
# 或者: from tests.mock import patch [as 别名]
def setUp(self):
# Patch sys.stdout for stream
self.patch_utils_stdout = mock.patch("freight_forwarder.utils.utils.stdout", name="utils_sys")
self.mock_utils_stdout = self.patch_utils_stdout.start()
# Patch normalized_values
self.patch_utils_normalize_keys = mock.patch("freight_forwarder.utils.utils.normalize_keys",
name="utils_normalize_keys")
self.mock_utils_normalize_keys = self.patch_utils_normalize_keys.start()
# Patch Display Error
self.patch_display_error = mock.patch("freight_forwarder.utils.utils._display_error",
name="mock_display_error")
self.mock_display_error = self.patch_display_error.start()
# Patch Display Progress
self.patch_display_progress = mock.patch("freight_forwarder.utils.utils._display_progress",
name="mock_display_error")
self.mock_display_progress = self.patch_display_progress.start()
# Patch Display Status
self.patch_display_status = mock.patch("freight_forwarder.utils.utils._display_status",
name="mock_display_status")
self.mock_display_status = self.patch_display_status.start()
# Patch Display Stream
self.patch_display_stream = mock.patch("freight_forwarder.utils.utils._display_stream",
name="mock_display_stream")
self.mock_display_stream = self.patch_display_stream.start()
# Mock Response Object
self.mock_object_request = mock.MagicMock(spec=requests.PreparedRequest, url="https://docker-host.test.io:2376")
self.mock_object_response = mock.MagicMock(spec=requests.Response, request=self.mock_object_request)
self.mock_object_response.status_code = 404
示例13: test_assemble_fleet_with_yaml_variables_in_hosts
# 需要导入模块: from tests import mock [as 别名]
# 或者: from tests.mock import patch [as 别名]
def test_assemble_fleet_with_yaml_variables_in_hosts(self,
mock_logger,
mock_container_ship,
mock_validate_commercial_invoice,
mock_write_state_file,
mock_service_deployment_validation,
mock_wait_for_dispatch,
mock_dispatch,
mock_complete_distribution
):
"""
Verify that feel is built correctly based on the provided configuration data
:param mock_commercial_invoice:
:return:
"""
mock_registries = mock.patch.object(CommercialInvoice, '_create_registries', return_value={
'tune_dev': RegistryV1Factory(),
u'docker_hub': RegistryV1Factory()
})
mock_registries.start()
mock_transport_service = mock.MagicMock(return_value='tomcat-test')
self.freight_forwarder._bill_of_lading = {'failures': {}, 'successful': {}}
commercial_invoice = self.freight_forwarder.commercial_invoice(
action='deploy',
data_center='local',
environment='staging',
transport_service=mock_transport_service()
)
mock_validate_commercial_invoice.return_value = commercial_invoice
test_deployment = self.freight_forwarder.deploy_containers(
commercial_invoice
)
# Ensure output of deployment is True based on no errors during the deployment process
self.assertTrue(test_deployment)
# Validate output of logging to ensure the correct host is output to the user
calls = [call('Running deploy.'),
call('dispatching service: ffbug-example-tomcat-test on host: https://192.168.99.100:2376.')]
mock_logger.info.assert_has_calls(calls)