python接口測試之mock(十三)


     在之前的博客中介紹了moco的詳細的使用,它主要是基於moco-runner-0.11.0-standalone.jar,通過編寫json的文件

來實現,那么我們現在來看python之中的mock,那么怎么理解mock了,mock翻譯過來就是模擬的意思,也就是說,它

是將測試對象所依存的對象替換為虛構對象的庫,該虛構對象的調用允許事后查看。在python的2.x版本中,它是屬於

第三方的庫,需要單獨的按鈕,在python3.3的版本以后,不需要單獨的安裝,直接導入就可以了,那么我們先看它的安裝

命令,安裝命令為:

                                                        pip  install mock 

見安裝的截圖:

安裝好后,在cmd的命令行中進入到python的環境中,可以直接的導入,見操作的截圖:

在python3.3以上的版本中,因為是標准庫,就不需要單獨的按鈕,直接導入就可以了,見操作的截圖:

     Ok,安裝成功后,下來我們來看mock庫的經常使用的方法以及它的詳細的幫助信息,見獲取的代碼:

#!/usr/bin/env python 
#-*- coding:utf-8 -*-

import  mock

print u'查看modk庫常用的方法:',dir(mock)
print u'查看mock庫詳細的幫助信息:',type(help(mock))

 

見然上的代碼執行后的詳細的信息:

