简体   繁体   English

异步测试不在 pytest 中运行

[英]async test does not run in pytest

I am trying to test an async function. Thus, I have an async test.我正在尝试测试异步 function。因此,我进行了异步测试。 When I use pycharm to set breakpoints in this test, they don't break.当我在这个测试中使用 pycharm 设置断点时,它们不会中断。 The things which are supposed to print to console do not print.应该打印到控制台的东西不会打印。 And, the test takes 0 seconds.而且,测试需要 0 秒。 pytest lists the test as PASSED , but it clearly isn't actually executing the code in context. pytest 将测试列为PASSED ,但显然它实际上并未在上下文中执行代码。

Here is the code:这是代码:

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())

What I've tried:我试过的:

  • enable python.debug.asyncio.repl in pycharm registry在 pycharm 注册表中启用python.debug.asyncio.repl
  • add arguments to pytest including --capture=no -s and --no-conv将 arguments 添加到 pytest,包括--capture=no -s--no-conv

Nothing seems to be working.似乎没有任何效果。 Would really appreciate some guidance here if anyone is accustomed to debugging async tests.如果有人习惯于调试异步测试,将非常感谢这里的一些指导。

According to pytest-asyncio documentation:根据pytest-asyncio文档:

Note that test classes subclassing the standard unittest library are not supported.请注意,不支持继承标准单元测试库的测试类。 Users are advised to use unittest.IsolatedAsyncioTestCase or an async framework such as asynctest.建议用户使用 unittest.IsolatedAsyncioTestCase 或异步框架,如 asynctest。

Did you consider removing the unittest.TestCase inheritance and rewriting your test class without it?您是否考虑删除unittest.TestCase inheritance 并在没有它的情况下重写您的测试 class?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM