有没有一种方法可以在Python unittest中向模拟对象添加属性

时间:2020-07-07 13:17:39

标签: python-3.x unit-testing

我正在尝试测试以下代码:

class ABC:
    def _check_something(cls, doc, doc_type=pqr.Document):
        return isinstance(doc, doc_type) and "xyz" in doc

文档是NoSQL对象。我正在验证它是否属于特定实例,并且其密钥中包含“ xyz”。

这是我在其测试班上得到的

class test_ABC(unittest.TestCase):
    def setUp(self):
        self.m_response = MagicMock()
        self.action.client = MagicMock()
        self.action.client.r_session.get.return_value = self.m_response

    def test_check_something(self):
        self.m_response.__class__ = pqr.Document       # Sets up the appropriate class for the mock up object.
        self.m_response.return_value["xyz"] = "test".  # Unable to add this key-value pair, hence test is failing on this point.
        self.assertTrue(self._check_something(self.m_response))

我对单元测试还很陌生。我尝试了单元测试文档中提到的几种不同方式,但尚未点击。有什么方法可以将该属性添加到满足"xyz" in doc条件的模拟对象中?

请让我知道您是否需要进行澄清。

0 个答案:

没有答案