C:\Python27\python.exe D:/git/Python/FullStack/PyUnit/xUnit/mockHelp.py
查看modk庫常用的方法: ['ANY', 'CallableMixin', 'DEFAULT', 'FILTER_DIR', 'MagicMock', 'Mock', 'NonCallableMagicMock', 'NonCallableMock', 'PropertyMock', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', '__version__', '_mock', 'absolute_import', 'call', 'create_autospec', 'mock', 'mock_open', 'patch', 'sentinel', 'version_info']
查看mock庫詳細的幫助信息:Help on package mock:

NAME
    mock

FILE
    c:\python27\lib\site-packages\mock\__init__.py

PACKAGE CONTENTS
    mock
    tests (package)

SUBMODULES
    _mock

CLASSES
    mock.mock.Base(__builtin__.object)
        mock.mock.CallableMixin
            mock.mock.Mock(mock.mock.CallableMixin, mock.mock.NonCallableMock)
                mock.mock.MagicMock(mock.mock.MagicMixin, mock.mock.Mock)
                mock.mock.PropertyMock
        mock.mock.NonCallableMock
            mock.mock.NonCallableMagicMock(mock.mock.MagicMixin, mock.mock.NonCallableMock)
    mock.mock.MagicMixin(__builtin__.object)
        mock.mock.MagicMock(mock.mock.MagicMixin, mock.mock.Mock)
        mock.mock.NonCallableMagicMock(mock.mock.MagicMixin, mock.mock.NonCallableMock)
    
    class CallableMixin(Base)
     |  Method resolution order:
     |      CallableMixin
     |      Base
     |      __builtin__.object
     |  
     |  Methods defined here:
     |  
     |  __call__(_mock_self, *args, **kwargs)
     |  
     |  __init__(self, spec=None, side_effect=None, return_value=sentinel.DEFAULT, wraps=None, name=None, spec_set=None, parent=None, _spec_state=None, _new_name='', _new_parent=None, **kwargs)
     |  
     |  ----------------------------------------------------------------------
     |  Data descriptors inherited from Base:
     |  
     |  __dict__
     |      dictionary for instance variables (if defined)
     |  
     |  __weakref__
     |      list of weak references to the object (if defined)
    
    class MagicMock(MagicMixin, Mock)
     |  MagicMock is a subclass of Mock with default implementations
     |  of most of the magic methods. You can use MagicMock without having to
     |  configure the magic methods yourself.
     |  
     |  If you use the `spec` or `spec_set` arguments then *only* magic
     |  methods that exist in the spec will be created.
     |  
     |  Attributes and the return value of a `MagicMock` will also be `MagicMocks`.
     |  
     |  Method resolution order:
     |      MagicMock
     |      MagicMixin
     |      Mock
     |      CallableMixin
     |      NonCallableMock
     |      Base
     |      __builtin__.object
     |  
     |  Methods defined here:
     |  
     |  mock_add_spec(self, spec, spec_set=False)
     |      Add a spec to a mock. `spec` can either be an object or a
     |      list of strings. Only attributes on the `spec` can be fetched as
     |      attributes from the mock.
     |      
     |      If `spec_set` is True then only attributes on the spec can be set.
     |  
     |  ----------------------------------------------------------------------
     |  Methods inherited from MagicMixin:
     |  
     |  __init__(self, *args, **kw)
     |  
     |  ----------------------------------------------------------------------
     |  Data descriptors inherited from MagicMixin:
     |  
     |  __dict__
     |      dictionary for instance variables (if defined)
     |  
     |  __weakref__
     |      list of weak references to the object (if defined)
     |  
     |  ----------------------------------------------------------------------
     |  Methods inherited from CallableMixin:
     |  
     |  __call__(_mock_self, *args, **kwargs)
     |  
     |  ----------------------------------------------------------------------
     |  Methods inherited from NonCallableMock:
     |  
     |  __delattr__(self, name)
     |  
     |  __dir__(self)
     |      Filter the output of `dir(mock)` to only useful members.
     |  
     |  __getattr__(self, name)
     |  
     |  __repr__(self)
     |  
     |  __setattr__(self, name, value)
     |  
     |  assert_any_call(self, *args, **kwargs)
     |      assert the mock has been called with the specified arguments.
     |      
     |      The assert passes if the mock has *ever* been called, unlike
     |      `assert_called_with` and `assert_called_once_with` that only pass if
     |      the call is the most recent one.
     |  
     |  assert_called(_mock_self)
     |      assert that the mock was called at least once
     |  
     |  assert_called_once(_mock_self)
     |      assert that the mock was called only once.
     |  
     |  assert_called_once_with(_mock_self, *args, **kwargs)
     |      assert that the mock was called exactly once and with the specified
     |      arguments.
     |  
     |  assert_called_with(_mock_self, *args, **kwargs)
     |      assert that the mock was called with the specified arguments.
     |      
     |      Raises an AssertionError if the args and keyword args passed in are
     |      different to the last call to the mock.
     |  
     |  assert_has_calls(self, calls, any_order=False)
     |      assert the mock has been called with the specified calls.
     |      The `mock_calls` list is checked for the calls.
     |      
     |      If `any_order` is False (the default) then the calls must be
     |      sequential. There can be extra calls before or after the
     |      specified calls.
     |      
     |      If `any_order` is True then the calls can be in any order, but
     |      they must all appear in `mock_calls`.
     |  
     |  assert_not_called(_mock_self)
     |      assert that the mock was never called.
     |  
     |  attach_mock(self, mock, attribute)
     |      Attach a mock as an attribute of this one, replacing its name and
     |      parent. Calls to the attached mock will be recorded in the
     |      `method_calls` and `mock_calls` attributes of this one.
     |  
     |  configure_mock(self, **kwargs)
     |      Set attributes on the mock through keyword arguments.
     |      
     |      Attributes plus return values and side effects can be set on child
     |      mocks using standard dot notation and unpacking a dictionary in the
     |      method call:
     |      
     |      >>> attrs = {'method.return_value': 3, 'other.side_effect': KeyError}
     |      >>> mock.configure_mock(**attrs)
     |  
     |  reset_mock(self, visited=None)
     |      Restore the mock object to its initial state.
     |  
     |  ----------------------------------------------------------------------
     |  Static methods inherited from NonCallableMock:
     |  
     |  __new__(cls, *args, **kw)
     |  
     |  ----------------------------------------------------------------------
     |  Data descriptors inherited from NonCallableMock:
     |  
     |  __class__
     |  
     |  call_args
     |  
     |  call_args_list
     |  
     |  call_count
     |  
     |  called
     |  
     |  mock_calls
     |  
     |  return_value
     |  
     |  side_effect
    
    class Mock(CallableMixin, NonCallableMock)
     |  Create a new `Mock` object. `Mock` takes several optional arguments
     |  that specify the behaviour of the Mock object:
     |  
     |  * `spec`: This can be either a list of strings or an existing object (a
     |    class or instance) that acts as the specification for the mock object. If
     |    you pass in an object then a list of strings is formed by calling dir on
     |    the object (excluding unsupported magic attributes and methods). Accessing
     |    any attribute not in this list will raise an `AttributeError`.
     |  
     |    If `spec` is an object (rather than a list of strings) then
     |    `mock.__class__` returns the class of the spec object. This allows mocks
     |    to pass `isinstance` tests.
     |  
     |  * `spec_set`: A stricter variant of `spec`. If used, attempting to *set*
     |    or get an attribute on the mock that isn't on the object passed as
     |    `spec_set` will raise an `AttributeError`.
     |  
     |  * `side_effect`: A function to be called whenever the Mock is called. See
     |    the `side_effect` attribute. Useful for raising exceptions or
     |    dynamically changing return values. The function is called with the same
     |    arguments as the mock, and unless it returns `DEFAULT`, the return
     |    value of this function is used as the return value.
     |  
     |    Alternatively `side_effect` can be an exception class or instance. In
     |    this case the exception will be raised when the mock is called.
     |  
     |    If `side_effect` is an iterable then each call to the mock will return
     |    the next value from the iterable. If any of the members of the iterable
     |    are exceptions they will be raised instead of returned.
     |  
     |  * `return_value`: The value returned when the mock is called. By default
     |    this is a new Mock (created on first access). See the
     |    `return_value` attribute.
     |  
     |  * `wraps`: Item for the mock object to wrap. If `wraps` is not None then
     |    calling the Mock will pass the call through to the wrapped object
     |    (returning the real result). Attribute access on the mock will return a
     |    Mock object that wraps the corresponding attribute of the wrapped object
     |    (so attempting to access an attribute that doesn't exist will raise an
     |    `AttributeError`).
     |  
     |    If the mock has an explicit `return_value` set then calls are not passed
     |    to the wrapped object and the `return_value` is returned instead.
     |  
     |  * `name`: If the mock has a name then it will be used in the repr of the
     |    mock. This can be useful for debugging. The name is propagated to child
     |    mocks.
     |  
     |  Mocks can also be called with arbitrary keyword arguments. These will be
     |  used to set attributes on the mock after it is created.
     |  
     |  Method resolution order:
     |      Mock
     |      CallableMixin
     |      NonCallableMock
     |      Base
     |      __builtin__.object
     |  
     |  Methods inherited from CallableMixin:
     |  
     |  __call__(_mock_self, *args, **kwargs)
     |  
     |  __init__(self, spec=None, side_effect=None, return_value=sentinel.DEFAULT, wraps=None, name=None, spec_set=None, parent=None, _spec_state=None, _new_name='', _new_parent=None, **kwargs)
     |  
     |  ----------------------------------------------------------------------
     |  Methods inherited from NonCallableMock:
     |  
     |  __delattr__(self, name)
     |  
     |  __dir__(self)
     |      Filter the output of `dir(mock)` to only useful members.
     |  
     |  __getattr__(self, name)
     |  
     |  __repr__(self)
     |  
     |  __setattr__(self, name, value)
     |  
     |  assert_any_call(self, *args, **kwargs)
     |      assert the mock has been called with the specified arguments.
     |      
     |      The assert passes if the mock has *ever* been called, unlike
     |      `assert_called_with` and `assert_called_once_with` that only pass if
     |      the call is the most recent one.
     |  
     |  assert_called(_mock_self)
     |      assert that the mock was called at least once
     |  
     |  assert_called_once(_mock_self)
     |      assert that the mock was called only once.
     |  
     |  assert_called_once_with(_mock_self, *args, **kwargs)
     |      assert that the mock was called exactly once and with the specified
     |      arguments.
     |  
     |  assert_called_with(_mock_self, *args, **kwargs)
     |      assert that the mock was called with the specified arguments.
     |      
     |      Raises an AssertionError if the args and keyword args passed in are
     |      different to the last call to the mock.
     |  
     |  assert_has_calls(self, calls, any_order=False)
     |      assert the mock has been called with the specified calls.
     |      The `mock_calls` list is checked for the calls.
     |      
     |      If `any_order` is False (the default) then the calls must be
     |      sequential. There can be extra calls before or after the
     |      specified calls.
     |      
     |      If `any_order` is True then the calls can be in any order, but
     |      they must all appear in `mock_calls`.
     |  
     |  assert_not_called(_mock_self)
     |      assert that the mock was never called.
     |  
     |  attach_mock(self, mock, attribute)
     |      Attach a mock as an attribute of this one, replacing its name and
     |      parent. Calls to the attached mock will be recorded in the
     |      `method_calls` and `mock_calls` attributes of this one.
     |  
     |  configure_mock(self, **kwargs)
     |      Set attributes on the mock through keyword arguments.
     |      
     |      Attributes plus return values and side effects can be set on child
     |      mocks using standard dot notation and unpacking a dictionary in the
     |      method call:
     |      
     |      >>> attrs = {'method.return_value': 3, 'other.side_effect': KeyError}
     |      >>> mock.configure_mock(**attrs)
     |  
     |  mock_add_spec(self, spec, spec_set=False)
     |      Add a spec to a mock. `spec` can either be an object or a
     |      list of strings. Only attributes on the `spec` can be fetched as
     |      attributes from the mock.
     |      
     |      If `spec_set` is True then only attributes on the spec can be set.
     |  
     |  reset_mock(self, visited=None)
     |      Restore the mock object to its initial state.
     |  
     |  ----------------------------------------------------------------------
     |  Static methods inherited from NonCallableMock:
     |  
     |  __new__(cls, *args, **kw)
     |  
     |  ----------------------------------------------------------------------
     |  Data descriptors inherited from NonCallableMock:
     |  
     |  __class__
     |  
     |  call_args
     |  
     |  call_args_list
     |  
     |  call_count
     |  
     |  called
     |  
     |  mock_calls
     |  
     |  return_value
     |  
     |  side_effect
     |  
     |  ----------------------------------------------------------------------
     |  Data descriptors inherited from Base:
     |  
     |  __dict__
     |      dictionary for instance variables (if defined)
     |  
     |  __weakref__
     |      list of weak references to the object (if defined)
    
    class NonCallableMagicMock(MagicMixin, NonCallableMock)
     |  A version of `MagicMock` that isn't callable.
     |  
     |  Method resolution order:
     |      NonCallableMagicMock
     |      MagicMixin
     |      NonCallableMock
     |      Base
     |      __builtin__.object
     |  
     |  Methods defined here:
     |  
     |  mock_add_spec(self, spec, spec_set=False)
     |      Add a spec to a mock. `spec` can either be an object or a
     |      list of strings. Only attributes on the `spec` can be fetched as
     |      attributes from the mock.
     |      
     |      If `spec_set` is True then only attributes on the spec can be set.
     |  
     |  ----------------------------------------------------------------------
     |  Methods inherited from MagicMixin:
     |  
     |  __init__(self, *args, **kw)
     |  
     |  ----------------------------------------------------------------------
     |  Data descriptors inherited from MagicMixin:
     |  
     |  __dict__
     |      dictionary for instance variables (if defined)
     |  
     |  __weakref__
     |      list of weak references to the object (if defined)
     |  
     |  ----------------------------------------------------------------------
     |  Methods inherited from NonCallableMock:
     |  
     |  __delattr__(self, name)
     |  
     |  __dir__(self)
     |      Filter the output of `dir(mock)` to only useful members.
     |  
     |  __getattr__(self, name)
     |  
     |  __repr__(self)
     |  
     |  __setattr__(self, name, value)
     |  
     |  assert_any_call(self, *args, **kwargs)
     |      assert the mock has been called with the specified arguments.
     |      
     |      The assert passes if the mock has *ever* been called, unlike
     |      `assert_called_with` and `assert_called_once_with` that only pass if
     |      the call is the most recent one.
     |  
     |  assert_called(_mock_self)
     |      assert that the mock was called at least once
     |  
     |  assert_called_once(_mock_self)
     |      assert that the mock was called only once.
     |  
     |  assert_called_once_with(_mock_self, *args, **kwargs)
     |      assert that the mock was called exactly once and with the specified
     |      arguments.
     |  
     |  assert_called_with(_mock_self, *args, **kwargs)
     |      assert that the mock was called with the specified arguments.
     |      
     |      Raises an AssertionError if the args and keyword args passed in are
     |      different to the last call to the mock.
     |  
     |  assert_has_calls(self, calls, any_order=False)
     |      assert the mock has been called with the specified calls.
     |      The `mock_calls` list is checked for the calls.
     |      
     |      If `any_order` is False (the default) then the calls must be
     |      sequential. There can be extra calls before or after the
     |      specified calls.
     |      
     |      If `any_order` is True then the calls can be in any order, but
     |      they must all appear in `mock_calls`.
     |  
     |  assert_not_called(_mock_self)
     |      assert that the mock was never called.
     |  
     |  attach_mock(self, mock, attribute)
     |      Attach a mock as an attribute of this one, replacing its name and
     |      parent. Calls to the attached mock will be recorded in the
     |      `method_calls` and `mock_calls` attributes of this one.
     |  
     |  configure_mock(self, **kwargs)
     |      Set attributes on the mock through keyword arguments.
     |      
     |      Attributes plus return values and side effects can be set on child
     |      mocks using standard dot notation and unpacking a dictionary in the
     |      method call:
     |      
     |      >>> attrs = {'method.return_value': 3, 'other.side_effect': KeyError}
     |      >>> mock.configure_mock(**attrs)
     |  
     |  reset_mock(self, visited=None)
     |      Restore the mock object to its initial state.
     |  
     |  ----------------------------------------------------------------------
     |  Static methods inherited from NonCallableMock:
     |  
     |  __new__(cls, *args, **kw)
     |  
     |  ----------------------------------------------------------------------
     |  Data descriptors inherited from NonCallableMock:
     |  
     |  __class__
     |  
     |  call_args
     |  
     |  call_args_list
     |  
     |  call_count
     |  
     |  called
     |  
     |  mock_calls
     |  
     |  return_value
     |  
     |  side_effect
    
    class NonCallableMock(Base)
     |  A non-callable version of `Mock`
     |  
     |  Method resolution order:
     |      NonCallableMock
     |      Base
     |      __builtin__.object
     |  
     |  Methods defined here:
     |  
     |  __delattr__(self, name)
     |  
     |  __dir__(self)
     |      Filter the output of `dir(mock)` to only useful members.
     |  
     |  __getattr__(self, name)
     |  
     |  __init__(self, spec=None, wraps=None, name=None, spec_set=None, parent=None, _spec_state=None, _new_name='', _new_parent=None, _spec_as_instance=False, _eat_self=None, unsafe=False, **kwargs)
     |  
     |  __repr__(self)
     |  
     |  __setattr__(self, name, value)
     |  
     |  assert_any_call(self, *args, **kwargs)
     |      assert the mock has been called with the specified arguments.
     |      
     |      The assert passes if the mock has *ever* been called, unlike
     |      `assert_called_with` and `assert_called_once_with` that only pass if
     |      the call is the most recent one.
     |  
     |  assert_called(_mock_self)
     |      assert that the mock was called at least once
     |  
     |  assert_called_once(_mock_self)
     |      assert that the mock was called only once.
     |  
     |  assert_called_once_with(_mock_self, *args, **kwargs)
     |      assert that the mock was called exactly once and with the specified
     |      arguments.
     |  
     |  assert_called_with(_mock_self, *args, **kwargs)
     |      assert that the mock was called with the specified arguments.
     |      
     |      Raises an AssertionError if the args and keyword args passed in are
     |      different to the last call to the mock.
     |  
     |  assert_has_calls(self, calls, any_order=False)
     |      assert the mock has been called with the specified calls.
     |      The `mock_calls` list is checked for the calls.
     |      
     |      If `any_order` is False (the default) then the calls must be
     |      sequential. There can be extra calls before or after the
     |      specified calls.
     |      
     |      If `any_order` is True then the calls can be in any order, but
     |      they must all appear in `mock_calls`.
     |  
     |  assert_not_called(_mock_self)
     |      assert that the mock was never called.
     |  
     |  attach_mock(self, mock, attribute)
     |      Attach a mock as an attribute of this one, replacing its name and
     |      parent. Calls to the attached mock will be recorded in the
     |      `method_calls` and `mock_calls` attributes of this one.
     |  
     |  configure_mock(self, **kwargs)
     |      Set attributes on the mock through keyword arguments.
     |      
     |      Attributes plus return values and side effects can be set on child
     |      mocks using standard dot notation and unpacking a dictionary in the
     |      method call:
     |      
     |      >>> attrs = {'method.return_value': 3, 'other.side_effect': KeyError}
     |      >>> mock.configure_mock(**attrs)
     |  
     |  mock_add_spec(self, spec, spec_set=False)
     |      Add a spec to a mock. `spec` can either be an object or a
     |      list of strings. Only attributes on the `spec` can be fetched as
     |      attributes from the mock.
     |      
     |      If `spec_set` is True then only attributes on the spec can be set.
     |  
     |  reset_mock(self, visited=None)
     |      Restore the mock object to its initial state.
     |  
     |  ----------------------------------------------------------------------
     |  Static methods defined here:
     |  
     |  __new__(cls, *args, **kw)
     |  
     |  ----------------------------------------------------------------------
     |  Data descriptors defined here:
     |  
     |  __class__
     |  
     |  call_args
     |  
     |  call_args_list
     |  
     |  call_count
     |  
     |  called
     |  
     |  mock_calls
     |  
     |  return_value
     |  
     |  side_effect
     |  
     |  ----------------------------------------------------------------------
     |  Data descriptors inherited from Base:
     |  
     |  __dict__
     |      dictionary for instance variables (if defined)
     |  
     |  __weakref__
     |      list of weak references to the object (if defined)
    
    class PropertyMock(Mock)
     |  A mock intended to be used as a property, or other descriptor, on a class.
     |  `PropertyMock` provides `__get__` and `__set__` methods so you can specify
     |  a return value when it is fetched.
     |  
     |  Fetching a `PropertyMock` instance from an object calls the mock, with
     |  no args. Setting it calls the mock with the value being set.
     |  
     |  Method resolution order:
     |      PropertyMock
     |      Mock
     |      CallableMixin
     |      NonCallableMock
     |      Base
     |      __builtin__.object
     |  
     |  Methods defined here:
     |  
     |  __get__(self, obj, obj_type)
     |  
     |  __set__(self, obj, val)
     |  
     |  ----------------------------------------------------------------------
     |  Methods inherited from CallableMixin:
     |  
     |  __call__(_mock_self, *args, **kwargs)
     |  
     |  __init__(self, spec=None, side_effect=None, return_value=sentinel.DEFAULT, wraps=None, name=None, spec_set=None, parent=None, _spec_state=None, _new_name='', _new_parent=None, **kwargs)
     |  
     |  ----------------------------------------------------------------------
     |  Methods inherited from NonCallableMock:
     |  
     |  __delattr__(self, name)
     |  
     |  __dir__(self)
     |      Filter the output of `dir(mock)` to only useful members.
     |  
     |  __getattr__(self, name)
     |  
     |  __repr__(self)
     |  
     |  __setattr__(self, name, value)
     |  
     |  assert_any_call(self, *args, **kwargs)
     |      assert the mock has been called with the specified arguments.
     |      
     |      The assert passes if the mock has *ever* been called, unlike
     |      `assert_called_with` and `assert_called_once_with` that only pass if
     |      the call is the most recent one.
     |  
     |  assert_called(_mock_self)
     |      assert that the mock was called at least once
     |  
     |  assert_called_once(_mock_self)
     |      assert that the mock was called only once.
     |  
     |  assert_called_once_with(_mock_self, *args, **kwargs)
     |      assert that the mock was called exactly once and with the specified
     |      arguments.
     |  
     |  assert_called_with(_mock_self, *args, **kwargs)
     |      assert that the mock was called with the specified arguments.
     |      
     |      Raises an AssertionError if the args and keyword args passed in are
     |      different to the last call to the mock.
     |  
     |  assert_has_calls(self, calls, any_order=False)
     |      assert the mock has been called with the specified calls.
     |      The `mock_calls` list is checked for the calls.
     |      
     |      If `any_order` is False (the default) then the calls must be
     |      sequential. There can be extra calls before or after the
     |      specified calls.
     |      
     |      If `any_order` is True then the calls can be in any order, but
     |      they must all appear in `mock_calls`.
     |  
     |  assert_not_called(_mock_self)
     |      assert that the mock was never called.
     |  
     |  attach_mock(self, mock, attribute)
     |      Attach a mock as an attribute of this one, replacing its name and
     |      parent. Calls to the attached mock will be recorded in the
     |      `method_calls` and `mock_calls` attributes of this one.
     |  
     |  configure_mock(self, **kwargs)
     |      Set attributes on the mock through keyword arguments.
     |      
     |      Attributes plus return values and side effects can be set on child
     |      mocks using standard dot notation and unpacking a dictionary in the
     |      method call:
     |      
     |      >>> attrs = {'method.return_value': 3, 'other.side_effect': KeyError}
     |      >>> mock.configure_mock(**attrs)
     |  
     |  mock_add_spec(self, spec, spec_set=False)
     |      Add a spec to a mock. `spec` can either be an object or a
     |      list of strings. Only attributes on the `spec` can be fetched as
     |      attributes from the mock.
     |      
     |      If `spec_set` is True then only attributes on the spec can be set.
     |  
     |  reset_mock(self, visited=None)
     |      Restore the mock object to its initial state.
     |  
     |  ----------------------------------------------------------------------
     |  Static methods inherited from NonCallableMock:
     |  
     |  __new__(cls, *args, **kw)
     |  
     |  ----------------------------------------------------------------------
     |  Data descriptors inherited from NonCallableMock:
     |  
     |  __class__
     |  
     |  call_args
     |  
     |  call_args_list
     |  
     |  call_count
     |  
     |  called
     |  
     |  mock_calls
     |  
     |  return_value
     |  
     |  side_effect
     |  
     |  ----------------------------------------------------------------------
     |  Data descriptors inherited from Base:
     |  
     |  __dict__
     |      dictionary for instance variables (if defined)
     |  
     |  __weakref__
     |      list of weak references to the object (if defined)

FUNCTIONS
    create_autospec(spec, spec_set=False, instance=False, _parent=None, _name=None, **kwargs)
        Create a mock object using another object as a spec. Attributes on the
        mock will use the corresponding attribute on the `spec` object as their
        spec.
        
        Functions or methods being mocked will have their arguments checked
        to check that they are called with the correct signature.
        
        If `spec_set` is True then attempting to set attributes that don't exist
        on the spec object will raise an `AttributeError`.
        
        If a class is used as a spec then the return value of the mock (the
        instance of the class) will have the same spec. You can use a class as the
        spec for an instance object by passing `instance=True`. The returned mock
        will only be callable if instances of the mock are callable.
        
        `create_autospec` also takes arbitrary keyword arguments that are passed to
        the constructor of the created mock.
    
    mock_open(mock=None, read_data='')
        A helper function to create a mock to replace the use of `open`. It works
        for `open` called directly or used as a context manager.
        
        The `mock` argument is the mock object to configure. If `None` (the
        default) then a `MagicMock` will be created for you, with the API limited
        to methods or attributes available on standard file handles.
        
        `read_data` is a string for the `read` methoddline`, and `readlines` of the
        file handle to return.  This is an empty string by default.
    
    patch(target, new=sentinel.DEFAULT, spec=None, create=False, spec_set=None, autospec=None, new_callable=None, **kwargs)
        `patch` acts as a function decorator, class decorator or a context
        manager. Inside the body of the function or with statement, the `target`
        is patched with a `new` object. When the function/with statement exits
        the patch is undone.
        
        If `new` is omitted, then the target is replaced with a
        `MagicMock`. If `patch` is used as a decorator and `new` is
        omitted, the created mock is passed in as an extra argument to the
        decorated function. If `patch` is used as a context manager the created
        mock is returned by the context manager.
        
        `target` should be a string in the form `'package.module.ClassName'`. The
        `target` is imported and the specified object replaced with the `new`
        object, so the `target` must be importable from the environment you are
        calling `patch` from. The target is imported when the decorated function
        is executed, not at decoration time.
        
        The `spec` and `spec_set` keyword arguments are passed to the `MagicMock`
        if patch is creating one for you.
        
        In addition you can pass `spec=True` or `spec_set=True`, which causes
        patch to pass in the object being mocked as the spec/spec_set object.
        
        `new_callable` allows you to specify a different class, or callable object,
        that will be called to create the `new` object. By default `MagicMock` is
        used.
        
        A more powerful form of `spec` is `autospec`. If you set `autospec=True`
        then the mock will be created with a spec from the object being replaced.
        All attributes of the mock will also have the spec of the corresponding
        attribute of the object being replaced. Methods and functions being
        mocked will have their arguments checked and will raise a `TypeError` if
        they are called with the wrong signature. For mocks replacing a class,
        their return value (the 'instance') will have the same spec as the class.
        
        Instead of `autospec=True` you can pass `autospec=some_object` to use an
        arbitrary object as the spec instead of the one being replaced.
        
        By default `patch` will fail to replace attributes that don't exist. If
        you pass in `create=True`, and the attribute doesn't exist, patch will
        create the attribute for you when the patched function is called, and
        delete it again afterwards. This is useful for writing tests against
        attributes that your production code creates at runtime. It is off by
        default because it can be dangerous. With it switched on you can write
        passing tests against APIs that don't actually exist!
        
        Patch can be used as a `TestCase` class decorator. It works by
        decorating each test method in the class. This reduces the boilerplate
        code when your test methods share a common patchings set. `patch` finds
        tests by looking for method names that start with `patch.TEST_PREFIX`.
        By default this is `test`, which matches the way `unittest` finds tests.
        You can specify an alternative prefix by setting `patch.TEST_PREFIX`.
        
        Patch can be used as a context manager, with the with statement. Here the
        patching applies to the indented block after the with statement. If you
        use "as" then the patched object will be bound to the name after the
        "as"; very useful if `patch` is creating a mock object for you.
        
        `patch` takes arbitrary keyword arguments. These will be passed to
        the `Mock` (or `new_callable`) on construction.
        
        `patch.dict(...)`, `patch.multiple(...)` and `patch.object(...)` are
        available for alternate use-cases.

DATA
    ANY = <ANY>
    DEFAULT = sentinel.DEFAULT
    FILTER_DIR = True
    __all__ = ('__version__', 'version_info', 'Mock', 'MagicMock', 'patch'...
    __version__ = '2.0.0'
    call = call
    sentinel = <mock.mock._Sentinel object>
    version_info = (2, 0, 0, 'final', 0)

VERSION
    2.0.0


<type 'NoneType'>

Process finished with exit code 0

 

mock詳細的學習文檔可以參考官方的文檔,官方地址:

                                                 http://www.voidspace.org.uk/python/mock/mock.html

       下面我們首先來看一個例子,來引入mock,來查看它解決什么問題,以及我們為什么需要mock,知道了這些

以后,我們再詳細的探討mock它詳細的使用。我們編寫如下的一個方法,它要實現的是刪除一個C盤下的文件夾,而

該文件夾就是Windows文件夾,見該代碼:

#!/usr/bin/env python 
#-*- coding:utf-8 -*-


import  mock
import  os

class Remove(object):

    def rmdir(self,path='c:/Windows'):
        os.rmdir(path)

 

如果我們要測試rmdir()的方法,那么執行的就是我們每測試一次,就得刪除一次文件夾,這樣我們的測試結果是

通過的,另外有這么幾個點需要考慮,第一就是該文件夾是否可刪除,如果刪除,引起系統出問題如何處理?第二

是刪除該文件夾的時候,是否已經創建了,難道我們每次刪除一次,就得判斷是否存在,是否創建,再做刪除的操作?

很顯然這樣的一個測試過程,最難的不是第二個問題,而是第一個文件,C盤下的Windows文件夾可以正常的刪除嗎?

可以做刪除的操作嗎?答案是不可以的,因為我們的系統盤就是C盤下,而Windows文件夾里面儲存了很多的系統文件,

絕對不允許刪除,那么我們如何測試了,這個時候,就需要mock,也就是模擬,我們再寫一個方法,模擬rmdir()方法

的執行,見更新后的代碼:

#!/usr/bin/env python 
#-*- coding:utf-8 -*-


import  mock
import  os

class Remove(object):

    def rmdir(self,path='c:/Windows'):
        r=os.rmdir(path)
        if r==None :
            return u'刪除成功'
        else:
            return u'Sorry,刪除失敗'

    def exists_get_rmdir(self):
        return self.rmdir()

 

見測試的代碼:

#!/usr/bin/env python 
#-*- coding:utf-8 -*-


import  mock
import  os
import  unittest


class Remove(object):

    def rmdir(self,path='c:/log'):
        r=os.rmdir(path)
        if r==None :
            return 'success'
        else:
            return 'fail'

    def exists_get_rmdir(self):
        return self.rmdir()


class MockTest(unittest.TestCase):
    def setUp(self):
        self.r=Remove()


    def tearDown(self):
        pass

    def test_success_rmdir(self):
        '''
        刪除目錄成功
        :return:
        '''
        success_path=mock.Mock(return_value='success')
        self.r.rmdir=success_path
        self.assertEqual(self.r.exists_get_rmdir(),'success')

    def test_fail_rmdir(self):
        '''
        刪除目錄失敗
        :return:
        '''
        fail_path=mock.Mock(return_value='fail')
        self.r.rmdir=fail_path
        self.assertEqual(self.r.exists_get_rmdir(),'fail')

if __name__=='__main__':
    unittest.main(verbosity=2)

 

執行如上的代碼的時候,我們就不需要考慮是否存在該文件夾,以及該文件夾是否可正常的刪除,我們完全使用mock

來解決了這個問題,那么我們來看它的執行順序:

1、找到替換的對象,我們需要測試的是exists_get_imdir()方法,那么我們就需要替換掉rmdir()方法

2、對Mock類進行實例化對象得到mock,並且設置這個mock的行為return_value值,也就是mock虛構對象,在測試通過中,我們

虛構return_value為'success',在測試不通過我們虛構return_value為'fail'

3、使用mock對象我們想替換的方法rmdir(),這樣我們就替換到了self.r.rmdir

4、編寫測試代碼,進行斷言,我們調用self.r.exists_get_imdir()方法,並且期望它的返回值與我們預期的結果一致(不管是成功的

還是失敗的)

      使用mock虛構對象,比如我們案例使用的Mock(return_value='success'),它是一個什么樣的形式了,如果我們使用函數怎么樣

編寫,下來我們詳細的來實現這個過程:

見如下模擬類當中的方法,見實現的案例代碼:

#!/usr/bin/env python 
#-*- coding:utf-8 -*-

import  mock

mock=mock.Mock()
mock.info.return_value='Hello Mock'
print mock.info()

class F:
    def info(self):
        return 'Hello Mock'

f=F()
print f.info()

 

     關於mock的庫下來的博客會繼續更新和介紹,如您對我寫的內容感興趣,可掃描如下的二維碼關注我的公眾號,謝謝!!!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM