TAP::Harness timing issue with TAP::Formatter::JUnit

756 views Asked by At

I have a set of scripts that produces a JUnit output for Jenkins.

The code I execute looks like this (this is just a snippet so you get the idea) :

#!/usr/bin/env perl                                                                                                                            

use strict;                                                                                                                                    
use warnings;                                                                                                                                  

use Test::More;                                                                                                                                
use TAP::Harness;                                                                                                                              
use Test::Builder;                                                                                                                             

my $runner = sub {                                                                                                                             
    my ($harness,$test) = @_;                                                                                                                  
    sleep(2);                                                                                                                                 
    my $builder = Test::Builder->new;                                                                                                          
    $builder->reset;                                                                                                                           
    $builder->output( \my ($out) );                                                                                                            
    $builder->failure_output( \$out );                                                                                                         
    $builder->todo_output( \$out );                                                                                                            
    $builder->is_eq($test,'test', 'Test is test');                                                                                             
    done_testing();                                                                                                                            
    return $out;                                                                                                                               
};                                                                                                                                             

my $h = TAP::Harness->new( {                                                                                                                   
    formatter_class => 'TAP::Formatter::JUnit',                                                                                                
    merge => 1,                                                                                                                                
    exec => $runner,                                                                                                                           
    verbosity => 1,                                                                                                                            
    timer => 1,                                                                                                                                
});                                                                                                                                            

$h->runtests( ['test']); 

When I run this with the interpreter, I get the following output :

<testsuites>
  <testsuite failures="0"
             errors="0"
             time="0.000340938568115234"
             tests="1"
             name="test">
    <testcase time="9.79900360107422e-05" name="1 - Test is test"></testcase>
    <testcase time="8.29696655273438e-05" name="(teardown)" />
    <system-out><![CDATA[ok 1 - Test is test
1..1
]]></system-out>
    <system-err></system-err>
  </testsuite>
</testsuites>

The main issue here is that the JUnit output seems to get the timing wrong. As per the sleep(2) instruction, it should have reported 2s.

Is there a way to get the timing in the JUnit file right ?

1

There are 1 answers

4
hmatt1 On

I'm interested in getting this to work, but here's what I've had success with so far. It seems to work better when split out into a separate test (.t) file.

The test runner looks like this:

#!/usr/bin/env perl                                                                                                                            
use strict;                                                                                                                                    
use warnings;                                                                                                                                  
use Test::More;                                                                                                                                
use TAP::Harness;                                                                                                                              
use Test::Builder;   

my $harness = TAP::Harness->new({
    formatter_class => 'TAP::Formatter::JUnit',
    merge           => 1,
    verbosity       => 1,
    normalize       => 1,
    color           => 1,
    timer           => 1,
});

$harness->runtests('simple.t');

and the test file looks like this:

#!/usr/bin/env perl
use Test::More;

ok(sleep(2), "Sleep test");

done_testing;

Here is the output:

<testsuites>
  <testsuite failures="0"
             errors="0"
             time="2.05175995826721"
             tests="1"
             name="simple_t">
    <testcase time="2.04811692237854" name="1 - Sleep test"></testcase>
    <testcase time="0.00308394432067871" name="(teardown)" />
    <system-out><![CDATA[ok 1 - Sleep test
1..1
]]></system-out>
    <system-err></system-err>
  </testsuite>
</testsuites>