我试图在一个派生自另一个类的类上编写一些unittests,但是我有一些困难来模拟父类init方法,尽管您可能不能这样做,所以我正在寻找建议。
以下是我的课程
导入.py
class Imported():
def __init__(self, a="I am Imported"):
print("a:{}".format(a))
父.py
from Imported import Imported
class Parent(object):
parent_list = ["PARENT"]
def __init__(self, a="I am Parent"):
imported = Imported(a)
派生.py
from Parent import Parent
class Derived(Parent):
Parent.parent_list.append("DERIVED")
在我的UNITStices中,我想验证当我从派生类派生对象(派生)时,Pal.PARTYNLISTROL= = [“父”,“派生”]。
这两种解决方案都不起作用
测试导出.py
import unittest
from mock import patch
from Derived import Derived
class test_Derived(unittest.TestCase):
@patch("Derived.Parent.__init__")
def test_init_001(self, mock_parent_init):
a = Derived("I am Derived")
mock_parent_init.assert_called_with("I am Derived")
self.assertEquals(a.parent_list, ["PARENT", "DERIVED"])
@patch("Derived.Imported.Imported")
def test_init_002(self, mock_parent_init):
a = Derived("I am Derived")
mock_parent_init.assert_called_with("I am Derived")
self.assertEquals(a.parent_list, ["PARENT", "DERIVED"])
test_init_001失败
TypeError: __init__() should return None, not 'MagicMock'
test_init_002失败
ImportError: No module named Parent.Imported
有什么建议吗?
对于第一个解决方案,将
__init__
方法的返回值更改为None
。对于第二种解决方案,补丁
Parent.Imported
:相关问题 更多 >
编程相关推荐