question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG]: setup not called when testing

See original GitHub issue

Describe 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:

  1. Create a file named app.py
  2. Create a file named test_app.py
  3. 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:closed
  • Created 4 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
hawzie197commented, Jan 27, 2021

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

3reactions
trim21commented, Nov 18, 2019

@heckad https://fastapi.tiangolo.com/tutorial/testing/

last section: Testing Events, startup and shutdown

need to use TestClient as context manager

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found