小编Ben*_*lca的帖子

mock.patch的正确路径

在 Python 中使用模拟时,我遇到了计算路径的问题。

假设我有以下文件

文件1.py

def func1():
    return 'X'
Run Code Online (Sandbox Code Playgroud)

文件2.py

from file1 import func1

class ClassA():

    def func_that_uses_func1(self):
        x = func1()
Run Code Online (Sandbox Code Playgroud)

如何修补 ClassA 上 func1 的使用?我尝试过 @mock.patch('file2.func1'),但出现错误AttributeError: <class 'ClassA'> does not have the attribute 'func1'

python unit-testing mocking python-unittest

14
推荐指数
1
解决办法
1万
查看次数

标签 统计

mocking ×1

python ×1

python-unittest ×1

unit-testing ×1