本文整理汇总了Python中tests.base.mock.patch函数的典型用法代码示例。如果您正苦于以下问题:Python patch函数的具体用法?Python patch怎么用?Python patch使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了patch函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_cache_file
def test_cache_file(self):
"""
Test providing a cachefile.
"""
test_file = os.path.abspath(__file__)
with mock.patch('os.path.join', return_value=test_file):
with mock.patch('yaml.load'):
jenkins_jobs.builder.JobCache("dummy").data = None
示例2: test_save_on_exit
def test_save_on_exit(self):
"""
Test that the cache is saved on normal object deletion
"""
with mock.patch('jenkins_jobs.builder.JobCache.save') as save_mock:
with mock.patch('os.path.isfile', return_value=False):
jenkins_jobs.builder.JobCache("dummy")
save_mock.assert_called_with()
示例3: test_update_jobs_and_delete_old
def test_update_jobs_and_delete_old(self, builder_mock, delete_job_mock,
get_jobs_mock, is_job_mock):
"""
Test update behaviour with --delete-old option
Test update of jobs with the --delete-old option enabled, where only
some jobs result in has_changed() to limit the number of times
update_job is called, and have the get_jobs() method return additional
jobs not in the input yaml to test that the code in cmd will call
delete_job() after update_job() when '--delete-old' is set but only
for the extra jobs.
"""
# set up some test data
jobs = ['old_job001', 'old_job002']
extra_jobs = [{'name': name} for name in jobs]
builder_obj = builder.Builder('http://jenkins.example.com',
'doesnot', 'matter',
plugins_list={})
# get the instance created by mock and redirect some of the method
# mocks to call real methods on a the above test object.
b_inst = builder_mock.return_value
b_inst.plugins_list = builder_obj.plugins_list
b_inst.update_jobs.side_effect = builder_obj.update_jobs
b_inst.delete_old_managed.side_effect = builder_obj.delete_old_managed
def _get_jobs():
return builder_obj.parser.jobs + extra_jobs
get_jobs_mock.side_effect = _get_jobs
# override cache to ensure Jenkins.update_job called a limited number
# of times
self.cache_mock.return_value.has_changed.side_effect = (
[True] * 2 + [False] * 2)
path = os.path.join(self.fixtures_path, 'cmd-002.yaml')
args = ['--conf', self.default_config_file, 'update', '--delete-old',
path]
with mock.patch('jenkins_jobs.builder.Jenkins.update_job') as update:
with mock.patch('jenkins_jobs.builder.Jenkins.is_managed',
return_value=True):
self.execute_jenkins_jobs_with_args(args)
self.assertEquals(2, update.call_count,
"Expected Jenkins.update_job to be called '%d' "
"times, got '%d' calls instead.\n"
"Called with: %s" % (2, update.call_count,
update.mock_calls))
calls = [mock.call(name) for name in jobs]
self.assertEqual(2, delete_job_mock.call_count,
"Expected Jenkins.delete_job to be called '%d' "
"times got '%d' calls instead.\n"
"Called with: %s" % (2, delete_job_mock.call_count,
delete_job_mock.mock_calls))
delete_job_mock.assert_has_calls(calls, any_order=True)
示例4: test_stream_input_output_no_encoding_exceed_recursion
def test_stream_input_output_no_encoding_exceed_recursion(self):
"""
Test that we don't have issues processing large number of jobs and
outputting the result if the encoding is not set.
"""
console_out = io.BytesIO()
input_file = os.path.join(self.fixtures_path,
'large-number-of-jobs-001.yaml')
with io.open(input_file, 'r') as f:
with mock.patch('sys.stdout', console_out):
console_out.encoding = None
with mock.patch('sys.stdin', f):
args = ['test']
self.execute_jenkins_jobs_with_args(args)
示例5: test_stream_output_ascii_encoding_invalid_char
def test_stream_output_ascii_encoding_invalid_char(self):
"""
Run test mode simulating using pipes for input and output using
ascii encoding for output with include containing a character
that cannot be converted.
"""
console_out = io.BytesIO()
console_out.encoding = 'ascii'
input_file = os.path.join(self.fixtures_path, 'unicode001.yaml')
with io.open(input_file, 'r', encoding='utf-8') as f:
with mock.patch('sys.stdout', console_out):
with mock.patch('sys.stdin', f):
e = self.assertRaises(UnicodeError, cmd.main, ['test'])
self.assertIn("'ascii' codec can't encode character", str(e))
示例6: test_skip_plugin_retrieval_if_no_config_provided
def test_skip_plugin_retrieval_if_no_config_provided(self, get_plugins_info_mock):
"""
Verify that retrieval of information from Jenkins instance about its
plugins will be skipped when run if no config file provided.
"""
with mock.patch("sys.stdout"):
cmd.main(["test", os.path.join(self.fixtures_path, "cmd-001.yaml")])
self.assertFalse(get_plugins_info_mock.called)
示例7: test_stream_input_output_utf8_encoding
def test_stream_input_output_utf8_encoding(self):
"""
Run test mode simulating using pipes for input and output using
utf-8 encoding
"""
console_out = io.BytesIO()
input_file = os.path.join(self.fixtures_path, 'cmd-001.yaml')
with io.open(input_file, 'r') as f:
with mock.patch('sys.stdout', console_out):
with mock.patch('sys.stdin', f):
cmd.main(['test'])
xml_content = io.open(os.path.join(self.fixtures_path, 'cmd-001.xml'),
'r', encoding='utf-8').read()
value = console_out.getvalue().decode('utf-8')
self.assertEqual(value, xml_content)
示例8: test_stream_output_ascii_encoding_invalid_char
def test_stream_output_ascii_encoding_invalid_char(self):
"""
Run test mode simulating using pipes for input and output using
ascii encoding for output with include containing a character
that cannot be converted.
"""
console_out = io.BytesIO()
console_out.encoding = 'ascii'
input_file = os.path.join(self.fixtures_path, 'unicode001.yaml')
with io.open(input_file, 'r', encoding='utf-8') as f:
with mock.patch('sys.stdout', console_out):
with mock.patch('sys.stdin', f):
args = ['--conf', self.default_config_file, 'test']
jenkins_jobs = entry.JenkinsJobs(args)
e = self.assertRaises(UnicodeError, jenkins_jobs.execute)
self.assertIn("'ascii' codec can't encode character", str(e))
示例9: test_delete_all_accept
def test_delete_all_accept(self, delete_job_mock):
"""
Test handling the deletion of a single Jenkins job.
"""
args = ['--conf', self.default_config_file, 'delete-all']
with mock.patch('jenkins_jobs.utils.input', return_value="y"):
self.execute_jenkins_jobs_with_args(args)
示例10: test_delete_all_abort
def test_delete_all_abort(self, delete_job_mock):
"""
Test handling the deletion of a single Jenkins job.
"""
args = ['--conf', self.default_config_file, 'delete-all']
with mock.patch('jenkins_jobs.utils.input', return_value="n"):
self.assertRaises(SystemExit,
self.execute_jenkins_jobs_with_args, args)
示例11: test_stream_input_output_ascii_encoding
def test_stream_input_output_ascii_encoding(self):
"""
Run test mode simulating using pipes for input and output using
ascii encoding with unicode input
"""
console_out = io.BytesIO()
console_out.encoding = 'ascii'
input_file = os.path.join(self.fixtures_path, 'cmd-001.yaml')
with io.open(input_file, 'r') as f:
with mock.patch('sys.stdout', console_out):
with mock.patch('sys.stdin', f):
args = ['--conf', self.default_config_file, 'test']
self.execute_jenkins_jobs_with_args(args)
xml_content = io.open(os.path.join(self.fixtures_path, 'cmd-001.xml'),
'r', encoding='utf-8').read()
value = console_out.getvalue().decode('ascii')
self.assertEqual(value, xml_content)
示例12: test_console_output
def test_console_output(self):
"""
Run test mode and verify that resulting XML gets sent to the console.
"""
console_out = io.BytesIO()
with mock.patch("sys.stdout", console_out):
cmd.main(["test", os.path.join(self.fixtures_path, "cmd-001.yaml")])
xml_content = codecs.open(os.path.join(self.fixtures_path, "cmd-001.xml"), "r", "utf-8").read()
self.assertEqual(console_out.getvalue().decode("utf-8"), xml_content)
示例13: test_skip_plugin_retrieval_if_no_config_provided
def test_skip_plugin_retrieval_if_no_config_provided(
self, get_plugins_mock):
"""
Verify that retrieval of information from Jenkins instance about its
plugins will be skipped when run if no config file provided.
"""
with mock.patch('sys.stdout', new_callable=io.BytesIO):
args = ['--conf', self.default_config_file, 'test',
os.path.join(self.fixtures_path, 'cmd-001.yaml')]
entry.JenkinsJobs(args)
self.assertFalse(get_plugins_mock.called)
示例14: test_valid_job
def test_valid_job(self):
"""
Run test mode and pass a valid job name
"""
args = ['--conf', self.default_config_file, 'test',
os.path.join(self.fixtures_path,
'cmd-001.yaml'),
'foo-job']
console_out = io.BytesIO()
with mock.patch('sys.stdout', console_out):
self.execute_jenkins_jobs_with_args(args)
示例15: test_console_output
def test_console_output(self):
"""
Run test mode and verify that resulting XML gets sent to the console.
"""
console_out = io.BytesIO()
with mock.patch('sys.stdout', console_out):
cmd.main(['test', os.path.join(self.fixtures_path,
'cmd-001.yaml')])
xml_content = io.open(os.path.join(self.fixtures_path, 'cmd-001.xml'),
'r', encoding='utf-8').read()
self.assertEqual(console_out.getvalue().decode('utf-8'), xml_content)