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

Python mock.foo函数代码示例

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

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



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

示例1: test_assert_has_calls

    def test_assert_has_calls(self):
        kalls1 = [call(1, 2), ({"a": 3},), ((3, 4),), call(b=6), ("", (1,), {"b": 6})]
        kalls2 = [call.foo(), call.bar(1)]
        kalls2.extend(call.spam().baz(a=3).call_list())
        kalls2.extend(call.bam(set(), foo={}).fish([1]).call_list())

        mocks = []
        for mock in Mock(), MagicMock():
            mock(1, 2)
            mock(a=3)
            mock(3, 4)
            mock(b=6)
            mock(1, b=6)
            mocks.append((mock, kalls1))

        mock = Mock()
        mock.foo()
        mock.bar(1)
        mock.spam().baz(a=3)
        mock.bam(set(), foo={}).fish([1])
        mocks.append((mock, kalls2))

        for mock, kalls in mocks:
            for i in range(len(kalls)):
                for step in 1, 2, 3:
                    these = kalls[i : i + step]
                    mock.assert_has_calls(these)

                    if len(these) > 1:
                        self.assertRaises(AssertionError, mock.assert_has_calls, list(reversed(these)))
开发者ID:GaloisInc,项目名称:echronos,代码行数:30,代码来源:testmock.py


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


示例3: test_assert_called_with_failure_message

 def test_assert_called_with_failure_message(self):
     mock = NonCallableMock()
     expected = "mock(1, '2', 3, bar='foo')"
     message = 'Expected call: %s\nNot called'
     self.assertRaisesWithMsg(AssertionError, message % (expected,), mock.assert_called_with, 1, '2', 3, bar='foo')
     mock.foo(1, '2', 3, foo='foo')
     asserters = [mock.foo.assert_called_with, mock.foo.assert_called_once_with]
     for meth in asserters:
         actual = "foo(1, '2', 3, foo='foo')"
         expected = "foo(1, '2', 3, bar='foo')"
         message = 'Expected call: %s\nActual call: %s'
         self.assertRaisesWithMsg(AssertionError, message % (expected, actual), meth, 1, '2', 3, bar='foo')
     for meth in asserters:
         actual = "foo(1, '2', 3, foo='foo')"
         expected = "foo(bar='foo')"
         message = 'Expected call: %s\nActual call: %s'
         self.assertRaisesWithMsg(AssertionError, message % (expected, actual), meth, bar='foo')
     for meth in asserters:
         actual = "foo(1, '2', 3, foo='foo')"
         expected = 'foo(1, 2, 3)'
         message = 'Expected call: %s\nActual call: %s'
         self.assertRaisesWithMsg(AssertionError, message % (expected, actual), meth, 1, 2, 3)
     for meth in asserters:
         actual = "foo(1, '2', 3, foo='foo')"
         expected = 'foo()'
         message = 'Expected call: %s\nActual call: %s'
         self.assertRaisesWithMsg(AssertionError, message % (expected, actual), meth)
开发者ID:johndpope,项目名称:sims4-ai-engine,代码行数:27,代码来源:testmock.py


示例4: test_repr

 def test_repr(self):
     mock = Mock(name='foo')
     self.assertIn('foo', repr(mock))
     self.assertIn("'%s'" % id(mock), repr(mock))
     mocks = [(Mock(), 'mock'), (Mock(name='bar'), 'bar')]
     for (mock, name) in mocks:
         self.assertIn('%s.bar' % name, repr(mock.bar))
         self.assertIn('%s.foo()' % name, repr(mock.foo()))
         self.assertIn('%s.foo().bing' % name, repr(mock.foo().bing))
         self.assertIn('%s()' % name, repr(mock()))
         self.assertIn('%s()()' % name, repr(mock()()))
         self.assertIn('%s()().foo.bar.baz().bing' % name, repr(mock()().foo.bar.baz().bing))
开发者ID:johndpope,项目名称:sims4-ai-engine,代码行数:12,代码来源:testmock.py


示例5: test_repr

    def test_repr(self):
        mock = Mock(name="foo")
        self.assertIn("foo", repr(mock))
        self.assertIn("'%s'" % id(mock), repr(mock))

        mocks = [(Mock(), "mock"), (Mock(name="bar"), "bar")]
        for mock, name in mocks:
            self.assertIn("%s.bar" % name, repr(mock.bar))
            self.assertIn("%s.foo()" % name, repr(mock.foo()))
            self.assertIn("%s.foo().bing" % name, repr(mock.foo().bing))
            self.assertIn("%s()" % name, repr(mock()))
            self.assertIn("%s()()" % name, repr(mock()()))
            self.assertIn("%s()().foo.bar.baz().bing" % name, repr(mock()().foo.bar.baz().bing))
开发者ID:GaloisInc,项目名称:echronos,代码行数:13,代码来源:testmock.py


示例6: test_mock_calls

 def test_mock_calls(self):
     mock = MagicMock()
     self.assertIs(mock.mock_calls == [], True)
     mock = MagicMock()
     mock()
     expected = [('', (), {})]
     self.assertEqual(mock.mock_calls, expected)
     mock.foo()
     expected.append(call.foo())
     self.assertEqual(mock.mock_calls, expected)
     self.assertEqual(mock.foo.mock_calls, [('', (), {})])
     mock = MagicMock()
     mock().foo(1, 2, 3, a=4, b=5)
     expected = [('', (), {}), ('().foo', (1, 2, 3), dict(a=4, b=5))]
     self.assertEqual(mock.mock_calls, expected)
     self.assertEqual(mock.return_value.foo.mock_calls, [('', (1, 2, 3), dict(a=4, b=5))])
     self.assertEqual(mock.return_value.mock_calls, [('foo', (1, 2, 3), dict(a=4, b=5))])
     mock = MagicMock()
     mock().foo.bar().baz()
     expected = [('', (), {}), ('().foo.bar', (), {}), ('().foo.bar().baz', (), {})]
     self.assertEqual(mock.mock_calls, expected)
     self.assertEqual(mock().mock_calls, call.foo.bar().baz().call_list())
     for kwargs in (dict(), dict(name='bar')):
         mock = MagicMock(**kwargs)
         int(mock.foo)
         expected = [('foo.__int__', (), {})]
         self.assertEqual(mock.mock_calls, expected)
         mock = MagicMock(**kwargs)
         mock.a()()
         expected = [('a', (), {}), ('a()', (), {})]
         self.assertEqual(mock.mock_calls, expected)
         self.assertEqual(mock.a().mock_calls, [call()])
         mock = MagicMock(**kwargs)
         mock(1)(2)(3)
         self.assertEqual(mock.mock_calls, call(1)(2)(3).call_list())
         self.assertEqual(mock().mock_calls, call(2)(3).call_list())
         self.assertEqual(mock()().mock_calls, call(3).call_list())
         mock = MagicMock(**kwargs)
         mock(1)(2)(3).a.b.c(4)
         self.assertEqual(mock.mock_calls, call(1)(2)(3).a.b.c(4).call_list())
         self.assertEqual(mock().mock_calls, call(2)(3).a.b.c(4).call_list())
         self.assertEqual(mock()().mock_calls, call(3).a.b.c(4).call_list())
         mock = MagicMock(**kwargs)
         int(mock().foo.bar().baz())
         last_call = ('().foo.bar().baz().__int__', (), {})
         self.assertEqual(mock.mock_calls[-1], last_call)
         self.assertEqual(mock().mock_calls, call.foo.bar().baz().__int__().call_list())
         self.assertEqual(mock().foo.bar().mock_calls, call.baz().__int__().call_list())
         self.assertEqual(mock().foo.bar().baz.mock_calls, call().__int__().call_list())
开发者ID:johndpope,项目名称:sims4-ai-engine,代码行数:49,代码来源:testmock.py


