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


Python unittest.mock方法代码示例

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


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

示例1: test_unify_no_success_no_recovery_file

# 需要导入模块: import unittest [as 别名]
# 或者: from unittest import mock [as 别名]
def test_unify_no_success_no_recovery_file(self, mock_merge_unique_identities):
        """Test command when the recovery file does not exist, the recovery mode is active and the execution isn't ok"""

        mock_merge_unique_identities.side_effect = Exception

        with unittest.mock.patch('sortinghat.cmd.unify.RecoveryFile.location') as mock_location:
            mock_location.return_value = self.recovery_path

            self.assertFalse(os.path.exists(self.recovery_path))
            with self.assertRaises(Exception):
                self.cmd.run('--recovery')
            self.assertTrue(os.path.exists(self.recovery_path))

            with open(self.recovery_path, 'r') as f:
                count_objs = 0
                for line in f.readlines():
                    matches_obj = json.loads(line.strip("\n"))
                    self.assertTrue(all([isinstance(m, str) for m in matches_obj['identities']]))
                    self.assertFalse(matches_obj['processed'])
                    count_objs += 1

                self.assertEqual(count_objs, 1) 
开发者ID:chaoss,项目名称:grimoirelab-sortinghat,代码行数:24,代码来源:test_cmd_unify.py

示例2: test_should_always_enable_force_admins_when_enabled

# 需要导入模块: import unittest [as 别名]
# 或者: from unittest import mock [as 别名]
def test_should_always_enable_force_admins_when_enabled(self, enable):
        # Given
        options = DotDict({
            'retries': 1,
            'enforce_admins': 'true',
            'github_repository': 'benjefferies/branch-bot-protection'
        })

        # When
        with patch('run.login') as mock:
            mock.return_value\
                .repository.return_value\
                .branch.return_value\
                .protection.return_value\
                .enforce_admins.enabled = True
            toggle_enforce_admin(options)

        # Then
        enable.assert_called_once() 
开发者ID:benjefferies,项目名称:branch-protection-bot,代码行数:21,代码来源:test_run.py

示例3: test_should_always_disable_force_admins_when_disabled

# 需要导入模块: import unittest [as 别名]
# 或者: from unittest import mock [as 别名]
def test_should_always_disable_force_admins_when_disabled(self, disable):
        # Given
        options = DotDict({
            'retries': 1,
            'enforce_admins': 'false',
            'github_repository': 'benjefferies/branch-bot-protection'
        })

        # When
        with patch('run.login') as mock:
            mock.return_value\
                .repository.return_value\
                .branch.return_value\
                .protection.return_value\
                .enforce_admins.enabled = False
            toggle_enforce_admin(options)

        # Then
        disable.assert_called_once() 
开发者ID:benjefferies,项目名称:branch-protection-bot,代码行数:21,代码来源:test_run.py

示例4: test_should_disable_force_admins

# 需要导入模块: import unittest [as 别名]
# 或者: from unittest import mock [as 别名]
def test_should_disable_force_admins(self, disable):
        # Given
        options = DotDict({
            'retries': 1,
            'github_repository': 'benjefferies/branch-bot-protection'
        })

        # When
        with patch('run.login') as mock:
            mock.return_value\
                .repository.return_value\
                .branch.return_value\
                .protection.return_value\
                .enforce_admins.enabled = True
            toggle_enforce_admin(options)

        # Then
        disable.assert_called_once() 
开发者ID:benjefferies,项目名称:branch-protection-bot,代码行数:20,代码来源:test_run.py

示例5: test_should_enable_force_admins

# 需要导入模块: import unittest [as 别名]
# 或者: from unittest import mock [as 别名]
def test_should_enable_force_admins(self, enable):
        # Given
        options = DotDict({
            'retries': 1,
            'github_repository': 'benjefferies/branch-bot-protection'
        })

        # When
        with patch('run.login') as mock:
            mock.return_value\
                .repository.return_value\
                .branch.return_value\
                .protection.return_value\
                .enforce_admins.enabled = False
            toggle_enforce_admin(options)

        # Then
        enable.assert_called_once() 
开发者ID:benjefferies,项目名称:branch-protection-bot,代码行数:20,代码来源:test_run.py

示例6: test_assert_called_once_with

# 需要导入模块: import unittest [as 别名]
# 或者: from unittest import mock [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

示例7: test_method_calls_compare_easily

# 需要导入模块: import unittest [as 别名]
# 或者: from unittest import mock [as 别名]
def test_method_calls_compare_easily(self):
        mock = Mock()
        mock.something()
        self.assertEqual(mock.method_calls, [('something',)])
        self.assertEqual(mock.method_calls, [('something', (), {})])

        mock = Mock()
        mock.something('different')
        self.assertEqual(mock.method_calls, [('something', ('different',))])
        self.assertEqual(mock.method_calls,
                         [('something', ('different',), {})])

        mock = Mock()
        mock.something(x=1)
        self.assertEqual(mock.method_calls, [('something', {'x': 1})])
        self.assertEqual(mock.method_calls, [('something', (), {'x': 1})])

        mock = Mock()
        mock.something('different', some='more')
        self.assertEqual(mock.method_calls, [
            ('something', ('different',), {'some': 'more'})
        ]) 
开发者ID:war-and-code,项目名称:jawfish,代码行数:24,代码来源:testmock.py

示例8: test_from_spec

