我正在尝试为函数编写UT,同时模拟其他函数。两者位于同一模块中。似乎unittest.mock.补丁()调用传递没有错误,但没有模拟任何东西,因此示例代码中出现异常。所以,我的问题是:如何用unittest.mock.补丁?你知道吗
import unittest
import unittest.mock
def foo():
raise Exception("not implemented")
def bar():
foo()
class UT(unittest.TestCase):
def testBar(self):
with unittest.mock.patch('mock_test.foo',spec=True) as mock_foo:
bar() # <- original foo is called here
unittest.main()
下面是一种使用
unittest.mock.patch
并修补foo
返回的方法。参见示例中的注释:输出:
有关更多信息,请访问official documentation
相关问题 更多 >
编程相关推荐