Capture and split TAP Output/Result

2019-06-08 11:05发布

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

标签: perl parsing tap
2条回答
手持菜刀,她持情操
2楼-- · 2019-06-08 11:46

You could write a plugin for App::Prove. A good reference/starting point is the Test::Pretty.

查看更多
看我几分像从前
3楼-- · 2019-06-08 11:50

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.

查看更多
登录 后发表回答