# 需要导入模块: import unittest [as 别名]
# 或者: from unittest import mock [as 别名]
def test_from_spec(self):
        class Something(object):
            x = 3
            __something__ = None
            def y(self):
                pass

        def test_attributes(mock):
            # should work
            mock.x
            mock.y
            mock.__something__
            self.assertRaisesRegex(
                AttributeError,
                "Mock object has no attribute 'z'",
                getattr, mock, 'z'
            )
            self.assertRaisesRegex(
                AttributeError,
                "Mock object has no attribute '__foobar__'",
                getattr, mock, '__foobar__'
            )

        test_attributes(Mock(spec=Something))
        test_attributes(Mock(spec=Something())) 
开发者ID:war-and-code,项目名称:jawfish,代码行数:27,代码来源:testmock.py

示例9: test_spec_class

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

        mock = Mock(spec=X)
        self.assertTrue(isinstance(mock, X))

        mock = Mock(spec=X())
        self.assertTrue(isinstance(mock, X))

        self.assertIs(mock.__class__, X)
        self.assertEqual(Mock().__class__.__name__, 'Mock')

        mock = Mock(spec_set=X)
        self.assertTrue(isinstance(mock, X))

        mock = Mock(spec_set=X())
        self.assertTrue(isinstance(mock, X)) 
开发者ID:war-and-code,项目名称:jawfish,代码行数:20,代码来源:testmock.py

示例10: test_dir

# 需要导入模块: import unittest [as 别名]
# 或者: from unittest import mock [as 别名]
def test_dir(self):
        mock = Mock()
        attrs = set(dir(mock))
        type_attrs = set([m for m in dir(Mock) if not m.startswith('_')])

        # all public attributes from the type are included
        self.assertEqual(set(), type_attrs - attrs)

        # creates these attributes
        mock.a, mock.b
        self.assertIn('a', dir(mock))
        self.assertIn('b', dir(mock))

        # instance attributes
        mock.c = mock.d = None
        self.assertIn('c', dir(mock))
        self.assertIn('d', dir(mock))

        # magic methods
        mock.__iter__ = lambda s: iter([])
        self.assertIn('__iter__', dir(mock)) 
开发者ID:war-and-code,项目名称:jawfish,代码行数:23,代码来源:testmock.py

示例11: test_configure_mock

# 需要导入模块: import unittest [as 别名]
# 或者: from unittest import mock [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

示例12: test_side_effect_iterator

# 需要导入模块: import unittest [as 别名]
# 或者: from unittest import mock [as 别名]
def test_side_effect_iterator(self):
        mock = Mock(side_effect=iter([1, 2, 3]))
        self.assertEqual([mock(), mock(), mock()], [1, 2, 3])
        self.assertRaises(StopIteration, mock)

        mock = MagicMock(side_effect=['a', 'b', 'c'])
        self.assertEqual([mock(), mock(), mock()], ['a', 'b', 'c'])
        self.assertRaises(StopIteration, mock)

        mock = Mock(side_effect='ghi')
        self.assertEqual([mock(), mock(), mock()], ['g', 'h', 'i'])
        self.assertRaises(StopIteration, mock)

        class Foo(object):
            pass
        mock = MagicMock(side_effect=Foo)
        self.assertIsInstance(mock(), Foo)

        mock = Mock(side_effect=Iter())
        self.assertEqual([mock(), mock(), mock(), mock()],
                         ['this', 'is', 'an', 'iter'])
        self.assertRaises(StopIteration, mock) 
开发者ID:war-and-code,项目名称:jawfish,代码行数:24,代码来源:testmock.py

示例13: test_side_effect_setting_iterator

# 需要导入模块: import unittest [as 别名]
# 或者: from unittest import mock [as 别名]
def test_side_effect_setting_iterator(self):
        mock = Mock()
        mock.side_effect = iter([1, 2, 3])
        self.assertEqual([mock(), mock(), mock()], [1, 2, 3])
        self.assertRaises(StopIteration, mock)
        side_effect = mock.side_effect
        self.assertIsInstance(side_effect, type(iter([])))

        mock.side_effect = ['a', 'b', 'c']
        self.assertEqual([mock(), mock(), mock()], ['a', 'b', 'c'])
        self.assertRaises(StopIteration, mock)
        side_effect = mock.side_effect
        self.assertIsInstance(side_effect, type(iter([])))

        this_iter = Iter()
        mock.side_effect = this_iter
        self.assertEqual([mock(), mock(), mock(), mock()],
                         ['this', 'is', 'an', 'iter'])
        self.assertRaises(StopIteration, mock)
        self.assertIs(mock.side_effect, this_iter) 
开发者ID:war-and-code,项目名称:jawfish,代码行数:22,代码来源:testmock.py

示例14: test_assert_any_call

# 需要导入模块: import unittest [as 别名]
# 或者: from unittest import mock [as 别名]
def test_assert_any_call(self):
        mock = Mock()
        mock(1, 2)
        mock(a=3)
        mock(1, b=6)

        mock.assert_any_call(1, 2)
        mock.assert_any_call(a=3)
        mock.assert_any_call(1, b=6)

        self.assertRaises(
            AssertionError,
            mock.assert_any_call
        )
        self.assertRaises(
            AssertionError,
            mock.assert_any_call,
            1, 3
        )
        self.assertRaises(
            AssertionError,
            mock.assert_any_call,
            a=4
        ) 
开发者ID:war-and-code,项目名称:jawfish,代码行数:26,代码来源:testmock.py

示例15: test_adding_child_mock

# 需要导入模块: import unittest [as 别名]
# 或者: from unittest import mock [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


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