Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/205.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中模拟实例方法_Python_Mocking_Pytest_Python Unittest - Fatal编程技术网

如何在Python中模拟实例方法

如何在Python中模拟实例方法,python,mocking,pytest,python-unittest,Python,Mocking,Pytest,Python Unittest,考虑以下三个文件 # my_class.py class MyClass: def __init__(self): pass def do_thing(self): return 5 # main.py from my_class import MyClass def my_func(): instance = MyClass() instance.do_thing() # test_main.py from ma

考虑以下三个文件

# my_class.py

class MyClass:
    def __init__(self):
        pass

    def do_thing(self):
        return 5


# main.py

from my_class import MyClass

def my_func():
    instance = MyClass()
    instance.do_thing()


# test_main.py

from main import my_func
from unittest.mock import patch

@patch('main.MyClass')
def test_my_func(MockMyClass):
    my_func()
    MockMyClass.do_thing.assert_called_once()
AssertionError:预期“do_thing”已被调用一次。呼叫0次。

我正在驱动函数
myfunc
中实例化一个类
MyClass
,并调用该类的一个方法
do\u thing
。我想做的是测试在调用驱动程序函数时,类的方法只被调用一次。我遇到了一个断言错误,这给了我很多问题


我已经在网上阅读了无数关于Python模拟的帖子和其他资源,但我还没有弄明白这一点。我认为诀窍在于@patch decorator修补模块导入到的名称空间,而不是从[.这里我做错了什么?

do\u thing
方法是
MyClass
not类方法的实例方法。您断言
MockMyClass.do\u thing.assert\u调用一次()
不正确。以下是单元测试解决方案:

my_class.py

class-MyClass:
定义初始化(自):
通过
def do_thing(自我):
返回5
main.py


从my_类导入MyClass
def my_func():
instance=MyClass()
例如:做某事
test\u main.py

从主导入我的功能
导入单元测试
从unittest.mock导入修补程序
类TestMain(unittest.TestCase):
@补丁('main.MyClass')
def test_my_func(自我,模拟MyClass):
mock_my_class_instance=MockMyClass.return_值
my_func()
mock_my_class_instance.do_thing.assert_called_once()
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
unittest.main()
单元测试结果和覆盖率报告:

。
----------------------------------------------------------------------
在0.001s内运行1次测试
好啊
姓名Stmts未找到封面
-----------------------------------------------------------------------
src/stackoverflow/60539392/main.py 4 0 100%
src/stackoverflow/60539392/my_class.py 5 2 60%3,6
src/stackoverflow/60539392/test_main.py 10 0 100%
-----------------------------------------------------------------------
全部的

my\u func创建MyClass的实例,并对该实例调用do\u thing。您可以检查该类,而不是该实例。