私はPythonで単体テストを行っています。私は私のクラスの単体テストを書こうとしています。しかし、私は問題に実行しています:Pythonユニットテスト模擬が正しくありません
from <path1> import InnerObj
from <path2> import new_obj
from <path3> import XYZ
class ClassToBeTested(object):
def __init__(self):
obj = new_obj(param1 = "XYZ", time = 1, innerObj = InnerObj())
self.attr1 = XYZ(obj)
def method(self, random, paramStr):
// Remainder of class
Testクラス:
from mock import patch, PropertyMock, MagicMock
from <path1> import InnerObj
from <path2> import new_obj
from <path3> import XYZ
@pytest.fixture()
@patch('<path1>.InnerObj', new=MagicMock())
@patch('<path2>.new_obj', new=MagicMock())
@patch('<path3>.XYZ', new=MagicMock())
def mock_test():
return ClassToBeTested()
def test_method_true(mock_test):
random = Random_Object()
booleanResult = mock_test.method(random, paramStr)
assert booleanResult == True
私が手にエラーがERROR at setup of test_method_true ______
あるエラー・スタック言及 innerObj/のinitの.py:26:中init qwerty_main = qwerty_assistant.get_root()
モックされたオブジェクトのinitメソッドでコードを呼び出すべきではないので、mockingがInnerObjに対して正しく行われていないと思われがちです。
ここで何か問題がありますか?誰かが正しい方向を指すのを助けてくれますか?
おかげ
は何のthats Iやっている。の値は、テストクラスの「import文」と元のコードで使用されているものと同じです。同じ値のも同様にパッチされます –
learningMyWayThru
インポートステートメントで使用されるパスであってはなりません。インポートするクラスへのパスにする必要があります。この場合、Classtobetestedを含むファイル – dm03514
Ahh !!!了解、ありがとう :) – learningMyWayThru