【问题标题】:Python Mock class import errorPython Mock 类导入错误
【发布时间】:2019-04-02 19:31:57
【问题描述】:

我试图在我的 Python 测试用例中模拟 elasticsearch.Elasticsearch.indices.exists 函数,但我收到以下导入错误。但是,仅模拟 elasticsearch.Elasticsearch 工作正常。

@ddt
class TestElasticSearchConnector(unittest.TestCase):

    @patch('elasticsearch.Elasticsearch.indices.exists')
    @patch('connectors.elastic_search_connector.ElasticSearchConnector._get_local_conn')
    def test_check_index(self, mock_es, _get_local_conn):
        mock_es = Mock()
        mock_es._index_exists = False
        mock_es.indices.exists.return_value = True
        mock_es.create.return_value = {'result': 'created'}

在此处获取模拟导入错误

================================================ ======================== 错误:test_check_index (tests.base.TestESConnector) -------------------------------------------------- -------------------- 回溯(最近一次通话最后): _dot_lookup 中的文件“/Users/user/.virtualenvs/my-prjlib/python3.6/site-packages/mock/mock.py”,第 1197 行 返回getattr(事物,comp) AttributeError:类型对象“Elasticsearch”没有属性“索引”

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/user/.virtualenvs/my-prjlib/python3.6/site-packages/mock/mock.py", line 1297, in patched
    arg = patching.__enter__()
  File "/Users/user/.virtualenvs/my-prjlib/python3.6/site-packages/mock/mock.py", line 1353, in __enter__
    self.target = self.getter()
  File "/Users/user/.virtualenvs/my-prjlib/python3.6/site-packages/mock/mock.py", line 1523, in <lambda>
    getter = lambda: _importer(target)
  File "/Users/user/.virtualenvs/my-prjlib/python3.6/site-packages/mock/mock.py", line 1210, in _importer
    thing = _dot_lookup(thing, comp, import_path)
  File "/Users/user/.virtualenvs/my-prjlib/python3.6/site-packages/mock/mock.py", line 1199, in _dot_lookup
    __import__(import_path)
ModuleNotFoundError: No module named 'elasticsearch.Elasticsearch'

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

FAILED (errors=1)

测试导入

>>  user$ python
Python 3.6.1 (default, May 10 2017, 09:46:05) 
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.42)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from elasticsearch import Elasticsearch
>>> 
>>> 

【问题讨论】:

    标签: python elasticsearch mocking


    【解决方案1】:

    这确实是一个错误。当指定要模拟的属性/方法时,它必须存在于对象上(在本例中为类)。也许您希望这个属性存在,但它只存在于实例化的对象上。

    In [1]: from elasticsearch import Elasticsearch
    In [2]: Elasticsearch.indices
    ---------------------------------------------------------------------------
    AttributeError                            Traceback (most recent call last)
    <ipython-input-2-313eaaedb2f6> in <module>()
    ----> 1 Elasticsearch.indices
    

    确实,它存在于实例化的对象上:

    In [3]: Elasticsearch().indices
    Out[3]: <elasticsearch.client.indices.IndicesClient at 0x102db0a90>
    

    【讨论】:

      【解决方案2】:

      当您实例化 Elasticsearch() 对象时,Elasticserch 库会生成 indices 属性。它使用库中的一个名为IndicesClient 的类来实现这一点,而这个类拥有exists 方法。因此,如果您模拟 IndicesClient 类的该方法的响应,您的测试应该可以工作。

      此外,函数的输入参数应该与装饰器的顺序相反。如果您将indices.exists 补丁放在第一位,那么它应该在函数的输入中排在第二位。

      from elasticsearch.client import IndicesClient
      
      @mock.patch.object(IndicesClient, 'exists')
      @mock.patch('connectors.elastic_search_connector.ElasticSearchConnector._get_local_conn')
      def test_check_index(self, mock_get_local_conn, mock_exists):
          mock_exists.return_value = True
          ...
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-05-18
        • 2016-05-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-21
        相关资源
        最近更新 更多