Skip to main content
 首页 » 编程设计

python之扭曲的海螺试验 TDD, "StringTransport instance has no attribute..."

2025年05月04日10jiqing9006

我正在设置一个可配置的 SSH stub shell,类似于 MockSSH ,我想以测试驱动的方式进行。

当我使用 conch.recvline.HistoricRecvLine 而不是基本的 twisted.internet.protocol.Protocol 时,我遇到了测试问题

# test.py 
from twisted.trial import unittest 
from twisted.test.proto_helpers import StringTransportenter code here 
 
class ShellTest(unittest.TestCase): 
    def setUp(self): 
        shell_factory = StubShell.ShellFactory() 
        self.shell = shell_factory.buildProtocol(('127.0.0.1', 0)) 
        self.transport = StringTransport() 
        self.shell.makeConnection(self.transport) 
 
    def test_echo(self): 
        self.shell.lineReceived('irrelevant') 
        self.assertEqual(self.transport.value(), 'something') 
 
# shell.py 
from twisted.internet import protocol 
 
class ShellProtocol(HistoricRecvLine): 
 
    def connectionMade(self): 
        HistoricRecvLine.connectionMade(self) 
 
    def lineReceived(self, line): 
        self.terminal.write('line received') 
 
class ShellFactory(protocol.Factory): 
    protocol = ShellProtocol 

这按预期工作。但是,当我进行更改时:

class ShellProtocol(HistoricRecvLine): 

我得到错误:

exceptions.AttributeError: StringTransport instance has no attribute 'LEFT_ARROW' 

下一步: 多亏了 Glyph 的帮助,我取得了一些进展,仍在尝试进行最低限度的测试设置,以确保我正确设置了 HistoricRecvLine 协议(protocol)。 我从 test_recvline.py 偷了一些代码,这对我来说仍然有点神奇,尤其是设置 sp.factory = self(unittest.Testcase)。我一直在努力将其降低到最低限度以使该测试通过。

class ShellTestConch(unittest.TestCase): 
 
def setUp(self): 
    self.sp = insults.ServerProtocol() 
    self.transport = StringTransport() 
    self.my_shell = StubShell.ShellProtocol() 
    self.sp.protocolFactory = lambda: self.my_shell 
    self.sp.factory = self 
    self.sp.makeConnection(self.transport) 

这让我更接近预期的输出,但是现在我看到终端正在大量破坏输出,这应该是预期的。

twisted.trial.unittest.FailTest: '\x1bc>>> \x1b[4hline received' != 'line received' 

出于 TDD 的目的,我不确定我是否应该只接受输出的 '\x1bc>>>...' 版本(至少在我通过设置自定义提示来打破它之前)或尝试覆盖shell 提示符以获得干净的输出。

请您参考如下方法:

很好的问题;感谢您的提问(也感谢您使用 Twisted,并进行 TDD :-))。

HistoricRecvLine是 TerminalProtocol , 它提供了 ITerminalProtocol . ITerminalProtocol.makeConnection必须通过 ITerminalTransport 的提供商调用.另一方面,您的单元测试正在使用 StringTransport 的实例调用您的 ShellProtocol.makeConnection , 它只提供 IConsumer , IPushProducer , 和 ITransport .

由于 ShellProtocol.makeConnection 需要 ITerminalTransport ,它期望它可以调用它的所有方法。不幸的是,LEFT_ARROW 没有正式记录在这个接口(interface)上,这是一个疏忽,但是 Twisted 中这个接口(interface)的提供者也提供了那个属性。

你想要做的是包装一个ServerProtocol在你的周围ITerminalProtocol ,在此过程中将用作您的 ITerminalTransport .您可以在 twisted.conch.stdio 中看到此组合的一个简单示例.