[BUG]: setup not called when testing
See original GitHub issueDescribe the bug
I’m using aioredis
as my redis driver and it’s initialized by a async function aioredis.create_redis_pool
.
So, I create and bind connection pool in startup
, and in router I get it by Depends
@app.on_event('startup')
async def setup():
app.objects = objects
app.redis_pool = await aioredis.create_redis_pool()
# depends
async def get_redis(request: Request):
return request.app.redis_pool
# router
@app.get('/')
async def get_session(redis=Depends(get_redis)):
pass
But when I’m running my test code, it raise a AttributeError: 'FastAPI' object has no attribute 'redis_pool'
To Reproduce Steps to reproduce the behavior:
- Create a file named
app.py
- Create a file named
test_app.py
- run test with command
pytest
app.py:
from fastapi import FastAPI, Depends
from starlette.requests import Request
app = FastAPI()
async def fake_connect_pool():
async def fake_call(x):
return x
return fake_call
@app.on_event('startup')
async def setup():
app.redis_pool = await fake_connect_pool()
# depends
async def get_redis(request: Request):
return request.app.redis_pool
# router
@app.get('/')
async def get_session(redis=Depends(get_redis)):
return await redis(233)
if __name__ == '__main__':
import uvicorn
uvicorn.run(app, port=8001)
test_app.py
from app import app
from starlette.testclient import TestClient
client = TestClient(app)
def test_index():
response = client.get("/")
assert response.status_code == 200
assert response.text == '233'
you will ses
request = <starlette.requests.Request object at 0x0000025884F3FCF8>
async def get_redis(request: Request):
> return request.app.redis_pool
E AttributeError: 'FastAPI' object has no attribute 'redis_pool'
app.py:21: AttributeError
Expected behavior
No error should be raised, startup
handler should be called before test case.
Environment: OS: windows
starlette 0.12.0 fastapi 0.28.0 pytest 4.6.2 python 3.7.2
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
setUp() not being called in JUnit - Stack Overflow
You're using a JUnit 5 @Test but JUnit 4 @Before / @After . You need to use @BeforeEach / @AfterEach from org.junit.jupiter ....
Read more >Error in unit test when using setUp() function - Laracasts
Getting an error after I add the setUp() function. Also, PHPStorm puts a red underline below the setUp() function name. The code looks...
Read more >Setup - Testing Library
React Testing Library does not require any configuration to be used. However, there are some things you can do when configuring your testing...
Read more >Setup and Teardown - Jest
If you have a test that often fails when it's run as part of a larger suite, but doesn't fail when you run...
Read more >Set Up and Tear Down State in Your Tests - Apple Developer
XCTest then runs each test method, calling setup and teardown methods in this order: XCTest runs the setup methods once before each test...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The section on “Testing Events, startup, and shutdown” looks like it has moved. Found it here: https://fastapi.tiangolo.com/advanced/testing-events/?h=+testing+even
@heckad https://fastapi.tiangolo.com/tutorial/testing/
last section: Testing Events, startup and shutdown
need to use TestClient as context manager