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

Python mock.reset_mock函数代码示例

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

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



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

示例1: test_arg_lists

    def test_arg_lists(self):
        mocks = [
            Mock(),
            MagicMock(),
            NonCallableMock(),
            NonCallableMagicMock()
        ]

        def assert_attrs(mock):
            names = 'call_args_list', 'method_calls', 'mock_calls'
            for name in names:
                attr = getattr(mock, name)
                self.assertIsInstance(attr, _CallList)
                self.assertIsInstance(attr, list)
                self.assertEqual(attr, [])

        for mock in mocks:
            assert_attrs(mock)

            if callable(mock):
                mock()
                mock(1, 2)
                mock(a=3)

                mock.reset_mock()
                assert_attrs(mock)

            mock.foo()
            mock.foo.bar(1, a=3)
            mock.foo(1).bar().baz(3)

            mock.reset_mock()
            assert_attrs(mock)
开发者ID:MarkTseng,项目名称:cpython,代码行数:33,代码来源:testmock.py


示例2: test_no_cache

    def test_no_cache(self):
        # Two runs without using the cache shall always run analyze() again.
        section = Section('test-section')
        filedict = {}

        task_args = 3, 4, 5
        bear = CustomTasksBear(section, filedict, tasks=[task_args])

        with unittest.mock.patch.object(bear, 'analyze',
                                        wraps=bear.analyze) as mock:
            # By default, omitting the cache parameter shall mean "no cache".
            results = self.execute_run({bear})
            mock.assert_called_once_with(*task_args)
            self.assertEqual(results, list(task_args))

            mock.reset_mock()

            results = self.execute_run({bear})
            mock.assert_called_once_with(*task_args)
            self.assertEqual(results, list(task_args))

            mock.reset_mock()

            # Passing None for cache shall disable it too explicitly.
            results = self.execute_run({bear}, None)
            mock.assert_called_once_with(*task_args)
            self.assertEqual(results, list(task_args))
开发者ID:Nosferatul,项目名称:coala,代码行数:27,代码来源:CoreTest.py


