문제

I used to use Trial for testing TCP servers written with Twisted. Is there any support from Trial to test UDP servers ?

thanks !

올바른 솔루션이 없습니다

다른 팁

There is actually no documentation, but support. Use

twisted.test.proto_helpers.FakeDatagramTransport

This is a test for the Echo Server from the UDPTutorial.

from twisted.trial import unittest
from twisted.test import proto_helpers
import echo


class EchoTest(unittest.TestCase):
    def setUp(self):
        self.protocol = echo.Echo()
        self.transport = proto_helpers.FakeDatagramTransport()
        self.protocol.transport = self.transport

    def test_echo(self):
        self.protocol.startProtocol()
        self.assertTrue(len(self.transport.written) == 0)
        # simulate incoming package
        self.protocol.datagramReceived("test", ("127.0.0.1", 55555))
        # check echo has been written as answer on the transport
        msg, addr = self.transport.written[0]
        self.assertEqual(msg, "test")
        self.assertEqual(addr[1], 55555)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top