我在运行异步测试时遇到了问题.

我的例子是:

import unittest
import asyncio
from asyncio import coroutine
import pytest
import os


class TestEcommerce(unittest.TestCase):

    async def assertFloatAlmostEqual(self, first, second, places=2, msg=""):
        """
        Used to compare float type values with the specified precision.
        Since with each test we can get numbers with slightly different accuracy - however, this is not an error
        :param msg:
        :param first:
        :param second:
        :param places:
        :return:
        """
        self.assertAlmostEqual(first, second, places=places, msg=msg)

    @pytest.mark.asyncio
    async def test_deep_analysis_find_complex(self):
        # Just for an example
        await self.assertFloatAlmostEqual(100.44, 100.445)
        pass

我得到了这个错误:

Kernel/test/test_demo.py::TestEcommerce::test_deep_analysis_find_complex /usr/lib/python3.10/unittest/Case.py:549:运行警告:协程 ‘TestEcommerce.test_deep_analysis_find_complex’从未被期待过 方法()使tracemalloc能够回溯到对象被分配的位置.看见 https://docs.pytest.org/en/stable/how-to/capture-warnings.html#resource-warnings个 更多信息.

-doc:https://docs.pytest.org/en/stable/how-to/capture-warnings.html

我早些时候查了similar question分,然而,上面的答案对我来说不起作用.

我还试图从main函数中删除async,但随后又出现了另一个错误(这实际上是意料之中的)

@pytest.mark.asyncio
def test_deep_analysis_find_complex(self):
    # Just for an example
    await self.assertFloatAlmostEqual(100.44, 100.445)
    pass

E await self.assertFloatAlmostEqual(100.44, 100.445) E
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ E SyntaxError: 'await' outside async function

你能告诉我怎么做这个测试吗?

以下是我的环境信息:

pip list --format=freeze | grep pytest

pytest==7.4.0
pytest-asyncio==0.21.1

python -V

Python3.10.6

推荐答案

pytest-asynciodocs开始:

请注意,不支持将标准unittest库子类化的测试类.建议用户使用unitest.IsolatedAsyncioTestCase或异步框架,如asynctest.

你需要使用unittest.IsolatedAsyncioTestCase或类似asynctest的东西,而不是unittest.TestCase.

Python相关问答推荐

Matplotlib轮廓线值似乎不对劲

过滤绕轴旋转的螺旋桨

如何销毁框架并使其在tkinter中看起来像以前的样子?

计算所有前面行(当前行)中列的值

使用polars .滤镜进行切片速度比pandas .loc慢

比较两个二元组列表,NP.isin

运行回文查找器代码时发生错误:[类型错误:builtin_index_or_system对象不可订阅]

如何在Python数据框架中加速序列的符号化

如果值发生变化,则列上的极性累积和

在Python中调用变量(特别是Tkinter)

numpy.unique如何消除重复列?

Pandas:计算中间时间条目的总时间增量

如何将数据帧中的timedelta转换为datetime

Python将一个列值分割成多个列,并保持其余列相同

Python—在嵌套列表中添加相同索引的元素,然后计算平均值

如何编辑此代码,使其从多个EXCEL文件的特定工作表中提取数据以显示在单独的文件中

我怎么才能用拉夫分拣呢?

为什么我的scipy.optimize.minimize(method=";newton-cg";)函数停留在局部最大值上?

如果服务器设置为不侦听创建,则QWebSocket客户端不连接到QWebSocketServer;如果服务器稍后开始侦听,则不连接

替换包含Python DataFrame中的值的<;