Python 模拟类导入错误



>我试图模拟弹性搜索。Elasticsearch.index.exists 函数在我的 Python 测试用例中,但我收到以下导入错误。但是,模拟只是弹性搜索。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( ---------------------------------------------------------------------- 回溯(最近一次调用(: 文件 "/Users/user/.virtualenvs/my-prjlib/python3.6/site-packages/mock/mock.py",第 1197 行,第 _dot_lookup 行 返回 getattr(thing, comp( 属性错误:类型对象"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
>>> 
>>> 

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

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>

松紧器库会在您实例化Elasticsearch()对象时生成 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
    ...

相关内容

最新更新