捕获并拆分TAP输出/结果 [英] Capture and split TAP Output/Result

查看:101
本文介绍了捕获并拆分TAP输出/结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想捕获脚本的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

罗莉

解决方案

一种简单的捕获输出的方法是使用 --archive标志来证明.这会将测试套件的输出以及结果摘要保存在压缩包中.您还应该使用--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";
}

如果由于某种原因您不能/不使用证明,则可以将捕获代码插入脚本中.我会强烈建议您反对,因为您必须针对每个测试脚本执行此操作,因此必须将其硬编码到测试中,这使得它们对正常测试的用处不大(例如,通过证明或测试来运行测试) :: Harness(证明只是包装器).您还必须做一些花哨的工作,以确保您捕获了测试的完整输出,所有警告发送到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文档.

i want to catch the TAP output of my scripts, to write it with some additional information into an openoffice document for my collegues, and as normal TAP-Output for me into the console. This has to be done inside(!) my script.

I'll guess TAP::Parser is the way i should go, right? I don't know how, and i cant find an simple example. If i got a script like:

#!/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

How can i get the results of each test? To create a openoffice document is not the problem.

Is TAP::Parser the right way to do what i want?

Thx

roli

解决方案

One simple way to capture the output is with the --archive flag to prove. This will save the test suite output in a tarball along with a summary of the results. You should also use the --merge flag so STDERR is captured.

$ 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

Once you have that you can read it back in at your leisure, reparse it with TAP::Parser and do what you like with it.

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";
}

If for some reason you can't/won't use prove, you can insert capture code into your script. I would HIGHLY RECOMMEND AGAINST THIS as you have to do it for every test script, it must be hard coded into the test which makes them less useful for normal testing (ie. running the test via prove or Test::Harness (which prove is just a wrapper around)). You also have to do some fancy footwork to make sure you capture the complete output of the test, any warnings going to STDERR or STDOUT, not just the test output.

So before I explain that, since you're running the test program by hand (which you shouldn't be) here's how you do it using the bash shell.

perl my_test.pl > test.out 2>&1

If that works for you, use that. It's not worth getting into hard coding it into the script.

You still have to process test.out using something like the TAP::Harness script above to get meaning out of it, but that will capture the complete output of the program. You can do this in one step, again with shell redirection.

perl my_test.pl 2>&1 | tap2oo

Where tap2oo is your program which translates TAP to an Open Office document.

这篇关于捕获并拆分TAP输出/结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