Mocking Functions Using Python Mock

后端 未结 5 1985
清酒与你
清酒与你 2020-12-01 06:08

I am trying to Mock a function (that returns some external content) using the python mock module.

I\'m having some trouble mocking functions that are im

5条回答
  •  暖寄归人
    2020-12-01 06:49

    You have to patch the function where it is being used. In your case that would be in the mymodule module.

    import mymodule
    >>> mymodule.get_content = Mock(return_value="mocked stuff")
    >>> m = mymodule.MyObj()
    >>> m.func()
    "mocked stuff"
    

    There is a reference in the docs here: http://docs.python.org/dev/library/unittest.mock.html#where-to-patch

提交回复
热议问题