• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Python mock.patch函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中tests.base.mock.patch函数的典型用法代码示例。如果您正苦于以下问题:Python patch函数的具体用法?Python patch怎么用?Python patch使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了patch函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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
开发者ID:akulakhan,项目名称:jenkins-job-builder,代码行数:8,代码来源:test_cachestorage.py


示例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()
开发者ID:akulakhan,项目名称:jenkins-job-builder,代码行数:9,代码来源:test_cachestorage.py


示例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)
开发者ID:turbonaitis,项目名称:jenkins-job-builder,代码行数:57,代码来源:test_update.py


示例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)
开发者ID:OSSystems,项目名称:jenkins-job-builder,代码行数:15,代码来源:test_test.py


示例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))
开发者ID:10173677,项目名称:jenkins-job-builder,代码行数:15,代码来源:test_test.py


示例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)
开发者ID:widgetpl,项目名称:jenkins-job-builder,代码行数:8,代码来源:test_test.py


示例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)
开发者ID:10173677,项目名称:jenkins-job-builder,代码行数:17,代码来源:test_test.py


示例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))
开发者ID:OSSystems,项目名称:jenkins-job-builder,代码行数:17,代码来源:test_test.py


示例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)
开发者ID:OSSystems,项目名称:jenkins-job-builder,代码行数:8,代码来源:test_delete_all.py


示例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)
开发者ID:OSSystems,项目名称:jenkins-job-builder,代码行数:9,代码来源:test_delete_all.py


示例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)
开发者ID:OSSystems,项目名称:jenkins-job-builder,代码行数:19,代码来源:test_test.py


示例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)
开发者ID:widgetpl,项目名称:jenkins-job-builder,代码行数:10,代码来源:test_test.py


示例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)
开发者ID:OSSystems,项目名称:jenkins-job-builder,代码行数:11,代码来源:test_test.py


示例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)
开发者ID:OSSystems,项目名称:jenkins-job-builder,代码行数:11,代码来源:test_test.py


示例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)
开发者ID:10173677,项目名称:jenkins-job-builder,代码行数:12,代码来源:test_test.py


示例16: test_skip_plugin_retrieval_if_disabled

 def test_skip_plugin_retrieval_if_disabled(self, get_plugins_info_mock):
     """
     Verify that retrieval of information from Jenkins instance about its
     plugins will be skipped when run if a config file provided and disables
     querying through a config option.
     """
     with mock.patch('sys.stdout', new_callable=io.BytesIO):
         cmd.main(['--conf',
                   os.path.join(self.fixtures_path,
                                'disable-query-plugins.conf'),
                   'test',
                   os.path.join(self.fixtures_path, 'cmd-001.yaml')])
     self.assertFalse(get_plugins_info_mock.called)
开发者ID:10173677,项目名称:jenkins-job-builder,代码行数:13,代码来源:test_test.py


示例17: 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):
            args = ['--conf', self.default_config_file, 'test',
                    os.path.join(self.fixtures_path, 'cmd-001.yaml')]
            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()
        self.assertEqual(console_out.getvalue().decode('utf-8'), xml_content)
开发者ID:OSSystems,项目名称:jenkins-job-builder,代码行数:13,代码来源:test_test.py


示例18: setUp

    def setUp(self):
        super(CmdTestsBase, self).setUp()

        # Testing the cmd module can sometimes result in the JobCache class
        # attempting to create the cache directory multiple times as the tests
        # are run in parallel.  Stub out the JobCache to ensure that each
        # test can safely create the cache directory without risk of
        # interference.
        cache_patch = mock.patch("jenkins_jobs.builder.JobCache", autospec=True)
        self.cache_mock = cache_patch.start()
        self.addCleanup(cache_patch.stop)

        self.default_config_file = os.path.join(self.fixtures_path, "empty_builder.ini")
开发者ID:asdwsda,项目名称:jenkins-job-builder,代码行数:13,代码来源:test_cmd.py


示例19: test_list

    def test_list(self):
        path = os.path.join(self.fixtures_path, 'cmd-002.yaml')

        console_out = io.BytesIO()
        with mock.patch('sys.stdout', console_out):
            self.execute_jenkins_jobs_with_args(
                ['--conf',
                 self.default_config_file,
                 'list',
                 '-p',
                 path] + self.globs)

        self.assertEqual(console_out.getvalue().decode('utf-8').rstrip(),
                         ('\n'.join(self.found)))
开发者ID:OSSystems,项目名称:jenkins-job-builder,代码行数:14,代码来源:test_list.py


示例20: setUp

    def setUp(self):
        super(CmdTestsBase, self).setUp()

        # Testing the cmd module can sometimes result in the CacheStorage class
        # attempting to create the cache directory multiple times as the tests
        # are run in parallel.  Stub out the CacheStorage to ensure that each
        # test can safely create the cache directory without risk of
        # interference.
        self.cache_patch = mock.patch('jenkins_jobs.builder.CacheStorage',
                                      autospec=True)
        self.cache_patch.start()

        self.config = configparser.ConfigParser()
        self.config.readfp(StringIO(cmd.DEFAULT_CONF))
开发者ID:CoRfr,项目名称:jenkins-job-builder,代码行数:14,代码来源:test_cmd.py



注:本文中的tests.base.mock.patch函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python baseclass.ParserTest类代码示例发布时间:2022-05-27
下一篇:
Python fake.word函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap