开发者

Mock an object in a method that is not a parameter in python

开发者 https://www.devze.com 2023-03-22 08:45 出处:网络
(i\'m a python newbie) I was looking at: Mocking out objects and methods And I was wondering if i can replace an object in a python method which is not passed as a parameter, lets say my method is l

(i'm a python newbie)

I was looking at: Mocking out objects and methods

And I was wondering if i can replace an object in a python method which is not passed as a parameter, lets say my method is like this:

def mypymethod(param1)
  myresult = os.path.something
  return myresult

and i wish to test mypymethod however i wish os.path at th开发者_高级运维is case to return "yourmockresult" when i call it from my test method

anyway i can do this? thanks!!


You should use Michael Foord's Mock module for the same.

From the documentation:

>>> from mock import Mock
>>> real = ProductionClass()
>>> real.method = Mock(return_value=3)
>>> real.method(3, 4, 5, key='value')
3
>>> real.method.assert_called_with(3, 4, 5, key='value')
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号