簡體   English   中英

異步測試不在 pytest 中運行

[英]async test does not run in pytest

我正在嘗試測試異步 function。因此,我進行了異步測試。 當我在這個測試中使用 pycharm 設置斷點時,它們不會中斷。 應該打印到控制台的東西不會打印。 而且,測試需要 0 秒。 pytest 將測試列為PASSED ,但顯然它實際上並未在上下文中執行代碼。

這是代碼:

import unittest.mock
import pytest
from app.listener import ListenerCog


class ListenerCogTestCase(unittest.TestCase):
    def setUp(self):
        # Create a mock discord.Client object
        self.client = unittest.mock.Mock()

        # Create a ListenerCog instance and pass the mock client as the bot attribute
        self.listener_cog = ListenerCog(self.client)

    @pytest.mark.asyncio
    async def test_on_ready(self):
        # Call the on_ready method of the ListenerCog instance
        await self.listener_cog.on_ready()

        # Assert that the "Running!" message is printed
        self.assertIn("Running!", self.stdout.getvalue())

我試過的:

  • 在 pycharm 注冊表中啟用python.debug.asyncio.repl
  • 將 arguments 添加到 pytest,包括--capture=no -s--no-conv

似乎沒有任何效果。 如果有人習慣於調試異步測試,將非常感謝這里的一些指導。

根據pytest-asyncio文檔:

請注意,不支持繼承標准單元測試庫的測試類。 建議用戶使用 unittest.IsolatedAsyncioTestCase 或異步框架,如 asynctest。

您是否考慮刪除unittest.TestCase inheritance 並在沒有它的情況下重寫您的測試 class?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM