Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interaction with test suites that reset the random package's seed #89

Open
sillly-milly opened this issue Apr 4, 2023 · 0 comments
Open

Comments

@sillly-milly
Copy link

I see that the mock client uses the random package to generate new IDs for documents (specifically random.choice).
In my project, I am generating an ES document after each test is ran, and resetting the random package's seed before each test is ran (this part is out of my control). Since the mock client is using the random package to generate IDs, then this means it generates the same ID for every test, which is not ideal for testing my document posting. I wonder if there is a way around this?

Repro (test_repro.py):

import elasticmock
import elasticsearch
import pytest
import random

@pytest.fixture
@elasticmock.elasticmock
def client(scope='session'):
    c = elasticsearch.Elasticsearch()
    c.indices.create('test')
    return c

@pytest.fixture(autouse=True)
def setup_and_teardown(client):
    random.seed(1)
    yield
    print(client.index(index='test', body={'test': 'test'})['_id'])


def test_1():
    random.choice('abcde')
    assert 1 == 1

def test_2():
    random.choice('12345')
    assert 1 == 1

Run with

python3 -m pip install pytest elasticmock elasticsearch
python3 -m pytest test_repro.py

Observe:

test_repro.py .K2ZWeqhFWCEPyYngFb51
.K2ZWeqhFWCEPyYngFb51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant