如何在pytest中模拟elasticsearch对象?

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

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

  1. import pytest
  2. from pytest_elasticsearch import factories
  3. elasticsearch_proc = factories.elasticsearch_proc(port=-1,
  4. index_store_type='fs')
  5. elasticsearch = factories.elasticsearch('elasticsearch_proc')
  6. class TestSuccessResponses:
  7. @pytest.fixture()
  8. def create_es_index(self, elasticsearch):
  9. elasticsearch.indices.create(index='test-index')
  10. def test_insert_record(self, elasticsearch):
  11. elasticsearch.create('test-index', '1', {'id': '1',
  12. 'type': 'Scrambled'}, refresh=True)
  13. custom_query = {"size": 10, "query": {"match_all": {}}}
  14. actual_response = es_client.client_query(elasticsearch, 'test-index', custom_query)
  15. print(actual_response)

我得到这个错误:

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

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

1tuwyuhd

1tuwyuhd1#

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

相关问题