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