-
-
Notifications
You must be signed in to change notification settings - Fork 147
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
Unable to use pytest with cache #49
Comments
Anyone faced this issue? |
What problems do you encounter? |
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 I have the
and here's the error
What should I do now? |
@tpthian Just put the following code above your imports where you are calling the @cache
|
@tpthian sorry this is my mistake. remove parameter |
This is a potential area where documentation could provide an example. |
On my end, none of the suggested solutions worked for me, all failing with |
I am struggling with the same identical problem |
ok this works for me. Create a file called
|
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 |
this worked for me. thank you @Mark1002. i was stuck on this for sooooo long - here's the top of my conftest.py if it helps anyone else in the future:
|
The error occurs because FastAPICache has not been initialized before use. Initialize FastAPICache in your main application file (usually main.py). You should do this after creating an instance of FastAPI. This solution worked for me: @app.on_event("startup"). 2.In the conftest.py file, added the following: @pytest.fixture(autouse=True, scope="function")
@pytest.mark.asyncio |
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?
The text was updated successfully, but these errors were encountered: