Python unittest,mock,补丁,inpu

2024-04-24 09:18:07 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我的代码有问题。 文件1:

class Abc(object):
...
def function1(self):
 #do something
def function2(self):
 x = input()
 return x+1

现在我试着测试函数2,所以我为它写了一个测试,我不知道我做错了什么:

^{pr2}$

获取ModuleNotFoundError:没有名为“function2”的模块 我在这里做错了什么?在

感谢您的帮助:)


Tags: 文件代码selfinputreturnobjectdefdo
1条回答
网友
1楼 · 发布于 2024-04-24 09:18:07

得到ModuleNotFoundError,因为funcion2不是模块。^{}很清楚这一点:

target should be a string in the form 'package.module.ClassName'. The target is imported and the specified object replaced with the new object, so the target must be importable from the environment you are calling patch() from. The target is imported when the decorated function is executed, not at decoration time.

当从文件所在的目录中使用python3 -m unittest discover执行时,这对我很有用。在

顺便说一句,在你的例子中有几个拼写错误,例如Abc().funcion2(),请注意funcion2中缺少的t。在

{{8}也不要使用

# file1.py
class Abc(object):
    def function1(self):
        return 30

    def function2(self):
        x = input()
        return x + "1"


^{pr2}$

相关问题 更多 >