如何在pytest中模拟elasticsearch对象?

zf2sa74q  于 2021-06-13  发布在  ElasticSearch
关注(0)|答案(1)|浏览(378)

我有一个类处理到elasticsearch的连接并使用elasticsearch库。我试图在pytest单元测试中使用pytest\u elasticsearch来模拟elasticsearch对象。我真的不知道该怎么办。
我试过这个:

import pytest
from pytest_elasticsearch import factories    
elasticsearch_proc = factories.elasticsearch_proc(port=-1,
                                                      index_store_type='fs')
    elasticsearch = factories.elasticsearch('elasticsearch_proc')
    class TestSuccessResponses:
        @pytest.fixture()
        def create_es_index(self, elasticsearch):
            elasticsearch.indices.create(index='test-index')
        def test_insert_record(self, elasticsearch):
            elasticsearch.create('test-index', '1', {'id': '1',
                                                     'type': 'Scrambled'}, refresh=True)
            custom_query = {"size": 10, "query": {"match_all": {}}}
            actual_response = es_client.client_query(elasticsearch, 'test-index', custom_query)
            print(actual_response)

我得到这个错误:

test setup failed
self = <[AttributeError("'ElasticSearchExecutor' object has no attribute 'command'") raised in repr()] ElasticSearchExecutor object at 0x7fb8289d4fa0>

我不知道如何解决这个问题,如何前进。

1tuwyuhd

1tuwyuhd1#

我是用 aioresponses (https://github.com/pnuckowski/aioresponses)直接模拟应该执行的http请求。

相关问题