4
我的代码模拟补丁的组织结构如下:与__init__.py
DIR/A.py:
from X import Y
class A:
...
DIR/__ init__.py:
from .A import A
__all__ = ['A']
测试/ test_A。 py:
class test_A:
@patch("dir.A.Y")
def test(self, mock_Y):
....
在运行tests/test_A.py时,我(as预期)出现错误:
AttributeError: <class 'dir.A.A'> does not have the attribute 'Y'
的问题是,@patch("dir.A.y")
试图找到Y
在dir.A.A
类,而不是模块dir.A
(它是实际存在)英寸
这很明显是因为我的__init__.py
。我可以通过将模块名称A
和类名称A
更改为不同的符号来解决此问题。
代码的组织方式,我想避免这样的命名更改。我怎样才能以Y
的方式在正确的地方使用patch
?
基本上我说的,而是更早。 :-) –
@MartijnPieters:这个问题基本上有一个答案。 :) –