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

Python mock.assert_called_with函数代码示例

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

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



在下文中一共展示了assert_called_with函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: test_success

def test_success(cli_paths):
    string_io = io.StringIO()
    pytest.helpers.create_cli_project(cli_paths.test_directory_path)
    material_path = pytest.helpers.create_cli_material(
        cli_paths.test_directory_path, "test_material"
    )
    script = supriya.cli.ManageMaterialScript()
    command = ["--edit", "test_material"]
    mock_path = "supriya.cli.ProjectPackageScript._call_subprocess"
    with unittest.mock.patch(mock_path) as mock:
        mock.return_value = 0
        with uqbar.io.RedirectedStreams(stdout=string_io), uqbar.io.DirectoryChange(
            cli_paths.inner_project_path
        ):
            try:
                script(command)
            except SystemExit as exception:
                if exception.args[0]:
                    raise RuntimeError("SystemExit: {}".format(exception.code))
    pytest.helpers.compare_strings(
        r"""
        Edit candidates: 'test_material' ...
        """,
        string_io.getvalue(),
    )
    definition_path = material_path.joinpath("definition.py")
    command = "{} {!s}".format(
        supriya.config.get("core", "editor", fallback="vim"), definition_path
    )
    mock.assert_called_with(command)
开发者ID:josiah-wolf-oberholtzer,项目名称:supriya,代码行数:30,代码来源:test_cli_ManageMaterialScript_edit.py


示例2: test_init_and_close_loop_for_test

    def test_init_and_close_loop_for_test(self):
        default_loop = self.create_default_loop()

        @asynctest.lenient
        class LoopTest(asynctest.TestCase):
            failing = False

            def runTest(self):
                try:
                    self.assertIsNotNone(self.loop)
                    self.assertFalse(self.loop.close.called)
                except Exception:
                    self.failing = True
                    raise

            def runFailingTest(self):
                self.runTest()
                raise SystemError()

        for method, test in itertools.product(self.run_methods, ('runTest', 'runFailingTest', )):
            with self.subTest(method=method, test=test), \
                    unittest.mock.patch('asyncio.new_event_loop') as mock:
                mock_loop = unittest.mock.Mock(asyncio.AbstractEventLoop)
                mock.return_value = mock_loop

                case = LoopTest(test)
                try:
                    getattr(case, method)()
                except SystemError:
                    pass

                mock.assert_called_with()
                mock_loop.close.assert_called_with()
                self.assertFalse(case.failing)
                self.assertIs(default_loop, asyncio.get_event_loop())
开发者ID:foolswood,项目名称:asynctest,代码行数:35,代码来源:test_case.py


示例3: test_side_effect

    def test_side_effect(self):
        mock = Mock()

        def effect(*args, **kwargs):
            raise SystemError('kablooie')

        mock.side_effect = effect
        self.assertRaises(SystemError, mock, 1, 2, fish=3)
        mock.assert_called_with(1, 2, fish=3)

        results = [1, 2, 3]
        def effect():
            return results.pop()
        mock.side_effect = effect

        self.assertEqual([mock(), mock(), mock()], [3, 2, 1],
                          "side effect not used correctly")

        mock = Mock(side_effect=sentinel.SideEffect)
        self.assertEqual(mock.side_effect, sentinel.SideEffect,
                          "side effect in constructor not used")

        def side_effect():
            return DEFAULT
        mock = Mock(side_effect=side_effect, return_value=sentinel.RETURN)
        self.assertEqual(mock(), sentinel.RETURN)
开发者ID:MarkTseng,项目名称:cpython,代码行数:26,代码来源:testmock.py


示例4: test_received_cmd_is_invalid

 def test_received_cmd_is_invalid(self):
     inst = self.make_agent(core.BaseAgent, None)
     with unittest.mock.patch.object(inst.timer, 'wait',
                                     return_value=('invalid', None)):
         with unittest.mock.patch.object(inst.timer, 'response') as mock:
             inst.delayfunc(5)
             mock.assert_called_with(is_ok=False, detail='invalid cmd')
开发者ID:zhangjoto,项目名称:agent,代码行数:7,代码来源:test_core.py


示例5: test_callbackConnect_non_gns3_server

def test_callbackConnect_non_gns3_server(http_client):

    params = {
        "virus": True,
    }
    mock = unittest.mock.MagicMock()
    http_client._callbackConnect("GET", "/version", mock, {}, {}, params)
    assert http_client._connected is False
    mock.assert_called_with({"message": "The remote server http://127.0.0.1:8000 is not a GNS3 server"}, error=True, server=http_client)
开发者ID:AJNOURI,项目名称:gns3-gui,代码行数:9,代码来源:test_http_client.py


示例6: test_assert_called_with

 def test_assert_called_with(self):
     mock = Mock()
     mock()
     mock.assert_called_with()
     self.assertRaises(AssertionError, mock.assert_called_with, 1)
     mock.reset_mock()
     self.assertRaises(AssertionError, mock.assert_called_with)
     mock(1, 2, 3, a='fish', b='nothing')
     mock.assert_called_with(1, 2, 3, a='fish', b='nothing')
开发者ID:johndpope,项目名称:sims4-ai-engine,代码行数:9,代码来源:testmock.py


示例7: test_java_exception_side_effect

 def test_java_exception_side_effect(self):
     import java
     mock = Mock(side_effect=java.lang.RuntimeException('Boom!'))
     try:
         mock(1, 2, fish=3)
     except java.lang.RuntimeException:
         pass
     self.fail('java exception not raised')
     mock.assert_called_with(1, 2, fish=3)
开发者ID:johndpope,项目名称:sims4-ai-engine,代码行数:9,代码来源:testmock.py


示例8: test_callbackConnect_version_non_local

def test_callbackConnect_version_non_local(http_client):

    params = {
        "local": False,
        "version": __version__
    }
    mock = unittest.mock.MagicMock()
    http_client._callbackConnect("GET", "/version", mock, {}, {}, params)
    assert http_client._connected is False
    mock.assert_called_with({"message": "Running server is not a GNS3 local server (not started with --local)"}, error=True, server=http_client)
开发者ID:AJNOURI,项目名称:gns3-gui,代码行数:10,代码来源:test_http_client.py


示例9: test_callbackConnect_major_version_invalid

def test_callbackConnect_major_version_invalid(http_client):

    params = {
        "local": True,
        "version": "1.2.3"
    }
    mock = unittest.mock.MagicMock()
    http_client._callbackConnect("GET", "/version", mock, {}, {}, params)
    assert http_client._connected is False
    mock.assert_called_with({"message": "Client version {} differs with server version 1.2.3".format(__version__)}, error=True, server=http_client)
开发者ID:AJNOURI,项目名称:gns3-gui,代码行数:10,代码来源:test_http_client.py


示例10: test_setting_call

    def test_setting_call(self):
        mock = Mock()
        def __call__(self, a):
            return self._mock_call(a)

        type(mock).__call__ = __call__
        mock('one')
        mock.assert_called_with('one')

        self.assertRaises(TypeError, mock, 'one', 'two')
开发者ID:MarkTseng,项目名称:cpython,代码行数:10,代码来源:testmock.py


示例11: test_callbackConnect_non_gns3_server

def test_callbackConnect_non_gns3_server(http_client):

    http_client.setMaxRetryConnection(0)
    params = {
        "virus": True,
    }
    mock = unittest.mock.MagicMock()
    http_client._query_waiting_connections.append((None, mock))
    http_client._callbackConnect(params)
    assert http_client._connected is False
    mock.assert_called_with({"message": "The remote server http://127.0.0.1:3080 is not a GNS3 server"}, error=True, server=None)
开发者ID:GNS3,项目名称:gns3-gui,代码行数:11,代码来源:test_http_client.py


示例12: test_callbackConnect_major_version_invalid

def test_callbackConnect_major_version_invalid(http_client):

    params = {
        "local": True,
        "version": "1.2.3"
    }
    mock = unittest.mock.MagicMock()
    http_client._query_waiting_connections.append((None, mock))
    http_client._callbackConnect(params)
    assert http_client._connected is False
    mock.assert_called_with({"message": "Client version {} is not the same as server (controller) version 1.2.3".format(__version__)}, error=True, server=None)
开发者ID:GNS3,项目名称:gns3-gui,代码行数:11,代码来源:test_http_client.py


示例13: test_java_exception_side_effect

    def test_java_exception_side_effect(self):
        import java
        mock = Mock(side_effect=java.lang.RuntimeException("Boom!"))

        # can't use assertRaises with java exceptions
        try:
            mock(1, 2, fish=3)
        except java.lang.RuntimeException:
            pass
        else:
            self.fail('java exception not raised')
        mock.assert_called_with(1,2, fish=3)
开发者ID:MarkTseng,项目名称:cpython,代码行数:12,代码来源:testmock.py


示例14: test_assert_called_with

    def test_assert_called_with(self):
        mock = Mock()
        mock()

        # Will raise an exception if it fails
        mock.assert_called_with()
        self.assertRaises(AssertionError, mock.assert_called_with, 1)

        mock.reset_mock()
        self.assertRaises(AssertionError, mock.assert_called_with)

        mock(1, 2, 3, a="fish", b="nothing")
        mock.assert_called_with(1, 2, 3, a="fish", b="nothing")
开发者ID:GaloisInc,项目名称:echronos,代码行数:13,代码来源:testmock.py


