简体   繁体   中英

python mock patch how to do it

I have a utility class A. Then a implementation class B which uses a method a() from class A. Now I want to unit test class B. But I want to replace the a() that it uses with a mock one. How can I do that with mock/patch?

Here is what I have tried, none of it worked:

@mock.patch(MODULE_B_PATH.A)
def test(mock_A):
    mock_A.a.return_value = ""
    new B().run()

@mock.patch(MODULE_A_PATH):
def test(mock_A):
    mock_A.a.return_value = ""
    new B().run()

我在这里找到了我想要的所有东西: http : //www.toptal.com/python/an-introduction-to-mocking-in-python

我总是回到作者的 mock网站

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM