Python接口測試之moco

  在如今的軟件開發過程當中,特別是app的部分,須要的不少數據以及內容,都是來自server端的API,可是不能保證在客戶端開發的時候,html

api在server端已經開發完成,專門等着前端來調用,理想的狀況是前端在開發的時候,已經有人寫好了接口,直接調用就能夠了,可是這僅僅前端

是理想的狀況,不少時候,現實老是比理想多一層思考和磨難,若是在前端開發的時候,提供api的同窗沒有提供,那麼怎麼辦?等待仍是本身java

先開發,等待確定是愚蠢的作法,那麼本身開發怎麼來解決api提供數據的這個問題,那麼使用mock就能夠很好的解決。python

什麼是mock?git

  mock簡單的理解就是開發在開發的過程當中,須要依賴一部分的接口,可是對方沒有提供或者環境等等狀況,總之是沒有,那麼開發使用mock servergithub

本身來mock數據,方便本身正常的進行開發和對編寫的功能進行自測。json

     在https://github.com/dreamhead/moco地址中能夠下載到moco-runner-0.11.0-standalone.jar,下載下來就是一個jarapi

簡單的編寫一個登陸的,見編寫的login.json字符串:app

[
  {
    "request":
    {
      "method":"post",
      "uri":"/login",
      "json":
      {
        "username":"admin",
        "password":"admin",
        "roleID":22
      }
    },
    "response":
    {
      "json":
      {
        "username":"wuya",
        "userID":22,
        "token":"asdgfhh32456asfgrsfss"
      }
    }
  }
]

在運行命令前,最好讓mock server與編寫的login.json文件是在同一個目錄下,見執行的命令: less

java -jar  moco-runner-0.10.0-standalone.jar http -p 12306 -c login.json

如上的命令中,

  java -jar  moco-runner-0.10.0-standalone.jar是啓動jar,

  moco-runner-0.10.0-standalone.jar 指jar包所在目錄(由於jar就在當前目錄下因此這裏使用的是相對路徑) http表示進行的是http協議,

    -p後面跟的是端口號,這裏端口號指的是12306,

  -c後面跟編寫的json文件,這裏是login.json,見執行如上的命令後出現的信息

 (切記無任何的錯誤信息表示OK,若是有錯誤,慢慢的進行檢查錯誤),見截圖:

OK,下來咱們使用postman來驗證下,咱們mock的登陸接口是否是OK的,見postman中填寫的信息,見截圖:

header爲:

  • Content-Length →63
  • Content-Type →application/json

OK,學習到這裏,咱們使用python編寫一個接口測試用例,來驗證換這個登陸的接口和獲取它的token,見實現的代碼:

# -*- coding:utf-8 -*-

import  unittest
import  requests

class MockLoginTest(unittest.TestCase):
    def setUp(self):
        self.url='http://localhost:12306'

    def tearDown(self):
        pass

    def getUrl(self,path):
        return self.url+path

    def getToken(self):
        '''獲取token'''
        data={
            "username":"admin",
            "password":"admin",
            "roleID":22
        }
        r=requests.post(self.getUrl('/login'),json=data)
        return r.json()['token']

    def test_login(self):
        '''驗證登陸的接口'''
        data={
            "username":"admin",
            "password":"admin",
            "roleID":22
        }
        r=requests.post(self.getUrl('/login'),json=data)
        self.assertEqual(r.status_code,200)
        self.assertEqual(r.json()['username'],'wuya')

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

OK,在上面的python代碼中,實現了對登陸接口的驗證和獲取了它的token

 

咱們先看以前的mock-server部分,以前編寫了一個登陸的mock,具體json文件見以下的內容:

[
  {
    "request":
    {
      "method":"post",
      "uri":"/login",
      "json":
      {
        "username":"admin",
        "password":"admin",
        "roleID":22
      }
    },
    "response":
    {
      "json":
      {
        "username":"wuya",
        "userID":22,
        "token":"asdgfhh32456asfgrsfss"
      }
    }
  }
]

 數據分離:

