4

我正在用 pytest 编写 python 测试并且有一些我想测试的异步代码,所以我安装了 pytest-asyncio 插件。异步代码使用 aiohttp 并运行测试我在测试成功运行后收到以下警告/错误/提示。

========================================================================================================================================================================= test session starts =========================================================================================================================================================================
platform win32 -- Python 3.8.7, pytest-6.2.1, py-1.10.0, pluggy-0.13.1
rootdir: C:\Users\user\workspace\example\app
plugins: asyncio-0.14.0
collected 1 item                                                                                                                                                                                                                                                                                                                                                       

tests\test_something.py .

========================================================================================================================================================================== 1 passed in 1.22s ==========================================================================================================================================================================
Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x000001D09DE4C310>
Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\asyncio\proactor_events.py", line 116, in __del__
    self.close()
  File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\asyncio\proactor_events.py", line 108, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\asyncio\base_events.py", line 719, in call_soon
    self._check_closed()
  File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\asyncio\base_events.py", line 508, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed

我正在使用python -m pytest运行以下代码

import aiohttp
import pytest

async def g():
    async with aiohttp.ClientSession() as session:
        async with session.get("https://www.w3schools.com/") as resp:
            pass


@pytest.mark.asyncio
async def test_connection():
    await g()

当我发帖时,测试后的警告不会发生

...
async with session.post('http://httpbin.org/post', data=b'data') as resp:
...

这是我的要求.txt

aiohttp==3.7.3
async-timeout==3.0.1
atomicwrites==1.4.0
attrs==20.3.0
certifi==2020.12.5
chardet==3.0.4
colorama==0.4.4
ibm-cloud-sdk-core==3.3.6
ibm-watson==5.1.0
idna==2.10
iniconfig==1.1.1
multidict==5.1.0
packaging==20.8
pluggy==0.13.1
py==1.10.0
PyJWT==2.0.0
pyparsing==2.4.7
pytest==6.2.1
pytest-asyncio==0.14.0
python-dateutil==2.8.1
requests==2.25.1
six==1.15.0
toml==0.10.2
typing-extensions==3.7.4.3
urllib3==1.26.2
websocket-client==0.48.0
websockets==8.1
yarl==1.6.3

异常被忽略的警告的目的是什么,为什么它只在某些情况下出现?

4

0 回答 0