我想抓住我的脚本的TAP输出,将其中的一些附加信息写入我的同事的openoffice文档中,并将其作为正常的TAP-Output输入控制台。这必须在(!)我的脚本内完成。
我猜TAP :: Parser是我应该去的方式,对吗?我不知道怎么样,我找不到一个简单的例子。如果我有一个像这样的脚本:
#!/usr/bin/perl
use strict;
use warnings;
use Test::More tests => 2;
is( 1 + 1, 2, "one plus one is two" );
#missing code to capture the result of the test above
is( 1 + 1, 11, "one plus one is more than two" );
#missing code to capture the result of the test above
我如何获得每项测试的结果?创建一个openoffice文档不是问题。
TAP :: Parser是正确的做法吗?
THX
roli
答案 0 :(得分:2)
捕获输出的一种简单方法是使用the --archive
flag to prove。这将在tarball中保存测试套件输出以及结果摘要。您还应该使用--merge
标志,以便捕获STDERR。
$ prove --archive test_out.tgz --merge my_test.pl
my_test.pl .. ok
All tests successful.
Files=1, Tests=3, 0 wallclock secs ( 0.01 usr 0.00 sys + 0.01 cusr 0.00 csys = 0.02 CPU)
Result: PASS
TAP Archive created at /home/you/test_out.tgz
一旦你拥有了它,你可以在闲暇时阅读它,用TAP :: Parser重新解析它并用它做你喜欢的事。
use TAP::Parser;
my $tap_file = shift;
open my $tap_fh, $tap_file or die $!;
# Can't just pass in the .t file, it will try to execute it.
my $parser = TAP::Parser->new({
source => $tap_fh
});
while ( my $result = $parser->next ) {
# do whatever you like with the $result, like print it back out
print $result->as_string, "\n";
}
如果由于某种原因你不能/不会使用证明,你可以在你的脚本中插入捕获代码。我会强烈反对这个,因为你必须为每个测试脚本做它,它必须硬编码到测试中,这使得它们对正常测试不太有用(即通过证明或测试运行测试) ::线束(证明只是一个包装))。您还必须做一些花哨的步法,以确保捕获测试的完整输出,任何警告转到STDERR或STDOUT,而不仅仅是测试输出。
所以在我解释之前,因为你手动运行测试程序(你不应该这样做),这就是你使用bash shell的方法。
perl my_test.pl > test.out 2>&1
如果适合您,请使用它。将其硬编码到脚本中是不值得的。
你仍然需要使用类似上面的TAP :: Harness脚本来处理test.out以获得意义,但这将捕获程序的完整输出。您可以一步完成此操作,再次使用shell重定向。
perl my_test.pl 2>&1 | tap2oo
tap2oo是您的程序,它将TAP转换为Open Office文档。
答案 1 :(得分:1)
你可以写一个plugin for App::Prove。一个很好的参考/起点是Test::Pretty。