Fed*_*ore 5 c++ python stdin stdout tty
我有一个由两部分组成的软件:一部分是在第一台PC上运行的python,另一部分是在第二部分上运行的cpp.它们通过串口(tty)进行通信.
我想在我的电脑上测试python端,用适当的数据提供它,看它是否按预期运行.
我开始使用子进程,但后来出现了问题:我应该提供哪个stdin和stdout?
cStringIO 不起作用,因为没有 fileno()
PIPE也不起作用,因为select.select()即使实际上没有发送任何内容,也有可读的内容
你有什么提示吗?我可以使用假的tty模块吗?
理想情况下,您应该模拟它并测试其行为,而不要过多依赖终端 IO。你可以使用mock.patch来实现。假设你想测试t_read:
@mock.patch.object(stdin, 'fileno')
@mock.patch.object(stdin, 'read')
def test_your_behavior(self, mock_read, mock_fileno):
# this should make select.select return what you expect it to return
mock_fileno.return_value = 'your expected value'
# rest of the test goes here...
Run Code Online (Sandbox Code Playgroud)
如果您可以发布至少部分您要测试的代码,我也许可以给您一个更好的例子。