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


Python Mock.side_effect方法代码示例

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


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

示例1: test_side_effect

# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import side_effect [as 别名]
    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,代码行数:28,代码来源:testmock.py

示例2: test_install_pending_order

# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import side_effect [as 别名]
    def test_install_pending_order(self):
        """Installation order of pending requests are respected"""
        done_callback = Mock()
        done_callback.side_effect = self.done_callback
        done_callback0 = Mock()
        done_callback0.side_effect = self.done_callback
        ordered_progress_callback = Mock()
        progress_callback = Mock()
        progress_callback.side_effect = ordered_progress_callback
        progress_callback0 = Mock()
        progress_callback0.side_effect = ordered_progress_callback
        self.handler.install_bucket(["testpackage"], progress_callback, done_callback)
        self.handler.install_bucket(["testpackage0"], progress_callback0, done_callback0)
        self.wait_for_callback(done_callback)
        self.wait_for_callback(done_callback0)

        self.assertEqual(self.done_callback.call_args_list,
                         [call(RequirementsHandler.RequirementsResult(bucket=['testpackage'], error=None)),
                          call(RequirementsHandler.RequirementsResult(bucket=['testpackage0'], error=None))])
        # we will get progress with 0, 1 (first bucket), 0, 1 (second bucket). So 4 progress signal status change
        current_status = RequirementsHandler.STATUS_DOWNLOADING
        current_status_change_count = 1
        calls = ordered_progress_callback.call_args_list
        for current_call in calls[1:]:
            if current_call[0][0]['step'] != current_status:
                current_status = current_call[0][0]['step']
                current_status_change_count += 1
        self.assertEqual(current_status_change_count, 4)
开发者ID:ubuntu,项目名称:ubuntu-make,代码行数:30,代码来源:test_requirements_handler.py

示例3: test_mock_method

# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import side_effect [as 别名]
    def test_mock_method(self):
        mock = Mock(side_effect=KeyError('foo'))
        values = {'a': 1, 'b': 2, 'c': 3}

        def side_effect(arg):
            return values[arg]

        mock.side_effect = side_effect
        print((mock('a'), mock('b'), mock('c')))

        mock.side_effect = [5, 4, 3, 2, 1]
        print((mock(), mock(), mock(), mock(), mock()))
开发者ID:,项目名称:,代码行数:14,代码来源:

示例4: test_reset_mock

# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import side_effect [as 别名]
    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,代码行数:34,代码来源:testmock.py

示例5: test_reset_mock

# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import side_effect [as 别名]
 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,代码行数:28,代码来源:testmock.py

示例6: test_depaginator_getitem_works

# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import side_effect [as 别名]
def test_depaginator_getitem_works():
    fetcher = Mock()
    fetcher.side_effect = [result]
    paginator = AutoDepaginator(fetcher)
    x = paginator[0]
    assert 'somekey' in x
    assert x['somekey'] == 'somecontent'
    assert paginator[1]['somekey'] == 'another content'
开发者ID:jsbueno,项目名称:drf_depaginator,代码行数:10,代码来源:test_depaginator.py

示例7: test_side_effect_setting_iterator

# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import side_effect [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:johndpope,项目名称:sims4-ai-engine,代码行数:19,代码来源:testmock.py

示例8: test_depaginator_returns_results

# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import side_effect [as 别名]
def test_depaginator_returns_results():
    fetcher = Mock()
    fetcher.side_effect = [result]
    paginator = iter(AutoDepaginator(fetcher))
    r0 = next(paginator)
    fetcher.assert_called_with()
    r1 = next(paginator)
    assert r0 == result['results'][0]
    assert r1 == result['results'][1]
开发者ID:jsbueno,项目名称:drf_depaginator,代码行数:11,代码来源:test_depaginator.py

示例9: test_side_effect_setting_iterator

# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import side_effect [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:GaloisInc,项目名称:echronos,代码行数:21,代码来源:testmock.py

示例10: test_project_init_errors_2

# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import side_effect [as 别名]
def test_project_init_errors_2(project_file_1_fixture):
    op = Mock()
    op.side_effect = raiser(PermissionError)
    op.return_value = project_file_1_fixture
    with patch('srmlf.project.open', op, create=True):
        isfile = Mock()
        isfile.return_value = True
        with patch('os.path.isfile', isfile, create=True):
            with pytest.raises(project.ProjectFileUnreadableException):
                project.Project('test')
开发者ID:gordonzola,项目名称:srmlf,代码行数:12,代码来源:test_project.py

示例11: test_coercer_value_errors_are_ignored

# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import side_effect [as 别名]
    def test_coercer_value_errors_are_ignored(self):
        s = MagicMock()
        s.cell_value.return_value = 'blah'

        c = Mock()
        c.side_effect = ValueError()

        self.assertEqual(safe_cell_str_value(s, 99, 99, c), 'blah')
        self.assertEqual(s.cell_value.call_count, 1)
        self.assertEqual(c.call_count, 1)
开发者ID:18F,项目名称:calc,代码行数:12,代码来源:test_spreadsheet_utils.py

示例12: test_depaginator_works_with_non_paginating_results_and_warns

# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import side_effect [as 别名]
def test_depaginator_works_with_non_paginating_results_and_warns():
    fetcher = Mock()
    fetcher.side_effect = [result['results']]
    fetcher.__name__ = 'test_fetcher'
    with patch('drf_depaginator.logger') as paginator_logger:
        paginator = iter(AutoDepaginator(fetcher))
        response = list(paginator)
        assert len(response) == 2
        assert response == result['results']
        assert any(str(call).startswith('call.warning') for call in paginator_logger.mock_calls), \
            'AutoDepaginator did not log warning about non-compliant API return'
开发者ID:jsbueno,项目名称:drf_depaginator,代码行数:13,代码来源:test_depaginator.py

示例13: test_alignstack_calls_filterfn

# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import side_effect [as 别名]
 def test_alignstack_calls_filterfn(self):
     filterfn = Mock()
     filterfn.side_effect = lambda x: x
     im = test_image()
     up = _up(im)
     down = _down(im)
     alignstack([im, up, down],
                shiftfn=RegisterTranslation(),
                filterfn=filterfn)
     for i, t in enumerate([im, up, down]):
         self.assertIs(filterfn.call_args_list[i][0][0], t)
开发者ID:markotoplak,项目名称:orange-infrared,代码行数:13,代码来源:test_owalignstack.py

示例14: test_install_pending_callback_not_mixed

# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import side_effect [as 别名]
    def test_install_pending_callback_not_mixed(self):
        """Callbacks are separated on pending requests"""
        done_callback = Mock()
        done_callback.side_effect = self.done_callback
        done_callback0 = Mock()
        done_callback0.side_effect = self.done_callback
        global_progress_callback = Mock()
        progress_callback = Mock()
        progress_callback.side_effect = global_progress_callback
        progress_callback0 = Mock()
        progress_callback0.side_effect = global_progress_callback
        self.handler.install_bucket(["testpackage"], progress_callback, done_callback)
        self.handler.install_bucket(["testpackage0"], progress_callback0, done_callback0)
        self.wait_for_callback(done_callback)
        self.wait_for_callback(done_callback0)

        self.assertTrue(progress_callback.call_count < global_progress_callback.call_count)
        self.assertTrue(progress_callback0.call_count < global_progress_callback.call_count)
        self.assertTrue(done_callback.call_count < self.done_callback.call_count)
        self.assertTrue(done_callback0.call_count < self.done_callback.call_count)
开发者ID:ubuntu,项目名称:ubuntu-make,代码行数:22,代码来源:test_requirements_handler.py

示例15: test_depaginator_stops_iteration

# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import side_effect [as 别名]
def test_depaginator_stops_iteration():
    fetcher = Mock()
    fetcher.side_effect = [result, {'next': None, 'count': 3, 'results': [{'somekey': 'yet another content'}]}]
    paginator = iter(AutoDepaginator(fetcher))
    next(paginator)
    next(paginator)
    fetcher.assert_called_once_with()
    next(paginator)
    fetcher.assert_called_with(limit=2, offset=2)
    with pytest.raises(StopIteration):
        next(paginator)
开发者ID:jsbueno,项目名称:drf_depaginator,代码行数:13,代码来源:test_depaginator.py


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