Closed miguelsousa closed 1 year ago
the fixture is returned directly from create_redis_fixture()
, so you would replace your fixture with it. like so:
redis_client = create_redis_fixture()
@pytest.fixture
def redis_client2(redis_client): # or if you want/need to create your own client object from credentials
yield Redis(**redis_client.pmr_credentials.as_redis_kwargs())
@DanCardin thanks for the feedback. Here's an updated test_myapp.py that works:
from unittest.mock import patch
from pytest_mock_resources import create_redis_fixture
from myapp import set_item, REDIS_KEY
redis_client_fixt = create_redis_fixture()
def test_set_item(redis_client_fixt):
with patch('myapp.REDIS_CLIENT', redis_client_fixt) as redis_client:
idx = 1234
value = b"some_value"
set_item(idx, value)
assert redis_client.hget(REDIS_KEY, idx) == value
It's worth mentioning that the module python_on_whales
had to be installed (python -m pip install python-on-whales
) and a redis
Docker image was pulled the first time the test ran.
you can ensure python_on_whales
is installed by using the "docker" extra (i.e. pip install pytest-mock-resources[docker]
Describe the bug I'm trying to use
pytest-mock-resources
for the first time to mock a Redis client. I've used the instructions at https://pytest-mock-resources.readthedocs.io/en/latest/redis.html as a guidance, but I'm running into errors. Not sure if they're caused by bugs, or if I'm doing something wrong with the API. Thanks in advance.Environment
To Reproduce Steps to reproduce the behavior:
python -m pip install pytest redis pytest-mock-resources
to install the necessary dependenciespython -m pytest
Expected behavior The two tests —
test_set_item
andtest_set_item2
— contained in test_myapp.py should succeed.Actual Behavior Both tests fail. The result is shown below.
Additional context Contents of myapp.py
Contents of test_myapp.py