Mock attributes in Python mock?

后端 未结 3 1798
-上瘾入骨i
-上瘾入骨i 2020-12-01 00:51

I\'m having a fairly difficult time using mock in Python:

def method_under_test():
    r = requests.post(\"http://localhost/post\")

    print r         


        
3条回答
  •  -上瘾入骨i
    2020-12-01 01:19

    A compact and simple way to do it is to use new_callable patch's attribute to force patch to use PropertyMock instead of MagicMock to create the mock object. The other arguments passed to patch will be used to create PropertyMock object.

    with patch('requests.post.ok', new_callable=PropertyMock, return_value=True) as mock_post:
        """Your test"""
    

提交回复
热议问题