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


Python mock.return_value方法代码示例

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


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

示例1: test_configure_mock

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

示例2: test_sh_dogetsession_notouch

# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import return_value [as 别名]
def test_sh_dogetsession_notouch(session_handler, monkeypatch, session_key):
    """
    unit tested: do_get_session

    test case:
    - retrieve_session returns a session
    - validate will be called
    - auto_touch is False by default, so skipping its clode block
    - session is returned
    """
    sh = session_handler

    monkeypatch.setattr(sh, '_retrieve_session', lambda x: 'session')

    with mock.patch.object(NativeSessionHandler, 'validate') as sh_validate:
        sh_validate.return_value = None

        result = sh.do_get_session(session_key)
        sh_validate.assert_called_once_with('session', session_key)
        assert result == 'session' 
开发者ID:YosaiProject,项目名称:yosai,代码行数:22,代码来源:test_session_manager.py

示例3: test_sh_validate_expired

# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import return_value [as 别名]
def test_sh_validate_expired(session_handler, mock_session, monkeypatch,
                             session_key):
    """
    unit test:  validate

    test case:
    do_validate raises expired session exception, calling on_expiration and
    raising
    """
    sh = session_handler

    with mock.patch.object(mock_session, 'validate') as ms_dv:
        ms_dv.side_effect = ExpiredSessionException
        with mock.patch.object(NativeSessionHandler, 'on_expiration') as sh_oe:
            sh_oe.return_value = None
            with pytest.raises(ExpiredSessionException):

                sh.validate(mock_session, 'sessionkey123')

                sh_oe.assert_called_once_with(mock_session,
                                              ExpiredSessionException,
                                              'sessionkey123') 
开发者ID:YosaiProject,项目名称:yosai,代码行数:24,代码来源:test_session_manager.py

示例4: test_sh_validate_invalid

# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import return_value [as 别名]
def test_sh_validate_invalid(session_handler, mock_session, monkeypatch,
                             session_key):
    """
    unit test:  validate

    test case:
    do_validate raises expired session exception, calling on_expiration and
    raising
    """
    sh = session_handler

    with mock.patch.object(mock_session, 'validate') as ms_dv:
        ms_dv.side_effect = StoppedSessionException
        with mock.patch.object(NativeSessionHandler, 'on_invalidation') as sh_oe:
            sh_oe.return_value = None
            with pytest.raises(InvalidSessionException):

                sh.validate(mock_session, 'sessionkey123')

                sh_oe.assert_called_once_with(mock_session,
                                              ExpiredSessionException,
                                              'sessionkey123') 
开发者ID:YosaiProject,项目名称:yosai,代码行数:24,代码来源:test_session_manager.py

示例5: test_sh_on_change

# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import return_value [as 别名]
def test_sh_on_change(session_handler, monkeypatch, session_store):
    """
    unit tested:  on_change

    test case:
    passthrough call to session_store.update
    """
    sh = session_handler
    monkeypatch.setattr(sh, 'session_store', session_store)
    with mock.patch.object(sh.session_store, 'update') as ss_up:
        ss_up.return_value = None

        sh.on_change('session')

        ss_up.assert_called_once_with('session')


# ------------------------------------------------------------------------------
# NativeSessionManager
# ------------------------------------------------------------------------------ 
开发者ID:YosaiProject,项目名称:yosai,代码行数:22,代码来源:test_session_manager.py

示例6: test_nsm_set_absolute_timeout

# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import return_value [as 别名]
def test_nsm_set_absolute_timeout(
        default_native_session_manager, monkeypatch, mock_session):
    """
    unit tested: set_absolute_timeout

    test case:
    basic code exercise, passes through and returns
    """
    nsm = default_native_session_manager

    monkeypatch.setattr(nsm, '_lookup_required_session', lambda x: mock_session)

    with mock.patch.object(nsm.session_handler, 'on_change') as mocky:
        mocky.return_value = None
        nsm.set_absolute_timeout('sessionkey123', 'timeout')
        mocky.assert_called_once_with(mock_session)
        assert mock_session.absolute_timeout == 'timeout' 
开发者ID:YosaiProject,项目名称:yosai,代码行数:19,代码来源:test_session_manager.py

示例7: test_nsm_set_internal_attribute

# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import return_value [as 别名]
def test_nsm_set_internal_attribute(
        default_native_session_manager, monkeypatch, mock_session):
    """
    unit tested:  set_internal_attribute

    test case:
    sets an internal_attribute
    """
    nsm = default_native_session_manager
    monkeypatch.setattr(nsm, '_lookup_required_session', lambda x: mock_session)
    with mock.patch.object(nsm.session_handler, 'on_change') as mocky:
        mocky.return_value = None
        with mock.patch.object(mock_session, 'set_internal_attribute') as sia:
            sia.return_value = None

            nsm.set_internal_attribute('sessionkey123',
                                       attribute_key='attr321', value=321)

            sia.assert_called_once_with('attr321', 321)
            mocky.assert_called_once_with(mock_session) 
开发者ID:YosaiProject,项目名称:yosai,代码行数:22,代码来源:test_session_manager.py

