pytest passes an async generator where the test expects a database session
Fix it fast
Most common cause, 1 of 2An async fixture uses @pytest.fixture while pytest-asyncio is in strict mode
Decorate async fixtures with @pytest_asyncio.fixture.
import pytest_asyncio
@pytest_asyncio.fixture
async def session():
async with SessionLocal() as s:
yield s
# or, for the whole project, in pyproject.toml:
# [tool.pytest.ini_options]
# asyncio_mode = "auto"Short answer
AttributeError: 'async_generator' object has no attribute 'add' means the test received the async generator itself, not the AsyncSession it yields. Most often an async fixture uses @pytest.fixture while pytest-asyncio runs in strict mode: use @pytest_asyncio.fixture or set asyncio_mode = "auto". Calling get_db() directly causes it too. Applies to pytest-asyncio 1.x with pytest 8.4; pytest 9 stops earlier.
Which cause do you have?
Check two things: the mode pytest-asyncio reports in the pytest header, and which async fixtures carry a plain @pytest.fixture decorator. mode=strict plus a hit in the grep means cause 1. Auto mode, or no hit, sends you to cause 2.
pytest --collect-only 2>&1 | grep "^asyncio:"
grep -rn -A1 "@pytest.fixture" tests/ | grep "async def"Then find the line you see in this list and open its fix:
- 1An async fixture uses @pytest.fixture while pytest-asyncio is in strict mode
The header shows asyncio: mode=strict, and the failing fixture is an async def with yield under a plain @pytest.fixture; pytest 8.4 adds the PytestDeprecationWarning, pytest 9 reports the fixture as not handled - 2The test or helper calls an async generator such as get_db() instead of iterating it
The failing line is your own code, for example db = get_db() followed by db.add(...), and the error stays in auto mode
What does the error look like?
AttributeError: 'async_generator' object has no attribute 'add'PytestDeprecationWarning: asyncio test 'test_create_item' requested async @pytest.fixture 'session' in strict mode. You might want to use @pytest_asyncio.fixture or switch to auto mode.'test_create_item' requested an async fixture 'session', with no plugin or hook that handled it. This is an error, as pytest does not natively support it.
Cause 1 of 2
An async fixture uses @pytest.fixture while pytest-asyncio is in strict mode
You seeThe header shows asyncio: mode=strict, and the failing fixture is an async def with yield under a plain @pytest.fixture; pytest 8.4 adds the PytestDeprecationWarning, pytest 9 reports the fixture as not handled
Check
The test gets an async_generator with no add attribute because nobody ran the fixture. In strict mode, pytest-asyncio only evaluates async fixtures decorated with @pytest_asyncio.fixture. pytest 8.x then passes the raw generator object into the test. pytest 9.0 turned that case into an error, so on current pytest the run stops at fixture setup.
pytest tests/test_items.py -x -W error::DeprecationWarningFix
Decorate async fixtures with @pytest_asyncio.fixture. If asyncio is the only async library your tests use, auto mode is simpler: pytest-asyncio then takes ownership of every async fixture and marks every async test.
import pytest_asyncio
@pytest_asyncio.fixture
async def session():
async with SessionLocal() as s:
yield s
# or, for the whole project, in pyproject.toml:
# [tool.pytest.ini_options]
# asyncio_mode = "auto"Verify
The test passes, and neither the deprecation warning nor the not-handled error appears in the summary.
pytest tests/test_items.py -qConfirmed by pytest-asyncio.readthedocs.io
Cause 2 of 2
The test or helper calls an async generator such as get_db() instead of iterating it
You seeThe failing line is your own code, for example db = get_db() followed by db.add(...), and the error stays in auto mode
Check
A dependency written as async def with yield, the usual shape of a FastAPI get_db(), is an asynchronous generator function. Python's reference says that calling one returns an asynchronous generator object; the session only appears when the generator is iterated, so .add() on the returned object fails.
grep -rn "= get_db()" tests/ app/Fix
Open the session yourself from the async_sessionmaker in tests. When the generator has to be used, await anext() on it to get the session and close the generator afterwards.
# preferred: use the session factory directly
async with SessionLocal() as session:
session.add(Item(name="a"))
await session.commit()
# or drive the generator by hand
gen = get_db()
session = await anext(gen)
session.add(Item(name="a"))
await session.commit()
await gen.aclose()Verify
The row is written, the test passes, and the AttributeError no longer appears.
pytest tests/test_items.py -qConfirmed by docs.python.org
How do I stop it coming back?
- Set asyncio_mode = "auto" in pyproject.toml when asyncio is your only async library, so a plain @pytest.fixture on an async fixture cannot slip through.
- Run pytest with -W error::DeprecationWarning in CI so pytest-asyncio's strict-mode warning fails the build on pytest 8.x.
- Keep one session fixture in conftest.py and make tests request it, instead of calling get_db() inside test bodies.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: stegnor.com, Menulo, ShopGo.