I have a django app that uses elastic search. I want to have 100% code test coverage so I need to test API calls to elasticsearch (which is "installed" locally).
So my question is this: is it better to mock whole elasticsearch or should I run elasticserver and check the results?
IMO It is better to mock elasticsearch and just check the python code (test if everything was called with the correct params).
Elasticsearch Setup To integrate Elasticsearch with Django, we need to install the following packages: elasticsearch - official low-level Python client for Elasticsearch. elasticsearch-dsl-py - high-level library for writing and running queries against Elasticsearch.
Using Elasticsearch with Django Install Django Elasticsearch DSL: $pip install django-elasticsearch-dsl. Then add django_elasticsearch_dsl to the INSTALLED_APPS. You must define ELASTICSEARCH_DSL in your django settings.
Unit Tests are isolated tests that test one specific function. Integration Tests, meanwhile, are larger tests that focus on user behavior and testing entire applications. Put another way, integration testing combines different pieces of code functionality to make sure they behave correctly.
My personal opinion is that tests that call the real Elasticsearch will generate more value and confidence than tests that mock it out. And it's very doable to setup test infrastructure in Django so that these tests are isolated:
from example.elasticsearch import search_media
def index_test_fixtures(es, index_name, data):
created = es.index(index=index_name, body=data)
assert created["result"] == "created"
es.indices.refresh(index_name)
class TestElasticsearch:
def test_elasticsearch(self, elasticsearch):
index_test_fixtures(
elasticsearch,
"movie",
{
"slug": "episode-5",
"title": "Star Wars: Episode V - The Empire Strikes Back",
"description": "After the Rebels are brutally overpowered by the Empire on the ice planet Hoth, Luke Skywalker begins Jedi training with Yoda, while his friends are pursued by Darth Vader and a bounty hunter named Boba Fett all over the galaxy.",
},
)
results = search_media("Star Wars")
assert results[0]["slug"] == "episode-5"
I wrote a bit about how to setup all the configuration (docker-compose
, pytest
, elasticsearch
, etc) in my personal blog: https://yanglinzhao.com/posts/test-elasticsearch-in-django. There's also a working example demo https://github.com/yanglinz/django-pytest-elasticsearch-example if that helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With