查看上面的內容,咱們能夠看出,這樣的方式並非那麼的友好,在UI或者接口的自動化測試中,咱們知道在TDD的模式中,對數據進行分離,這樣也是

爲了維護的方便,這樣的目的是爲了後期自動化測試用例的擴展性,和它的易維護性。

那麼下來咱們來對上面的json文件進行修改下,對response部分的數據進行分離下,把它放在一個文件中,這樣就能夠分離了出來

咱們把response的內容從login.json分離到login_response.json,切記login.json與login_response.json務必保持在同一個目錄下,

見login.json的內容:

[
  {
    "request":
    {
      "method":"post",
      "uri":"/login",
      "json":
      {
        "username":"admin",
        "password":"admin",
        "roleID":22
      }
    },
    "response":
    {
      "file":"login_response.json"
    }
  }
]

見login_response.json文件的內容:

{
  "username":"wuya",
  "userID":22,
  "token":"asdgfhh32456asfgrsfss"
}

執行login.json文件後,訪問該接口,看是否OK,見執行login.json文件的命令:

使用postman查看該接口,看是否OK,見以下的截圖:

    下面咱們再進行一個業務,就是輸入一個車牌號,查詢該車牌號的顯示時長,和它的停車費用,直接在login.json文件中完善,見完善後的內容:

[
  {
    "request":
    {
      "method":"post",
      "uri":"/login",
      "json":
      {
        "username":"admin",
        "password":"admin",
        "roleID":22
      }
    },
    "response":
    {
      "file":"login_response.json"
    }
  },

  {
    "request":
    {
      "method":"post",
      "uri":"/parkinside",
      "json":
      {
        "token":"asdgfhh32456asfgrsfss",
        "vpl":"AJ3585"
      }
    },
    "response":
    {
      "file":"parkinside.json"
    }
  }
]
 

 

見parkinside.json文件的內容:

{
  "vplInfo":
  {
    "userID":22,
    "username":"wuya",
    "vpl":"京AJ3585"
  },
  "Parking time long":"20小時18分鐘",
  "Parking fee":"20$"
}

 

見查詢停車時長的接口請求的結果,見以下的postman截圖:

 

下面咱們經過python語言,來對如上的二個接口進行實戰的操做,切記parkinside的接口是在登陸以後才能夠操做的業務,未登陸操做該業務,

返回502無效的token,見實現的代碼:

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

import  unittest
import  requests

class MockTest(unittest.TestCase):
    def setUp(self):
        self.url='http://localhost:12306'
        
    def tearDown(self):
        pass
    
    def test_login(self,url='/login'):
        '''驗證登陸的接口'''
        data={
            "username":"admin",
            "password":"admin",
            "roleID":22
        }
        r=requests.post(self.url+url,json=data)
        self.assertEqual(r.status_code,200)
        self.assertEqual(r.json()['username'],'wuya')
        
    def getToken(self,url='/login'):
        '''登陸成功後獲取token'''
        data={
            "username":"admin",
            "password":"admin",
            "roleID":22
        }
        r=requests.post(self.url+url,json=data)
        return r.json()['token']
    
    def test_parkingside(self,url='/parkinside'):
        '''驗證查詢停車時長接口'''
        data={
            "token":self.getToken(),
            "vpl":"AJ3585"
        }
        r=requests.post(self.url+url,json=data)
        self.assertEqual(r.status_code,200)
        self.assertEqual(r.json()['Parking time long'],u'20小時18分鐘')
        self.assertEqual(r.json()['Parking fee'], u'20$')
        
if __name__=='__main__':
    unittest.main(verbosity=2) 

 

  上面介紹了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
View Code

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來解決了這個問題,那麼咱們來看它的執行順序:

一、找到替換的對象,咱們須要測試的是exists_get_imdir()方法,那麼咱們就須要替換掉rmdir()方法

二、對Mock類進行實例化對象獲得mock,而且設置這個mock的行爲return_value值,也就是mock虛構對象,在測試經過中,咱們虛構return_value爲'success',在測試不經過咱們虛構return_value爲'fail'

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

四、編寫測試代碼,進行斷言,咱們調用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()
相關文章
相關標籤/搜索