LoginSignup
0
0

pytest-asyncio が入っているのに非同期関数のテストがスキップされる。

Posted at

問題

pytest-asyncio が入っているのに pytest を実行すると以下のような警告が出て、非同期関数のテストがスキップされました。

============================= test session starts ==============================
platform linux -- Python 3.11.4, pytest-7.4.0, pluggy-1.2.0
rootdir: /src
plugins: anyio-3.7.1, asyncio-0.21.1
asyncio: mode=Mode.STRICT
collected 3 items / 2 deselected / 1 selected

tests/test_main.py s                                                     [100%]

=============================== warnings summary ===============================
~~ 略 ~~
PytestUnhandledCoroutineWarning: async def functions are not natively supported and have been skipped.

~~ 略 ~~

================= 1 skipped, 2 deselected, 6 warnings in 3.54s =================

原因と対処法

非同期関数のテスト用のデコレーターの付け忘れでした。
以下のように非同期関数の定義の前にデコレーターを付けました。

@pytest.mark.asyncio
async def <任意の関数名>():
    ~~ 略 ~~

結果

以下のようにうまくテストが実行されました。

============================= test session starts ==============================
platform linux -- Python 3.11.4, pytest-7.4.0, pluggy-1.2.0
rootdir: /src
plugins: anyio-3.7.1, asyncio-0.21.1
asyncio: mode=Mode.STRICT
collected 3 items / 2 deselected / 1 selected

tests/test_main.py .                                                     [100%]

~~ 略 ~~

================= 1 passed, 2 deselected, 7 warnings in 4.20s ==================
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0