当前位置: 首页>>代码示例>>Python>>正文


Python mock.foo方法代码示例

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


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

示例1: test_assert_called_once_with

# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import foo [as 别名]
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:war-and-code,项目名称:jawfish,代码行数:24,代码来源:testmock.py

示例2: test_subclass_with_properties

# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import foo [as 别名]
def test_subclass_with_properties(self):
        class SubClass(Mock):
            def _get(self):
                return 3
            def _set(self, value):
                raise NameError('strange error')
            some_attribute = property(_get, _set)

        s = SubClass(spec_set=SubClass)
        self.assertEqual(s.some_attribute, 3)

        def test():
            s.some_attribute = 3
        self.assertRaises(NameError, test)

        def test():
            s.foo = 'bar'
        self.assertRaises(AttributeError, test) 
开发者ID:war-and-code,项目名称:jawfish,代码行数:20,代码来源:testmock.py

示例3: test_configure_mock

# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import foo [as 别名]
def test_configure_mock(self):
        mock = Mock(foo='bar')
        self.assertEqual(mock.foo, 'bar')

        mock = MagicMock(foo='bar')
        self.assertEqual(mock.foo, 'bar')

        kwargs = {'side_effect': KeyError, 'foo.bar.return_value': 33,
                  'foo': MagicMock()}
        mock = Mock(**kwargs)
        self.assertRaises(KeyError, mock)
        self.assertEqual(mock.foo.bar(), 33)
        self.assertIsInstance(mock.foo, MagicMock)

        mock = Mock()
        mock.configure_mock(**kwargs)
        self.assertRaises(KeyError, mock)
        self.assertEqual(mock.foo.bar(), 33)
        self.assertIsInstance(mock.foo, MagicMock) 
开发者ID:war-and-code,项目名称:jawfish,代码行数:21,代码来源:testmock.py

示例4: test_adding_child_mock

# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import foo [as 别名]
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:war-and-code,项目名称:jawfish,代码行数:24,代码来源:testmock.py

示例5: test_repr

# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import foo [as 别名]
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:war-and-code,项目名称:jawfish,代码行数:16,代码来源:testmock.py

示例6: test_repr_with_spec

# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import foo [as 别名]
def test_repr_with_spec(self):
        class X(object):
            pass

        mock = Mock(spec=X)
        self.assertIn(" spec='X' ", repr(mock))

        mock = Mock(spec=X())
        self.assertIn(" spec='X' ", repr(mock))

        mock = Mock(spec_set=X)
        self.assertIn(" spec_set='X' ", repr(mock))

        mock = Mock(spec_set=X())
        self.assertIn(" spec_set='X' ", repr(mock))

        mock = Mock(spec=X, name='foo')
        self.assertIn(" spec='X' ", repr(mock))
        self.assertIn(" name='foo' ", repr(mock))

        mock = Mock(name='foo')
        self.assertNotIn("spec", repr(mock))

        mock = Mock()
        self.assertNotIn("spec", repr(mock))

        mock = Mock(spec=['foo'])
        self.assertNotIn("spec", repr(mock)) 
开发者ID:war-and-code,项目名称:jawfish,代码行数:30,代码来源:testmock.py

示例7: test_exceptional_side_effect

# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import foo [as 别名]
def test_exceptional_side_effect(self):
        mock = Mock(side_effect=AttributeError)
        self.assertRaises(AttributeError, mock)

        mock = Mock(side_effect=AttributeError('foo'))
        self.assertRaises(AttributeError, mock) 
开发者ID:war-and-code,项目名称:jawfish,代码行数:8,代码来源:testmock.py

示例8: test_baseexceptional_side_effect

# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import foo [as 别名]
def test_baseexceptional_side_effect(self):
        mock = Mock(side_effect=KeyboardInterrupt)
        self.assertRaises(KeyboardInterrupt, mock)

        mock = Mock(side_effect=KeyboardInterrupt('foo'))
        self.assertRaises(KeyboardInterrupt, mock) 
开发者ID:war-and-code,项目名称:jawfish,代码行数:8,代码来源:testmock.py

示例9: test_spec_list_subclass

# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import foo [as 别名]
def test_spec_list_subclass(self):
        class Sub(list):
            pass
        mock = Mock(spec=Sub(['foo']))

        mock.append(3)
        mock.append.assert_called_with(3)
        self.assertRaises(AttributeError, getattr, mock, 'foo') 
开发者ID:war-and-code,项目名称:jawfish,代码行数:10,代码来源:testmock.py

示例10: test_setting_attribute_with_spec_set

# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import foo [as 别名]
def test_setting_attribute_with_spec_set(self):
        class X(object):
            y = 3

        mock = Mock(spec=X)
        mock.x = 'foo'

        mock = Mock(spec_set=X)
        def set_attr():
            mock.x = 'foo'

        mock.y = 'foo'
        self.assertRaises(AttributeError, set_attr) 
开发者ID:war-and-code,项目名称:jawfish,代码行数:15,代码来源:testmock.py

示例11: test_arg_lists

# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import foo [as 别名]
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:war-and-code,项目名称:jawfish,代码行数:35,代码来源:testmock.py

示例12: test_assert_has_calls

# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import foo [as 别名]
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:war-and-code,项目名称:jawfish,代码行数:40,代码来源:testmock.py

示例13: test_mock_add_spec_magic_methods

# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import foo [as 别名]
def test_mock_add_spec_magic_methods(self):
        for Klass in MagicMock, NonCallableMagicMock:
            mock = Klass()
            int(mock)

            mock.mock_add_spec(object)
            self.assertRaises(TypeError, int, mock)

            mock = Klass()
            mock['foo']
            mock.__int__.return_value =4

            mock.mock_add_spec(int)
            self.assertEqual(int(mock), 4)
            self.assertRaises(TypeError, lambda: mock['foo']) 
开发者ID:war-and-code,项目名称:jawfish,代码行数:17,代码来源:testmock.py

示例14: test_attach_mock

# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import foo [as 别名]
def test_attach_mock(self):
        classes = Mock, MagicMock, NonCallableMagicMock, NonCallableMock
        for Klass in classes:
            for Klass2 in classes:
                m = Klass()

                m2 = Klass2(name='foo')
                m.attach_mock(m2, 'bar')

                self.assertIs(m.bar, m2)
                self.assertIn("name='mock.bar'", repr(m2))

                m.bar.baz(1)
                self.assertEqual(m.mock_calls, [call.bar.baz(1)])
                self.assertEqual(m.method_calls, [call.bar.baz(1)]) 
开发者ID:war-and-code,项目名称:jawfish,代码行数:17,代码来源:testmock.py

示例15: test_attach_mock_return_value

# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import foo [as 别名]
def test_attach_mock_return_value(self):
        classes = Mock, MagicMock, NonCallableMagicMock, NonCallableMock
        for Klass in Mock, MagicMock:
            for Klass2 in classes:
                m = Klass()

                m2 = Klass2(name='foo')
                m.attach_mock(m2, 'return_value')

                self.assertIs(m(), m2)
                self.assertIn("name='mock()'", repr(m2))

                m2.foo()
                self.assertEqual(m.mock_calls, call().foo().call_list()) 
开发者ID:war-and-code,项目名称:jawfish,代码行数:16,代码来源:testmock.py


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