相关疑难解决方法(0)

如何测试Python 3.4 asyncio代码?

使用Python 3.4 asyncio库编写代码单元测试的最佳方法是什么?假设我想测试TCP客户端(SocketConnection):

import asyncio
import unittest

class TestSocketConnection(unittest.TestCase):
    def setUp(self):
        self.mock_server = MockServer("localhost", 1337)
        self.socket_connection = SocketConnection("localhost", 1337)

    @asyncio.coroutine
    def test_sends_handshake_after_connect(self):
        yield from self.socket_connection.connect()
        self.assertTrue(self.mock_server.received_handshake())
Run Code Online (Sandbox Code Playgroud)

当使用默认测试运行器运行此测试用例时,测试将始终成功,因为该方法仅执行到第一yield from条指令,之后它在执行任何断言之前返回.这导致测试总是成功.

是否有预先构建的测试运行器能够处理这样的异步代码?

python unit-testing python-3.x python-unittest python-asyncio

66
推荐指数
7
解决办法
2万
查看次数