バグ解消法、お役立ち情報など

image

AttributeError: 'async_generator' object has no attribute 'post'エラーの解消法

発生したエラー

PythonのテストフレームワークであるPytestを使用中、以下のエラーに遭遇しました。

ターミナル
AttributeError: 'async_generator' object has no attribute 'post'

ちなみに、テスト実行コマンドは以下のとおりです。

テスト実行コマンド
docker compose run --entrypoint "poetry run pytest" demo-app

解消法

フィクスチャを以下のように修正します。

修正前

@pytest.fixture async def async_client() -> AsyncClient:

修正後

import pytest_asyncio @pytest_asyncio.fixture async def async_client() -> AsyncClient:

pytest_asyncioを使用することでエラーを解消できます。

原因

asyncio_modeのデフォルト値が変わったことが原因のようです。

https://github.com/pytest-dev/pytest-asyncio/releases

↓参考にさせていただきました!

https://www.beex-inc.com/blog/rejoin-nasu

バグ解消法、お役立ち情報など