Skip to main content

Openmock

Mock/fake of opensearch library, allows you to mock opensearch-py

Fork of Python Elasticsearch(TM) Mock. Sometimes the developers who work with elasticsearch (TM), don't really have any input in choice of host and need to get work done.

Libraries.io dependency status for latest release Downloads

Installation

pip install openmock

Usage

To use Openmock, decorate your test method with @openmock decorator:

from unittest import TestCase

from openmock import openmock


class TestClass(TestCase):

    @openmock
    def test_should_return_something_from_opensearch(self):
        self.assertIsNotNone(some_function_that_uses_opensearch())

Custom Behaviours

You can also force the behaviour of the OpenSearch instance by importing the openmock.behaviour module:

from unittest import TestCase

from openmock import behaviour


class TestClass(TestCase):

    ...

    def test_should_return_internal_server_error_when_simulate_server_error_is_true(self):
        behaviour.server_failure.enable()
        ...
        behaviour.server_failure.disable()

You can also disable all behaviours by calling behaviour.disable_all() (Consider put this in your def tearDown(self) method)

Available Behaviours

  • server_failure: Will make all calls to OpenSearch returns the following error message:
    {
        'status_code': 500,
        'error': 'Internal Server Error'
    }
    

Code example

Let's say you have a prod code snippet like this one:

import opensearchpy

class FooService:

    def __init__(self):
        self.es = opensearchpy.OpenSearch(hosts=[{'host': 'localhost', 'port': 9200}])

    def create(self, index, body):
        es_object = self.es.index(index, body)
        return es_object.get('_id')

    def read(self, index, id):
        es_object = self.es.get(index, id)
        return es_object.get('_source')

Then you should be able to test this class by mocking OpenSearch using the following test class:

from unittest import TestCase
from openmock import openmock
from foo.bar import FooService

class FooServiceTest(TestCase):

    @openmock
    def should_create_and_read_object(self):
        # Variables used to test
        index = 'test-index'
        expected_document = {
            'foo': 'bar'
        }

        # Instantiate service
        service = FooService()

        # Index document on OpenSearch
        id = service.create(index, expected_document)
        self.assertIsNotNone(id)

        # Retrieve document from OpenSearch
        document = service.read(index, id)
        self.assertEquals(expected_document, document)

Admin tools

Openmock ships three interactive admin tools for exploring fake state outside of tests.

Streamlit web UI (requires pip install openmock[web]):

openmock

Tkinter desktop GUI (no extra dependencies — uses Python's built-in tkinter):

openmock gui

FastAPI REST Bridge (requires pip install openmock[rest]):

openmock serve

These tools open a six-tab interface or an HTTP facade covering indices, search sandbox, cluster stats, CAT output, fake security (users/roles), and ingest pipelines. State in these tools is separate from your test processes.

Notes:

  • The mocked search method evaluates the query against indexed documents and returns matching results, not all documents. Use match_all if you want everything.
  • The mocked suggest method returns the exactly suggestions dictionary passed as body serialized in OpenSearch.suggest response. Attention: If the term is an int, the suggestion will be python term + 1. If not, the suggestion will be formatted as python {0}_suggestion.format(term) . Example:
    • Suggestion Body:
    suggestion_body = {
        'suggestion-string': {
            'text': 'test_text',
            'term': {
                'field': 'string'
            }
        },
        'suggestion-id': {
            'text': 1234567,
            'term': {
                'field': 'id'
            }
        }
    }
    
    • Suggestion Response:
    {
        'suggestion-string': [
            {
                'text': 'test_text',
                'length': 1,
                'options': [
                    {
                        'text': 'test_text_suggestion',
                        'freq': 1,
                        'score': 1.0
                    }
                ],
                'offset': 0
            }
        ],
        'suggestion-id': [
            {
                'text': 1234567,
                'length': 1,
                'options': [
                    {
                        'text': 1234568,
                        'freq': 1,
                        'score': 1.0
                    }
                ],
                'offset': 0
            }
        ],
    }
    

Testing

Preferred for testing one version of python.

pytest test

Run the same pytest suite against a disposable Docker OpenSearch instead of the in-memory fake:

uv run python scripts/opensearch_docker.py test

If you want to manage the container yourself, the backend seam is driven by OPENMOCK_TEST_BACKEND=real and OPENMOCK_REAL_OPENSEARCH_URL=http://localhost:9200. The default remains the in-memory mock backend.

The collected tests are split into two marker groups:

  • parity for live-backend parity coverage,
  • mock_backend for tests that are specific to the in-memory fake.

Examples:

uv run python -m pytest tests -m parity
uv run python -m pytest tests -m mock_backend

Won't catch pytest tests.

python -m unittest

We are trying to support a full matrix of openmock versions and python versions 3.9+. This is slow.

tox

Changelog

See CHANGELOG.md

License

MIT with normalize_host.py being Apache 2 from Elasticsearch.

Metadata

Release files for openmock 3.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for openmock 3.2.0
File Size Uploaded
openmock-3.2.0.tar.gz 39.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for openmock 3.2.0
File Interpreter ABI Platform
openmock-3.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 85.0 kB

Release files / openmock-3.2.0.tar.gz

Download URL openmock-3.2.0.tar.gz
Size 39.8 kB
Tags Source
SHA-256 checksum
How to use checksums
6e08c0590665d89763fd750aaa144e107aca30bd9aba00d54ec6b00d727b8bfd
BLAKE2b-256 checksum
How to use checksums
f685e740ab4e9e5f6796fd0b9395f759a3fa27544a3d2ef88f379ba5b863336e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.13

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 21, 2026.

Transparency log

Release files / openmock-3.2.0-py3-none-any.whl

Download URL openmock-3.2.0-py3-none-any.whl
Size 45.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8000d9f331732ed88c5876c0a256d4e0134f36f9400646492b3f28f83b5fb8ff
BLAKE2b-256 checksum
How to use checksums
4ab60ee886907820899ea375c1e750c353689ad30bb77155b7f2f343a9d5a778
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.13

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 21, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

3.2.0 This release

2 release files

3.1.5

2 release files

3.1.4

2 release files

3.1.3

2 release files

3.1.2

2 release files

3.0.2

2 release files

3.0.1

2 release files

3.0.0

2 release files

2.3.6

2 release files

2.3.5

2 release files

2.3.4

2 release files

2.3.3

2 release files

2.3.2

2 release files

2.3.1

2 release files

2.3.0

2 release files

2.2.0

2 release files

2.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page