1.假如我想要mock库中date对象,但是我只想mock today()这个方法,就是today()返回我想要的结果,但是其他的date的初始化方法不能有所改变。
from datatime import date with 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 3 mock.other() # return KeyError
3.mock其他module中使用的函数
假如test_fuction_uu.py中要测试fuction_uu,但是fuction_uu调用了myfuction,我想要mock这个myfuction。终于用如下的patch的方式搞定了
test_module.py
def myfuction(): return 2 def fuction_uu(): return myfuction()
@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_value cursor.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