示例7: test_adding_child_mock

 def test_adding_child_mock(self):
     for Klass in (NonCallableMock, Mock, MagicMock, NonCallableMagicMock):
         mock = Klass()
         mock.foo = Mock()
         mock.foo()
         self.assertEqual(mock.method_calls, [call.foo()])
         self.assertEqual(mock.mock_calls, [call.foo()])
         mock = Klass()
         mock.bar = Mock(name='name')
         mock.bar()
         self.assertEqual(mock.method_calls, [])
         self.assertEqual(mock.mock_calls, [])
         mock = Klass()
         mock.baz = MagicMock()()
         mock.baz()
         self.assertEqual(mock.method_calls, [])
         self.assertEqual(mock.mock_calls, [])
开发者ID:johndpope,项目名称:sims4-ai-engine,代码行数:17,代码来源:testmock.py


示例8: test_adding_child_mock

    def test_adding_child_mock(self):
        for Klass in NonCallableMock, Mock, MagicMock, NonCallableMagicMock:
            mock = Klass()

            mock.foo = Mock()
            mock.foo()

            self.assertEqual(mock.method_calls, [call.foo()])
            self.assertEqual(mock.mock_calls, [call.foo()])

            mock = Klass()
            mock.bar = Mock(name='name')
            mock.bar()
            self.assertEqual(mock.method_calls, [])
            self.assertEqual(mock.mock_calls, [])

            # mock with an existing _new_parent but no name
            mock = Klass()
            mock.baz = MagicMock()()
            mock.baz()
            self.assertEqual(mock.method_calls, [])
            self.assertEqual(mock.mock_calls, [])
开发者ID:MarkTseng,项目名称:cpython,代码行数:22,代码来源:testmock.py


示例9: test_assert_called_with_failure_message

    def test_assert_called_with_failure_message(self):
        mock = NonCallableMock()

        expected = "mock(1, '2', 3, bar='foo')"
        message = "Expected call: %s\nNot called"
        self.assertRaisesWithMsg(AssertionError, message % (expected,), mock.assert_called_with, 1, "2", 3, bar="foo")

        mock.foo(1, "2", 3, foo="foo")

        asserters = [mock.foo.assert_called_with, mock.foo.assert_called_once_with]
        for meth in asserters:
            actual = "foo(1, '2', 3, foo='foo')"
            expected = "foo(1, '2', 3, bar='foo')"
            message = "Expected call: %s\nActual call: %s"
            self.assertRaisesWithMsg(AssertionError, message % (expected, actual), meth, 1, "2", 3, bar="foo")

        # just kwargs
        for meth in asserters:
            actual = "foo(1, '2', 3, foo='foo')"
            expected = "foo(bar='foo')"
            message = "Expected call: %s\nActual call: %s"
            self.assertRaisesWithMsg(AssertionError, message % (expected, actual), meth, bar="foo")

        # just args
        for meth in asserters:
            actual = "foo(1, '2', 3, foo='foo')"
            expected = "foo(1, 2, 3)"
            message = "Expected call: %s\nActual call: %s"
            self.assertRaisesWithMsg(AssertionError, message % (expected, actual), meth, 1, 2, 3)

        # empty
        for meth in asserters:
            actual = "foo(1, '2', 3, foo='foo')"
            expected = "foo()"
            message = "Expected call: %s\nActual call: %s"
            self.assertRaisesWithMsg(AssertionError, message % (expected, actual), meth)
开发者ID:GaloisInc,项目名称:echronos,代码行数:36,代码来源:testmock.py


