python mock 的一些小例子

來源:互聯網
上載者:User

1.假如我想要mock庫中date對象,但是我只想mock today()這個方法,就是today()返回我想要的結果,但是其他的date的初始化方法不能有所改變。

from datatime import datewith patch('mymodule.date') as mock_date:    mock_date.taday.return_value = date(2010,10,8)    mock_date.side_effect = lambda *args, **kw:date(*args, **kw)        assert mymodule.date.today() == date(2010,10,8)    assert mymodule.date(2009,6,8) == date(2009,6,8)


2.配置mock

mock = Mock()attrs = {'method.return_value':3,'other.side_effect':KeyError}mock.configure_mock(**attrs)mock.method()   # return 3mock.other()    # return KeyError

3.mock其他module中使用的函數

假如test_fuction_uu.py中要測試fuction_uu,但是fuction_uu調用了myfuction,我想要mock這個myfuction。終於用如下的patch的方式搞定了

test_module.py

def myfuction():    return 2def fuction_uu():    return myfuction()

test_mock_fuction.py

@patch('test_module.myfuction',MagicMock(return_value = 13))def test_faction_uu():    print  fuction_uu()    if __name__ == '__main__':    test_faction_uu()

4.對於類的patch,注意setup和teardown,在mock的官方文檔中說如果setup中拋出異常,那麼teardown就不會被執行,這可能會造成一個坑。文檔上說用unittest2中的cleanup函數可以避免這個問題。

class MyTest():    def setUp(self):        patcher = patch('package.module.Class')        self.MockClass = patcher.start()        self.addCleanup(patcher.stop)        def test_something(self):        assert package.Module.Class is self.MockClass   

5.patch.object可以有“替換”參數的作用,下面這個

@patch.object(SomeClass,'class_mothod') def test(mock_method):    SomeClass.class_method(3)    mock_method.assert_called_with(3)    test()   # attention: no parameter!


6.mock資料庫操作

mock = Mock()cursor = mock.connection.cursor.return_valuecursor.execute.return_value = ['foo']mock.conection.cursor().execute("select 1")# return ['foo']expected = call.connection.cursor().execute("select 1")mock.mock_calls#return [call.connection.cursor(),call.connection.cursor().ececute('select 1')]mock.mock_calls == expected#return True





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.