Perl:测试输入阅读器? [英] Perl: Testing an input reader?
问题描述
有没有一种方法可以使用标准的Test等模块自动测试Perl程序是否正在从诸如STDIN正确吗?例如.测试一个程序,该程序从STDIN读取两个整数并打印它们的和.
Is there a way to automatically test using the standard Test etc. modules whether a Perl program is reading input from e.g. STDIN properly? E.g. testing a program that reads two integers from STDIN and prints their sum.
推荐答案
目前尚不清楚您的意思,我会假设您要编写一个测试脚本来测试您的主程序,这是测试的一部分,我会答应需要通过STDIN传递测试输入数据.
It's not 100% clear what you mean, I'll asnswer assuming you want to write a test script that tests your main program, which as part of the test needs to have test input data passed via STDIN.
如果程序输出所读取的内容,则可以轻松地做到这一点.您不需要特殊的测试模块-只需:
You can easily do that if your program outputs what it reads. You don't need a special test module - simply:
-
通过系统调用调用您要测试的程序
Call your program your're testing via a system call
-
使用
redirect both STDIN and STDOUT of tested program to your test script, using
-
IPC::Open2
模块可通过管道打开两侧文件句柄
IPC::Open2
module to open both sides via pipes to filehandles,
...或者,构建命令以重定向到文件/从文件重定向并在测试脚本中读取/写入文件
... OR, build your command to redirect to/from files and read/write the files in the test script
从上一步中收集的测试程序中检查STDOUT,以确保打印正确的值.
Check STDOUT from tested program that you collected in the last step to make sure correct values are printed.
这篇关于Perl:测试输入阅读器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!