示例10: test_mock_calls

    def test_mock_calls(self):
        mock = MagicMock()

        # need to do this because MagicMock.mock_calls used to just return
        # a MagicMock which also returned a MagicMock when __eq__ was called
        self.assertIs(mock.mock_calls == [], True)

        mock = MagicMock()
        mock()
        expected = [('', (), {})]
        self.assertEqual(mock.mock_calls, expected)

        mock.foo()
        expected.append(call.foo())
        self.assertEqual(mock.mock_calls, expected)
        # intermediate mock_calls work too
        self.assertEqual(mock.foo.mock_calls, [('', (), {})])

        mock = MagicMock()
        mock().foo(1, 2, 3, a=4, b=5)
        expected = [
            ('', (), {}), ('().foo', (1, 2, 3), dict(a=4, b=5))
        ]
        self.assertEqual(mock.mock_calls, expected)
        self.assertEqual(mock.return_value.foo.mock_calls,
                         [('', (1, 2, 3), dict(a=4, b=5))])
        self.assertEqual(mock.return_value.mock_calls,
                         [('foo', (1, 2, 3), dict(a=4, b=5))])

        mock = MagicMock()
        mock().foo.bar().baz()
        expected = [
            ('', (), {}), ('().foo.bar', (), {}),
            ('().foo.bar().baz', (), {})
        ]
        self.assertEqual(mock.mock_calls, expected)
        self.assertEqual(mock().mock_calls,
                         call.foo.bar().baz().call_list())

        for kwargs in dict(), dict(name='bar'):
            mock = MagicMock(**kwargs)
            int(mock.foo)
            expected = [('foo.__int__', (), {})]
            self.assertEqual(mock.mock_calls, expected)

            mock = MagicMock(**kwargs)
            mock.a()()
            expected = [('a', (), {}), ('a()', (), {})]
            self.assertEqual(mock.mock_calls, expected)
            self.assertEqual(mock.a().mock_calls, [call()])

            mock = MagicMock(**kwargs)
            mock(1)(2)(3)
            self.assertEqual(mock.mock_calls, call(1)(2)(3).call_list())
            self.assertEqual(mock().mock_calls, call(2)(3).call_list())
            self.assertEqual(mock()().mock_calls, call(3).call_list())

            mock = MagicMock(**kwargs)
            mock(1)(2)(3).a.b.c(4)
            self.assertEqual(mock.mock_calls,
                             call(1)(2)(3).a.b.c(4).call_list())
            self.assertEqual(mock().mock_calls,
                             call(2)(3).a.b.c(4).call_list())
            self.assertEqual(mock()().mock_calls,
                             call(3).a.b.c(4).call_list())

            mock = MagicMock(**kwargs)
            int(mock().foo.bar().baz())
            last_call = ('().foo.bar().baz().__int__', (), {})
            self.assertEqual(mock.mock_calls[-1], last_call)
            self.assertEqual(mock().mock_calls,
                             call.foo.bar().baz().__int__().call_list())
            self.assertEqual(mock().foo.bar().mock_calls,
                             call.baz().__int__().call_list())
            self.assertEqual(mock().foo.bar().baz.mock_calls,
                             call().__int__().call_list())
开发者ID:MarkTseng,项目名称:cpython,代码行数:76,代码来源:testmock.py


示例11: MagicMock

# programme ...).
# Pour cela il y a les mock : 
"""Un objet mock, c’est un objet basé sur le null object pattern qui sert à 
faire semblant. Quand on l’instancie avec n’importe quoi, ça marche, quand on 
appelle n’importe quelle méthode, ça marche et ça renvoie un mock"""
import unittest.mock

# Un objet mock peut être appeler comme une classe ou une fonction
# et retouren toujours un mock. Il est possible d'appeler n'importe quel
# méthode et retournera toujors un mock
from unittest.mock import MagicMock # ou from mock import MagicMock
mock = MagicMock()
print(mock)
print(mock())
print(mock(1, True, [Exception, {}]))
print(mock.foo())
print(mock.nimporte().nawak().je().te().dis())
print(mock + mock - 10000)

# Il est possible de faire en sorte qu'ils aient un comportement spécifique.
mock.you = MagicMock(side_effect=ValueError('mofo !')) # un callable marche aussi
#mock.you() # Provoque une ValueError
mock.mock = MagicMock(return_value="moooooooooock")
print(mock.mock())

# Il est possible de combiner de vrai objets avec des mock

class maClasse:
    def func1(self):
        print("coucou")
    def func2(self):
开发者ID:guillaume-havard,项目名称:Tests_py,代码行数:31,代码来源:testsunitaires.py



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

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