示例3: test_call

    def test_call(self):
        mock = Mock()
        self.assertTrue(is_instance(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")
开发者ID:MarkTseng,项目名称:cpython,代码行数:32,代码来源:testmock.py


示例4: test_reset_mock

    def test_reset_mock(self):
        parent = Mock()
        spec = ["something"]
        mock = Mock(name="child", parent=parent, spec=spec)
        mock(sentinel.Something, something=sentinel.SomethingElse)
        something = mock.something
        mock.something()
        mock.side_effect = sentinel.SideEffect
        return_value = mock.return_value
        return_value()

        mock.reset_mock()

        self.assertEqual(mock._mock_name, "child", "name incorrectly reset")
        self.assertEqual(mock._mock_parent, parent, "parent incorrectly reset")
        self.assertEqual(mock._mock_methods, spec, "methods incorrectly reset")

        self.assertFalse(mock.called, "called not reset")
        self.assertEqual(mock.call_count, 0, "call_count not reset")
        self.assertEqual(mock.call_args, None, "call_args not reset")
        self.assertEqual(mock.call_args_list, [], "call_args_list not reset")
        self.assertEqual(
            mock.method_calls, [], "method_calls not initialised correctly: %r != %r" % (mock.method_calls, [])
        )
        self.assertEqual(mock.mock_calls, [])

        self.assertEqual(mock.side_effect, sentinel.SideEffect, "side_effect incorrectly reset")
        self.assertEqual(mock.return_value, return_value, "return_value incorrectly reset")
        self.assertFalse(return_value.called, "return value mock not reset")
        self.assertEqual(mock._mock_children, {"something": something}, "children reset incorrectly")
        self.assertEqual(mock.something, something, "children incorrectly cleared")
        self.assertFalse(mock.something.called, "child not reset")
开发者ID:GaloisInc,项目名称:echronos,代码行数:32,代码来源:testmock.py


示例5: test_reset_mock

 def test_reset_mock(self):
     parent = Mock()
     spec = ['something']
     mock = Mock(name='child', parent=parent, spec=spec)
     mock(sentinel.Something, something=sentinel.SomethingElse)
     something = mock.something
     mock.something()
     mock.side_effect = sentinel.SideEffect
     return_value = mock.return_value
     return_value()
     mock.reset_mock()
     self.assertEqual(mock._mock_name, 'child', 'name incorrectly reset')
     self.assertEqual(mock._mock_parent, parent, 'parent incorrectly reset')
     self.assertEqual(mock._mock_methods, spec, 'methods incorrectly reset')
     self.assertFalse(mock.called, 'called not reset')
     self.assertEqual(mock.call_count, 0, 'call_count not reset')
     self.assertEqual(mock.call_args, None, 'call_args not reset')
     self.assertEqual(mock.call_args_list, [], 'call_args_list not reset')
     self.assertEqual(mock.method_calls, [], 'method_calls not initialised correctly: %r != %r' % (mock.method_calls, []))
     self.assertEqual(mock.mock_calls, [])
     self.assertEqual(mock.side_effect, sentinel.SideEffect, 'side_effect incorrectly reset')
     self.assertEqual(mock.return_value, return_value, 'return_value incorrectly reset')
     self.assertFalse(return_value.called, 'return value mock not reset')
     self.assertEqual(mock._mock_children, {'something': something}, 'children reset incorrectly')
     self.assertEqual(mock.something, something, 'children incorrectly cleared')
     self.assertFalse(mock.something.called, 'child not reset')
开发者ID:johndpope,项目名称:sims4-ai-engine,代码行数:26,代码来源:testmock.py


示例6: _hard_reset_mock

    def _hard_reset_mock(self, mock):
        """Reset a mock and also clear its return value and side effects.

        :param MagicMock mock: mock instance to reset
        """
        mock.reset_mock()
        mock.return_value = MagicMock(name='mock()')
        mock.side_effect = None
开发者ID:liveblog,项目名称:superdesk-core,代码行数:8,代码来源:rss_test.py


示例7: 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


示例8: test_coerce_anything

    def test_coerce_anything(self):
        t = xso.Bool()
        mock = unittest.mock.MagicMock()

        result = mock.__bool__()
        mock.reset_mock()

        self.assertEqual(
            result,
            t.coerce(mock))

        mock.__bool__.assert_called_once_with()
开发者ID:sdfsdhgjkbmnmxc,项目名称:aioxmpp,代码行数:12,代码来源:test_types.py


示例9: test_cache

    def test_cache(self):
        section = Section('test-section')
        filedict = {}

        cache = {}

        task_args = 10, 11, 12
        bear = CustomTasksBear(section, filedict, tasks=[task_args])

        with unittest.mock.patch.object(bear, 'analyze',
                                        wraps=bear.analyze) as mock:
            # First time we have a cache miss.
            results = self.execute_run({bear}, cache)
            mock.assert_called_once_with(*task_args)
            self.assertEqual(results, list(task_args))
            self.assertEqual(len(cache), 1)
            self.assertEqual(next(iter(cache.keys())), CustomTasksBear)
            self.assertEqual(len(next(iter(cache.values()))), 1)

            # All following times we have a cache hit (we don't modify the
            # cache in between).
            for i in range(3):
                mock.reset_mock()

                results = self.execute_run({bear}, cache)
                self.assertFalse(mock.called)
                self.assertEqual(results, list(task_args))
                self.assertEqual(len(cache), 1)
                self.assertIn(CustomTasksBear, cache)
                self.assertEqual(len(next(iter(cache.values()))), 1)

        task_args = 500, 11, 12
        bear = CustomTasksBear(section, filedict, tasks=[task_args])
        with unittest.mock.patch.object(bear, 'analyze',
                                        wraps=bear.analyze) as mock:
            # Invocation with different args should add another cache entry,
            # and invoke analyze() again because those weren't cached before.
            results = self.execute_run({bear}, cache)
            mock.assert_called_once_with(*task_args)
            self.assertEqual(results, list(task_args))
            self.assertEqual(len(cache), 1)
            self.assertIn(CustomTasksBear, cache)
            self.assertEqual(len(next(iter(cache.values()))), 2)

            mock.reset_mock()

            results = self.execute_run({bear}, cache)
            self.assertFalse(mock.called)
            self.assertEqual(results, list(task_args))
            self.assertEqual(len(cache), 1)
            self.assertIn(CustomTasksBear, cache)
            self.assertEqual(len(next(iter(cache.values()))), 2)
开发者ID:Nosferatul,项目名称:coala,代码行数:52,代码来源:CoreTest.py


示例10: 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


示例11: test_assert_called_once_with

 def test_assert_called_once_with(self):
     mock = Mock()
     mock()
     mock.assert_called_once_with()
     mock()
     self.assertRaises(AssertionError, mock.assert_called_once_with)
     mock.reset_mock()
     self.assertRaises(AssertionError, mock.assert_called_once_with)
     mock('foo', 'bar', baz=2)
     mock.assert_called_once_with('foo', 'bar', baz=2)
     mock.reset_mock()
     mock('foo', 'bar', baz=2)
     self.assertRaises(AssertionError, lambda : mock.assert_called_once_with('bob', 'bar', baz=2))
开发者ID:johndpope,项目名称:sims4-ai-engine,代码行数:13,代码来源:testmock.py


示例12: test_assert_called_once_with

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

        # Will raise an exception if it fails
        mock.assert_called_once_with()

        mock()
        self.assertRaises(AssertionError, mock.assert_called_once_with)

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

        mock("foo", "bar", baz=2)
        mock.assert_called_once_with("foo", "bar", baz=2)

        mock.reset_mock()
        mock("foo", "bar", baz=2)
        self.assertRaises(AssertionError, lambda: mock.assert_called_once_with("bob", "bar", baz=2))
开发者ID:GaloisInc,项目名称:echronos,代码行数:19,代码来源:testmock.py


示例13: test_reset_mock_recursion

    def test_reset_mock_recursion(self):
        mock = Mock()
        mock.return_value = mock

        # used to cause recursion
        mock.reset_mock()
开发者ID:MarkTseng,项目名称:cpython,代码行数:6,代码来源:testmock.py


示例14: test_async_gen_fixture_finalized

async def test_async_gen_fixture_finalized(mock):
    try:
        assert mock.called
        assert mock.call_args_list[-1] == unittest.mock.call(END)
    finally:
        mock.reset_mock()
开发者ID:pytest-dev,项目名称:pytest-asyncio,代码行数:6,代码来源:test_async_gen_fixtures_36.py


示例15: test_reset_mock_recursion

 def test_reset_mock_recursion(self):
     mock = Mock()
     mock.return_value = mock
     mock.reset_mock()
开发者ID:johndpope,项目名称:sims4-ai-engine,代码行数:4,代码来源:testmock.py


示例16: test_get_or_insert

    def test_get_or_insert(self, monkeypatch, model):
        """Test the get_or_insert function"""
        reset_mocks = lambda mocks: [mock.reset_mock() for mock in mocks]
        mock_model_insert_many = mock.Mock()
        insert_many_execute = mock_model_insert_many.return_value.execute

        mock_model_select = mock.Mock()
        model_select_where = mock_model_select.return_value.where
        model_select_where.return_value = [mock.sentinel.rv]

        mock_elt_insert = mock.MagicMock()
        mock_elt_insert.__getitem__.return_value = mock.sentinel.elt_insert

        mocks = [mock_model_insert_many, insert_many_execute, mock_elt_insert,
                 mock_model_select, model_select_where]

        monkeypatch.setattr(model, 'insert_many', mock_model_insert_many)
        monkeypatch.setattr(model, 'select', mock_model_select)

        elts_insert = [mock_elt_insert]
        elts_get = [mock.sentinel.elt_get]

        rv = api_recipes.get_or_insert(model, elts_insert, elts_get)

        where_exp_get = peewee.Expression(model.id, peewee.OP.IN,
                                          [mock.sentinel.elt_get])
        where_exp_insert = peewee.Expression(model.name, peewee.OP.IN,
                                             [mock.sentinel.elt_insert])
        where_exp = peewee.Expression(where_exp_get, peewee.OP.OR,
                                      where_exp_insert)

        insert_many_calls = [mock.call(elts_insert, model.name)]
        get_item_calls = [mock.call('name')]
        assert rv == [mock.sentinel.rv]
        assert mock_model_insert_many.call_args_list == insert_many_calls
        assert insert_many_execute.call_args_list == [mock.call()]
        assert mock_elt_insert.__getitem__.call_args_list == get_item_calls
        assert mock_model_select.call_args_list == [mock.call()]
        assert model_select_where.call_args_list == [mock.call(where_exp)]

        reset_mocks(mocks)
        rv = api_recipes.get_or_insert(model, elts_insert, None)

        assert rv == [mock.sentinel.rv]
        assert mock_model_insert_many.call_args_list == insert_many_calls
        assert insert_many_execute.call_args_list == [mock.call()]
        assert mock_elt_insert.__getitem__.call_args_list == get_item_calls
        assert mock_model_select.call_args_list == [mock.call()]
        assert model_select_where.call_args_list == [
            mock.call(where_exp_insert)
        ]

        reset_mocks(mocks)
        rv = api_recipes.get_or_insert(model, None, elts_get)

        assert rv == [mock.sentinel.rv]
        assert mock_model_insert_many.call_args_list == []
        assert insert_many_execute.call_args_list == []
        assert mock_elt_insert.__getitem__.call_args_list == []
        assert mock_model_select.call_args_list == [mock.call()]
        assert model_select_where.call_args_list == [mock.call(where_exp_get)]

        reset_mocks(mocks)
        rv = api_recipes.get_or_insert(model, None, None)

        assert rv == []
        assert mock_model_insert_many.call_args_list == []
        assert insert_many_execute.call_args_list == []
        assert mock_elt_insert.__getitem__.call_args_list == []
        assert mock_model_select.call_args_list == []
        assert model_select_where.call_args_list == []
开发者ID:RulzUrLife,项目名称:RulzUrAPI,代码行数:71,代码来源:recipes_test.py



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

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