【问题标题】:How to mock self in python?如何在python中模拟自我?
【发布时间】:2016-09-26 08:34:15
【问题描述】:

考虑以下代码。我想模拟self.get_value,在foo.verify_client()中调用

import unittest
import mock

def mock_get_value(self, value):
    return 'client'

class Foo:
    def __init__(self):
        pass
    def get_value(self, value):
        return value
    def verify_client(self):
        client = self.get_value('client')
        return client == 'client'

class testFoo(unittest.TestCase):
    @mock.patch('self.get_value', side_effect = mock_get_value, autospec = True)
    def test_verify_client(self):
        foo = Foo()
        result = foo.verify_client()
        self.assertTrue(result)

if __name__ == "__main__":
    unittest.main()

但是我失败了,错误如下。

E
======================================================================
ERROR: test_verify_client (__main__.testFoo)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/apps/Python/lib/python2.7/site-packages/mock/mock.py", line 1297, in patched
    arg = patching.__enter__()
  File "/apps/Python/lib/python2.7/site-packages/mock/mock.py", line 1353, in __enter__
    self.target = self.getter()
  File "/apps/Python/lib/python2.7/site-packages/mock/mock.py", line 1523, in <lambda>
    getter = lambda: _importer(target)
  File "/apps/Python/lib/python2.7/site-packages/mock/mock.py", line 1206, in _importer
    thing = __import__(import_path)
ImportError: No module named self

----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (errors=1)

我该怎么做?

【问题讨论】:

    标签: python unit-testing mocking


    【解决方案1】:

    我想通了。改变这一行

    @mock.patch('self.get_value', side_effect = mock_get_value, autospec = True)
    

    @mock.patch('test.Foo.get_value', mock_get_value)
    

    工作。

    注意补丁函数的格式应该是module_name + class_name + method_name

    【讨论】:

    • 为什么在'test.Foo.get_value'中是“测试”?
    • @NicScozzaro test 是模块名称。请参阅我在答案中的最后一句话。
    • @Searene 我认为这不能回答@NicScozzaro 的问题。在模拟装饰器中,为什么 test.Foo.get_valuetest 而不是 test_verify_client 或其他东西? test这个词是怎么来的?
    【解决方案2】:

    另一种选择是模拟self 参数。这可以将verify_client 中的逻辑与Foo 类的实例化解耦

    import unittest
    import mock
    
    
    class Foo:
        def __init__(self):
            pass
        def get_value(self, value):
            return value
        def verify_client(self):
            client = self.get_value('client')
            return client == 'client'
    
    class testFoo(unittest.TestCase):
    
        def test_verify_client(self):
            mock_self = mock.Mock()
            mock_self.get_value.return_value = 'client'
            result = Foo.verify_client(mock_self)  # NOTE: Foo is the class, not an instance
            self.assertTrue(result)
    
    if __name__ == "__main__":
        unittest.main()
    

    【讨论】:

    • 澄清一下,这适用于不是@staticmethod 的方法。它可能看起来不像,但效果很好!
    猜你喜欢
    • 1970-01-01
    • 2021-08-25
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多