Python 在另一个函数中调用的模拟类方法

Python 在另一个函数中调用的模拟类方法,python,testing,pytest,python-unittest,pytest-mock,Python,Testing,Pytest,Python Unittest,Pytest Mock,我想模拟一个类方法的输出,该输出由另一个模块中定义的函数调用。例如: class_模块.py class my_class: def __init__(self, user): self.user = user def my_method(self): return [1,2,3] def other_methods(self): other_func(self.user) return &quo

我想模拟一个类方法的输出,该输出由另一个模块中定义的函数调用。例如:

class_模块.py

class my_class:
    def __init__(self, user):
        self.user = user

    def my_method(self):
        return [1,2,3]
    
    def other_methods(self):
        other_func(self.user)
        return "something I do not really want to mock"
from class_module import my_class

def my_fun():
    user = "me"
    foo = my_class(user)
    foo.other_methods()
    return foo.my_method()
功能模块.py

class my_class:
    def __init__(self, user):
        self.user = user

    def my_method(self):
        return [1,2,3]
    
    def other_methods(self):
        other_func(self.user)
        return "something I do not really want to mock"
from class_module import my_class

def my_fun():
    user = "me"
    foo = my_class(user)
    foo.other_methods()
    return foo.my_method()
test.py

class my_class:
    def __init__(self, user):
        self.user = user

    def my_method(self):
        return [1,2,3]
    
    def other_methods(self):
        other_func(self.user)
        return "something I do not really want to mock"
from class_module import my_class

def my_fun():
    user = "me"
    foo = my_class(user)
    foo.other_methods()
    return foo.my_method()
@补丁(“函数模块.我的类”)
def测试我的乐趣(模拟类):
class_mock.return_value.my_method.return_value=[]
酒吧=我的乐趣()
断言条==[]
然而,我得到一个
断言错误
[1,2,3]!=[]
。因此,我想模拟永远不会发生在我想要的方法上。有人能解释一下怎么做吗?为什么会这样

编辑

实际上,所示的实现不起作用,因为测试正在启动一个完全独立的过程。因此,任何函数都不能被模拟。对不起,我的误解是

允许修补类的特定方法

class_模块.py

class my_class:
    def __init__(self, user):
        self.user = user

    def my_method(self):
        return [1,2,3]
    
    def other_methods(self):
        other_func(self.user)
        return "something I do not really want to mock"
from class_module import my_class

def my_fun():
    user = "me"
    foo = my_class(user)
    foo.other_methods()
    return foo.my_method()
class-MyClass:
定义初始化(自我,用户):
self.user=用户
定义my_方法(自我):
返回[1,2,3]
def其他_方法(自身):
返回“任何东西”
功能模块.py

class my_class:
    def __init__(self, user):
        self.user = user

    def my_method(self):
        return [1,2,3]
    
    def other_methods(self):
        other_func(self.user)
        return "something I do not really want to mock"
from class_module import my_class

def my_fun():
    user = "me"
    foo = my_class(user)
    foo.other_methods()
    return foo.my_method()
从类\模块导入MyClass
定义我的乐趣():
user=“我”
foo=MyClass(用户)
assert foo.other_methods()=“任何”
返回foo.my_方法()
测试我的功能py

class my_class:
    def __init__(self, user):
        self.user = user

    def my_method(self):
        return [1,2,3]
    
    def other_methods(self):
        other_func(self.user)
        return "something I do not really want to mock"
from class_module import my_class

def my_fun():
    user = "me"
    foo = my_class(user)
    foo.other_methods()
    return foo.my_method()
来自unittest.mock导入修补程序的

从类\模块导入MyClass
从功能模块导入我的乐趣
@对象(MyClass,“my_方法”)
def测试我的乐趣(我的方法):
#给定
任意_值=[99,98,97]
my_method.return_value=任何_值
#什么时候
结果=我的乐趣()
#然后
断言结果==任何_值
使用这些文件,
pytest test\u my\u func.py
成功通过。

允许修补类的特定方法

class_模块.py

class my_class:
    def __init__(self, user):
        self.user = user

    def my_method(self):
        return [1,2,3]
    
    def other_methods(self):
        other_func(self.user)
        return "something I do not really want to mock"
from class_module import my_class

def my_fun():
    user = "me"
    foo = my_class(user)
    foo.other_methods()
    return foo.my_method()
class-MyClass:
定义初始化(自我,用户):
self.user=用户
定义my_方法(自我):
返回[1,2,3]
def其他_方法(自身):
返回“任何东西”
功能模块.py

class my_class:
    def __init__(self, user):
        self.user = user

    def my_method(self):
        return [1,2,3]
    
    def other_methods(self):
        other_func(self.user)
        return "something I do not really want to mock"
from class_module import my_class

def my_fun():
    user = "me"
    foo = my_class(user)
    foo.other_methods()
    return foo.my_method()
从类\模块导入MyClass
定义我的乐趣():
user=“我”
foo=MyClass(用户)
assert foo.other_methods()=“任何”
返回foo.my_方法()
测试我的功能py

class my_class:
    def __init__(self, user):
        self.user = user

    def my_method(self):
        return [1,2,3]
    
    def other_methods(self):
        other_func(self.user)
        return "something I do not really want to mock"
from class_module import my_class

def my_fun():
    user = "me"
    foo = my_class(user)
    foo.other_methods()
    return foo.my_method()
来自unittest.mock导入修补程序的

从类\模块导入MyClass
从功能模块导入我的乐趣
@对象(MyClass,“my_方法”)
def测试我的乐趣(我的方法):
#给定
任意_值=[99,98,97]
my_method.return_value=任何_值
#什么时候
结果=我的乐趣()
#然后
断言结果==任何_值

有了这些文件,
pytest test\u my_func.py
成功通过。

如果没有修补单个方法,请替换类
@patch(“function\u module.my_class”)
,然后查看示例。谢谢@jornsharpe,我尝试了您提供的其他答案。然而,我仍然遇到同样的问题。我更新了我的问题,使之更类似于我的代码。你的代码对我有效,测试在本地通过。你不修补任何方法,替换类
@patch(“function\u module.my\u class”)
,然后查看示例。谢谢@jornsharpe,我尝试了你提供的其他答案。然而,我仍然遇到同样的问题。我更新了我的问题,使之更类似于我的代码。你的代码对我有效,测试在本地通过。