pytest mock 单测
mock一般与patch联用
demo.py:
def get_sum(x, y):
pass
--------------------------------------------------------------------
import demo
from unittest import mock
def test_fun():
mock_get_sum = mock.patch('demo.get_sum', return_value=20)
mock_get_sum.start()
result = demo.get_sum()
mock_get_sum.stop()
print(result)
--------------------------------------------------------------------
test_fun() # -> 此时返回值为20
可能会遇到报错 mock.patch AttributeError: 'function' object has no attribute
此时看一下闭包问题,包括 patch后的 函数路径,有没有用单引号引起来

浙公网安备 33010602011771号