繁体   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