Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在setUp()中创建asyncio事件是AIOUnitest的一个陷阱_Python_Python 3.x_Async Await_Python Asyncio_Python Unittest - Fatal编程技术网

Python 在setUp()中创建asyncio事件是AIOUnitest的一个陷阱

Python 在setUp()中创建asyncio事件是AIOUnitest的一个陷阱,python,python-3.x,async-await,python-asyncio,python-unittest,Python,Python 3.x,Async Await,Python Asyncio,Python Unittest,我正在使用aiounitest进行单元测试。 在setUp()方法中创建类型为asyncio.Event()的对象。 等待此对象将导致“RuntimeError…将Future连接到其他循环” 问题的原因似乎是aiounitest在执行测试之前,但在调用setUp之后,创建了一个新的事件循环。 直接从测试函数调用own setUp和tearDown可以解决这个问题。但这不是我想要的 是否可以在setUp()中使用正确的事件循环创建事件 对于aiounitestwheresetUp()和tearD

我正在使用aiounitest进行单元测试。 在setUp()方法中创建类型为asyncio.Event()的对象。 等待此对象将导致“RuntimeError…将Future连接到其他循环”

问题的原因似乎是aiounitest在执行测试之前,但在调用setUp之后,创建了一个新的事件循环。 直接从测试函数调用own setUp和tearDown可以解决这个问题。但这不是我想要的

是否可以在setUp()中使用正确的事件循环创建事件

对于aiounitestwheresetUp()tearDown(),是否有一种替代方法,具有正确的事件循环和异步支持

这里是我的测试代码:

import asyncio
import aiounittest


class TestAsyncioEvent(aiounittest.AsyncTestCase):

    async def trigger_event(self, event):
        await asyncio.sleep(0.5)
        event.set()

    def setUp(self):
        aiounittest.AsyncTestCase.setUp(self)
        self.event = asyncio.Event()

    async def test_event_wait(self):
        task = asyncio.create_task(self.trigger_event(self.event))
        await self.event.wait()
        await task
最好使用asynctest.TestCase而不是aiounittest.AsyncTestCase。然后,设置和拆卸功能可以是同步或异步的。您可以使用@asynctest.fail_on(active_handles=True)来修饰测试函数,以检查未完成的任务。最好使用asynctest.TestCase而不是aiounittest.AsyncTestCase。然后,设置和拆卸功能可以是同步或异步的。您可以使用@asynctest.fail_on(active_handles=True)来修饰测试函数,以检查未完成的任务。