示例8: test_nsm_remove_internal_attribute

# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import return_value [as 别名]
def test_nsm_remove_internal_attribute(
        default_native_session_manager, monkeypatch, mock_session):
    """
    unit tested:  remove_internal_attribute

    test case:
    successfully removes an internal_attribute
    """
    nsm = default_native_session_manager
    monkeypatch.setattr(mock_session, 'remove_internal_attribute', lambda x: 'attr1')
    monkeypatch.setattr(nsm, '_lookup_required_session', lambda x: mock_session)
    with mock.patch.object(nsm.session_handler, 'on_change') as mocky:
        mocky.return_value = None

        result = nsm.remove_internal_attribute('sessionkey123',
                                               attribute_key='attr321')

        mocky.assert_called_once_with(mock_session)
        assert result == 'attr1' 
开发者ID:YosaiProject,项目名称:yosai,代码行数:21,代码来源:test_session_manager.py

示例9: test_nsm_set_attribute

# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import return_value [as 别名]
def test_nsm_set_attribute(
        default_native_session_manager, monkeypatch, mock_session):
    """
    unit tested:  set_attribute

    test case:
    sets an attribute
    """
    nsm = default_native_session_manager
    monkeypatch.setattr(nsm, '_lookup_required_session', lambda x: mock_session)
    with mock.patch.object(nsm.session_handler, 'on_change') as mocky:
        mocky.return_value = None
        with mock.patch.object(mock_session, 'set_attribute') as sia:
            sia.return_value = None

            nsm.set_attribute('sessionkey123',
                              attribute_key='attr321', value=321)

            sia.assert_called_once_with('attr321', 321)
            mocky.assert_called_once_with(mock_session) 
开发者ID:YosaiProject,项目名称:yosai,代码行数:22,代码来源:test_session_manager.py

示例10: test_nsm_remove_attribute

# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import return_value [as 别名]
def test_nsm_remove_attribute(
        default_native_session_manager, monkeypatch, mock_session):
    """
    unit tested:  remove_attribute

    test case:
    successfully removes an attribute
    """
    nsm = default_native_session_manager
    monkeypatch.setattr(mock_session, 'remove_attribute', lambda x: 'attr1')
    monkeypatch.setattr(nsm, '_lookup_required_session', lambda x: mock_session)
    with mock.patch.object(nsm.session_handler, 'on_change') as mocky:
        mocky.return_value = None

        result = nsm.remove_attribute('sessionkey123',
                                      attribute_key='attr321')

        mocky.assert_called_once_with(mock_session)
        assert result == 'attr1' 
开发者ID:YosaiProject,项目名称:yosai,代码行数:21,代码来源:test_session_manager.py

示例11: test_nsm_remove_attribute_nothing

# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import return_value [as 别名]
def test_nsm_remove_attribute_nothing(
        default_native_session_manager, monkeypatch, mock_session):
    """
    unit tested:  remove_attribute

    test case:
    removing an attribute that doesn't exist returns None
    """
    nsm = default_native_session_manager
    mock_session.remove_attribute.return_value = None
    monkeypatch.setattr(nsm, '_lookup_required_session', lambda x: mock_session)
    with mock.patch.object(nsm.session_handler, 'on_change') as mocky:
        mocky.return_value = None

        result = nsm.remove_attribute('sessionkey123',
                                      attribute_key='attr321')

        assert result is None 
开发者ID:YosaiProject,项目名称:yosai,代码行数:20,代码来源:test_session_manager.py

示例12: test_constructor

# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import return_value [as 别名]
def test_constructor(self):
        mock = Mock()

        self.assertFalse(mock.called, "called not initialised correctly")
        self.assertEqual(mock.call_count, 0,
                         "call_count not initialised correctly")
        self.assertTrue(is_instance(mock.return_value, Mock),
                        "return_value not initialised correctly")

        self.assertEqual(mock.call_args, None,
                         "call_args not initialised correctly")
        self.assertEqual(mock.call_args_list, [],
                         "call_args_list not initialised correctly")
        self.assertEqual(mock.method_calls, [],
                          "method_calls not initialised correctly")

        # Can't use hasattr for this test as it always returns True on a mock
        self.assertFalse('_items' in mock.__dict__,
                         "default mock should not have '_items' attribute")

        self.assertIsNone(mock._mock_parent,
                          "parent not initialised correctly")
        self.assertIsNone(mock._mock_methods,
                          "methods not initialised correctly")
        self.assertEqual(mock._mock_children, {},
                         "children not initialised incorrectly") 
开发者ID:war-and-code,项目名称:jawfish,代码行数:28,代码来源:testmock.py

示例13: test_return_value_in_constructor

# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import return_value [as 别名]
def test_return_value_in_constructor(self):
        mock = Mock(return_value=None)
        self.assertIsNone(mock.return_value,
                          "return value in constructor not honoured") 
开发者ID:war-and-code,项目名称:jawfish,代码行数:6,代码来源:testmock.py

示例14: test_side_effect

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

示例15: test_reset_mock

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


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