我在运行异步测试时遇到了问题.
我的例子是:
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