示例15: test_init_and_close_loop_for_test

    def test_init_and_close_loop_for_test(self):
        default_loop = self.create_default_loop()

        @asynctest.lenient
        class LoopTest(asynctest.TestCase):
            failing = False

            def runTest(self):
                try:
                    self.assertIsNotNone(self.loop)
                    self.assertFalse(self.loop.close.called)
                except Exception:
                    self.failing = True
                    raise

            def runFailingTest(self):
                self.runTest()
                raise SystemError()

        for method, test in itertools.product(self.run_methods, ('runTest', 'runFailingTest', )):
            with self.subTest(method=method, test=test), \
                    unittest.mock.patch('asyncio.new_event_loop') as mock:
                mock_loop = unittest.mock.Mock(asyncio.AbstractEventLoop)
                mock_loop.run_until_complete.side_effect = default_loop.run_until_complete

                if sys.version_info >= (3, 6):
                    mock_loop.shutdown_asyncgens = asynctest.CoroutineMock()

                mock.return_value = mock_loop

                case = LoopTest(test)
                try:
                    getattr(case, method)()
                except SystemError:
                    pass

                mock.assert_called_with()
                mock_loop.close.assert_called_with()

                if sys.version_info >= (3, 6):
                    mock_loop.shutdown_asyncgens.assert_awaited()

                # If failing is True, one of the assertions in runTest failed
                self.assertFalse(case.failing)
                # Also, ensure we didn't override the original loop
                self.assertIs(default_loop, asyncio.get_event_loop())
开发者ID:Martiusweb,项目名称:asynctest,代码行数:46,代码来源:test_case.py


示例16: test_callbackConnect_minor_version_invalid

def test_callbackConnect_minor_version_invalid(http_client):

    new_version = "{}.{}.{}".format(__version_info__[0], __version_info__[1], __version_info__[2] + 1)
    params = {
        "local": True,
        "version": new_version
    }
    mock = unittest.mock.MagicMock()

    # Stable release
    if __version_info__[3] == 0:
        http_client._callbackConnect("GET", "/version", mock, {}, {}, params)
        assert http_client._connected is False
        mock.assert_called_with({"message": "Client version {} differs with server version {}".format(__version__, new_version)}, error=True, server=http_client)
    else:
        http_client._callbackConnect("GET", "/version", mock, {}, {}, params)
        assert http_client._connected is True
开发者ID:AJNOURI,项目名称:gns3-gui,代码行数:17,代码来源:test_http_client.py


示例17: test_callbackConnect_minor_version_invalid

def test_callbackConnect_minor_version_invalid(http_client):

    new_version = "{}.{}.{}".format(__version_info__[0], __version_info__[1], __version_info__[2] + 1)
    params = {
        "local": True,
        "version": new_version
    }
    mock = unittest.mock.MagicMock()

    http_client._query_waiting_connections.append((None, mock))
    # Stable release
    if __version_info__[3] == 0:
        http_client._callbackConnect(params)
        assert http_client._connected is False
        mock.assert_called_with({"message": "Client version {} is not the same as server (controller) version {}".format(__version__, new_version)}, error=True, server=None)
    else:
        http_client._callbackConnect(params)
        assert http_client._connected is True
开发者ID:GNS3,项目名称:gns3-gui,代码行数:18,代码来源:test_http_client.py


示例18: assert_called

def assert_called(context):
    actual = json.loads(context.text or '{}')
    # Kinda hacky but tokens are time based
    mail = context.mocks['jam.plugins.user.sendgrid'].SendGridClient().send.call_args[0][0]

    mock = context.mocks['jam.plugins.user.sendgrid'].Mail
    mock.assert_called_with(to=actual['to'])

    if actual.get('from'):
        mail.set_from(actual.get('from'))

    token = mail.add_substitution.call_args_list[0][0][1]
    mail.add_substitution.assert_any_call(':token', token)

    user = mail.add_substitution.call_args_list[1][0][1]
    mail.add_substitution.assert_any_call(':user', user)

    mail.add_filter.assert_any_call('templates', 'enable', 1)
    mail.add_filter.assert_any_call('templates', 'template_id', actual['template'])
开发者ID:CenterForOpenScience,项目名称:jamdb,代码行数:19,代码来源:plugins.py


示例19: test_before_test_called_before_user_setup

    def test_before_test_called_before_user_setup(self):
        mock = unittest.mock.Mock()
        setattr(asynctest._fail_on._fail_on, "before_test_default", mock)
        self.addCleanup(delattr, asynctest._fail_on._fail_on,
                        "before_test_default")

        class TestCase(asynctest.TestCase):
            def setUp(self):
                self.assertTrue(mock.called)

            def runTest(self):
                pass

        for method in self.run_methods:
            with self.subTest(method=method):
                case = Test.FooTestCase()
                getattr(case, method)()

                self.assert_checked("default")
                self.assertTrue(mock.called)
                mock.assert_called_with(case)
开发者ID:foolswood,项目名称:asynctest,代码行数:21,代码来源:test_case.py


示例20: test_assert_called_with_function_spec

    def test_assert_called_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_with(1, 2, 3)
        mock.assert_called_with(a=1, b=2, c=3)
        self.assertRaises(AssertionError, mock.assert_called_with, 1, b=3, c=2)
        # Expected call doesn't match the spec's signature
        with self.assertRaises(AssertionError) as cm:
            mock.assert_called_with(e=8)
        self.assertIsInstance(cm.exception.__cause__, TypeError)
开发者ID:chayao2015,项目名称:kbengine,代码行数:14,代码来源:testmock.py



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
Python mock.assert_has_calls函数代码示例发布时间:2022-05-27
下一篇:
Python mock.assert_called_once_with函数代码示例发布时间: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