Python 模拟文件中导入的类的初始化

Python 模拟文件中导入的类的初始化,python,unit-testing,mocking,Python,Unit Testing,Mocking,我有一个文件副本_x_to _y.py,如下所示: from abcd import F def function_to_be_tested(): F() from xyz import XY class F(): def __init__(self, arg1): self.xy = XY(arg1) 在abcd.py文件中,我有如下内容: from abcd import F def function_to_be_tested(): F()

我有一个文件副本_x_to _y.py,如下所示:

from abcd import F

def function_to_be_tested():
     F()
from xyz import XY

class F():
   def __init__(self, arg1):
       self.xy = XY(arg1)
在abcd.py文件中,我有如下内容:

from abcd import F

def function_to_be_tested():
     F()
from xyz import XY

class F():
   def __init__(self, arg1):
       self.xy = XY(arg1)
我想在我的测试用例中模拟XY的init。 我试着用以下方法模拟F的init:

def mock_func(*args, **kwargs):
    pass

@patch('path/to/copy_x_to_y.F.__init__', mock_func)
def test():
    assert function_to_be_tested() is None
但它总是调用XY的init,导致初始化调用时出错
使用arg1连接S3。如何测试这种结构?

为什么要模拟XY的初始?是否要返回XY的特定对象,是否要检查XY.\uuuu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

问题的一个可能解决方案是模拟整个类,但让它返回一个普通对象。下面是一个例子:

>>> from unittest.mock import patch
>>> class MyClass:
...   def __init__(self, val):
...     self._val = val
...   def foo(self):
...     print(self._val)
... 
>>> a = MyClass(1)
>>> a.foo()
1
>>> patcher = patch('__main__.MyClass', return_value=a)
>>> mock_class = patcher.start()
>>> b = MyClass(2)  # This will return a.
>>> b.foo()
1
>>> mock_class.call_args_list
[call(2)]
>>> patcher.stop()
在你的情况下,这将是:

from xyz import XY
from path/to/copy_x_to_y import function_to_be_tested
def test():
  arg1 = ...
  a = XY(arg1)  # Has to be called before the patch to get a "normal" object.
  with patch('xyz.XY', return_value=a) as mock_xy:
    # Run funcion to be tested here and check results.
    function_to_be_tested()
    assert ...
一些旁注: 不过,如果您真的需要这样做的话,可以直接模拟init。 如果使用补丁装饰器,则必须为装饰函数提供一个额外的参数,该参数是类或对象的模拟参数。 另外,补丁通常是独占的?用于修补类,而patch.object用于修补类或模块内的成员。
想要模拟XY的初始值的原因是什么?是否要返回XY的特定对象,是否要检查XY.\uuuu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

问题的一个可能解决方案是模拟整个类,但让它返回一个普通对象。下面是一个例子:

>>> from unittest.mock import patch
>>> class MyClass:
...   def __init__(self, val):
...     self._val = val
...   def foo(self):
...     print(self._val)
... 
>>> a = MyClass(1)
>>> a.foo()
1
>>> patcher = patch('__main__.MyClass', return_value=a)
>>> mock_class = patcher.start()
>>> b = MyClass(2)  # This will return a.
>>> b.foo()
1
>>> mock_class.call_args_list
[call(2)]
>>> patcher.stop()
在你的情况下,这将是:

from xyz import XY
from path/to/copy_x_to_y import function_to_be_tested
def test():
  arg1 = ...
  a = XY(arg1)  # Has to be called before the patch to get a "normal" object.
  with patch('xyz.XY', return_value=a) as mock_xy:
    # Run funcion to be tested here and check results.
    function_to_be_tested()
    assert ...
一些旁注: 不过,如果您真的需要这样做的话,可以直接模拟init。 如果使用补丁装饰器,则必须为装饰函数提供一个额外的参数,该参数是类或对象的模拟参数。 另外,补丁通常是独占的?用于修补类,而patch.object用于修补类或模块内的成员。
我来这里是想找到一个答案来检查XY.\uuuu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu;我想旁注1会启用这个功能,用mock_init.assert_调用,_with…,我来这里是想找到一个答案来检查XY.u init_uu是否用特定参数调用;我猜侧注1会启用它,使用mock_init.assert_调用_with。。。。