fastapi-cache
fastapi-cache copied to clipboard
Unable to use pytest with cache
I have a few integration tests than run as part of jenkins job, but I have been unable to get pytests to run and connect to the redis cache. I have tried this but had the same error https://fastapi.tiangolo.com/advanced/testing-events/
Has anyone found a way to test their routes that have a cache?
Anyone faced this issue? I tried the methods outlined in here: https://github.com/tiangolo/fastapi/issues/2003 but I still hit the same issue and tests fail
What problems do you encounter? I finally handled the init error but unfortunately my service function used by the cached endpoint has not been called once. Is there a way to enable/disable the cache for testing or to simulate a cached/uncached request?
I faced the same problem. It fails with "You must call init first!"
add to your conftest.py file
from unittest import mock
mock.patch("fastapi_cache.decorator.cache", lambda *args, **kwargs: lambda f: f).start()
you also can try this code below to mock cache.
# conftest.py
from unittest import mock
def mock_cache(*args, **kwargs):
def wrapper(func):
@wraps(func)
async def inner(*args, **kwargs):
return await func(*args, **kwargs)
return inner
return wrapper
mock.patch("fastapi_cache.decorator.cache", mock_cache).start()
@pytest.fixture(scope="module")
async def client():
from app.main import app # need to load app module after mock. otherwise, it would fail
async with AsyncClient(app=app, base_url="http://test") as client:
yield client
Hi @Mark1002,
Sorry for being a noop here , but I'm hitting error when trying to use the fixture client
. How is client fixture supposed to be used in a test function ?
I have the conftest.py
setup as above, then my code below yield error.
import pytest
from app.main import init_app
@pytest.mark.anyio
async def test_api_get_news_details(asyncclient):
response = await asyncclient.get("/news/34986915")
assert response.status_code == 200
and here's the error
fixture 'mock_decorator' not found
> available fixtures: anyio_backend, anyio_backend_name, anyio_backend_options, cache, capfd, capfdbinary, caplog, capsys, capsysbinary, client, doctest_namespace, monkeypatch, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
What should I do now?
@tpthian Just put the following code above your imports where you are calling the @cache
mock.patch("fastapi_cache.decorator.cache", lambda *args, **kwargs: lambda f: f).start()
@tpthian sorry this is my mistake. remove parameter mock_decorator
in async def client
. i have edit my code snippet above.
This is a potential area where documentation could provide an example.
you also can try this code below to mock cache.
# conftest.py from unittest import mock def mock_cache(*args, **kwargs): def wrapper(func): @wraps(func) async def inner(*args, **kwargs): return await func(*args, **kwargs) return inner return wrapper mock.patch("fastapi_cache.decorator.cache", mock_cache).start() @pytest.fixture(scope="module") async def client(): from app.main import app # need to load app module after mock. otherwise, it would fail async with AsyncClient(app=app, base_url="http://test") as client: yield client
On my end, none of the suggested solutions worked for me, all failing with 'function' object has no attribute 'start'
I am struggling with the same identical problem
ok this works for me.
Create a file called conftest.py
within your module and paste this:
from unittest import mock
def mock_cache():
mock.patch("fastapi_cache.decorator.cache", lambda *args, **kwargs: lambda f: f).start()
def pytest_sessionstart(session):
mock_cache()
@tpthianПросто поместите следующий код над импортом, где вы вызываете @cache
mock.patch("fastapi_cache.decorator.cache", lambda *args, **kwargs: lambda f: f).start()
thx for the reply it really helped me and solved this problem, but none linter allows to set this code on top😥. On the other hand other solutions don't help me, so we have what we have