如何对 __init__ 方法外初始化的实例变量进行单元测试
How to unit test instance variables initialized outside __init__ method
我有一个 class,它在 __init__
以外的方法中初始化实例变量。 __init__
方法中进一步引用了该实例变量。
在下面的代码中,ProductionClass
是我正在测试的 class。在 ProductionClass
中,__init__
调用方法 a_method
。方法 a_method
初始化实例变量 inst_var_2
。 inst_var_2
被 __init__
用来设置另一个实例变量 inst_var_3
.
class LibraryClass(object):
def __init__(self):
self.some_attrib = "some_attrib_value"
class ProductionClass(object):
def __init__(self, arg_one):
self.inst_var_1 = arg_one
self.a_method()
self.inst_var_3 = self.inst_var_2.some_attrib
def a_method(self):
self.inst_var_2 = LibraryClass()
import unittest
from unittest.mock import patch, MagicMock
class ProdTestCase(unittest.TestCase):
@patch.object(ProductionClass, 'a_method')
def test_init_method(self, mock_method):
instance = ProductionClass(1234)
self.assertEqual(1234, instance.inst_var_1)
mock_method.assert_called_once_with()
我正在尝试对 ProductionClass
的 __init__
方法进行单元测试,但由于缺少实例变量 inst_var_2
,我无法创建 ProductionClass
的实例.这是错误消息:
Traceback (most recent call last):
...
File "/... production_class.py", line 9, in __init__
self.inst_var_3 = self.inst_var_2.some_attrib
AttributeError: 'ProductionClass' object has no attribute 'inst_var_2'
我的问题是,在我模拟 a_method
时是否可以对 __init__
方法进行单元测试。我想模拟 a_method
因为我不希望该方法实例化 LibraryClass
.
您的问题与:python mock - patching a method without obstructing implementation 基本相同。
由于您模拟了 a_method
,因此无法设置 self.inst_var_2
。因此__init__
不能成功。您必须在测试中考虑到这一点,但是,您仍然可以断言调用了模拟:
@patch.object(ProductionClass, 'a_method')
def test_init_method(self, mock_method):
with self.assertRaises(AttributeError):
ProductionClass(1234):
mock_method.assert_called_once_with()
I want to mock a_method
because I don't want the method to instantiate LibraryClass
.
最好打补丁 your_module.LibraryClass
,而不是 a_method
。
我有一个 class,它在 __init__
以外的方法中初始化实例变量。 __init__
方法中进一步引用了该实例变量。
在下面的代码中,ProductionClass
是我正在测试的 class。在 ProductionClass
中,__init__
调用方法 a_method
。方法 a_method
初始化实例变量 inst_var_2
。 inst_var_2
被 __init__
用来设置另一个实例变量 inst_var_3
.
class LibraryClass(object):
def __init__(self):
self.some_attrib = "some_attrib_value"
class ProductionClass(object):
def __init__(self, arg_one):
self.inst_var_1 = arg_one
self.a_method()
self.inst_var_3 = self.inst_var_2.some_attrib
def a_method(self):
self.inst_var_2 = LibraryClass()
import unittest
from unittest.mock import patch, MagicMock
class ProdTestCase(unittest.TestCase):
@patch.object(ProductionClass, 'a_method')
def test_init_method(self, mock_method):
instance = ProductionClass(1234)
self.assertEqual(1234, instance.inst_var_1)
mock_method.assert_called_once_with()
我正在尝试对 ProductionClass
的 __init__
方法进行单元测试,但由于缺少实例变量 inst_var_2
,我无法创建 ProductionClass
的实例.这是错误消息:
Traceback (most recent call last):
...
File "/... production_class.py", line 9, in __init__
self.inst_var_3 = self.inst_var_2.some_attrib
AttributeError: 'ProductionClass' object has no attribute 'inst_var_2'
我的问题是,在我模拟 a_method
时是否可以对 __init__
方法进行单元测试。我想模拟 a_method
因为我不希望该方法实例化 LibraryClass
.
您的问题与:python mock - patching a method without obstructing implementation 基本相同。
由于您模拟了 a_method
,因此无法设置 self.inst_var_2
。因此__init__
不能成功。您必须在测试中考虑到这一点,但是,您仍然可以断言调用了模拟:
@patch.object(ProductionClass, 'a_method')
def test_init_method(self, mock_method):
with self.assertRaises(AttributeError):
ProductionClass(1234):
mock_method.assert_called_once_with()
I want to mock
a_method
because I don't want the method to instantiateLibraryClass
.
最好打补丁 your_module.LibraryClass
,而不是 a_method
。