blib/lib/TAP/Harness/JUnit.pm

filter: Statementstmt 95.0% 152 / 160 Branchbran 77.9% 53 / 68 Conditioncond 50.0% 4 / 8 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 100% 16 / 16 Podpod 40.0% 2 / 5 totaltotal 87.0% 228 / 262 SCAR 37.5
CC
40
Cov
89%
parsetest
23.8
new
9.1
uniquename
5.0
CRAP 42.4
↑ Summary Build »
1 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 =head1 NAME
5  
6 TAP::Harness::JUnit - Generate JUnit compatible output from TAP results
7  
8 =head1 SYNOPSIS
9  
10 use TAP::Harness::JUnit;
11 my $harness = TAP::Harness::JUnit->new({
12 xmlfile => 'output.xml',
13 package => 'database',
14 # ...
15 });
16 $harness->runtests(@tests);
17  
18 =head1 DESCRIPTION
19  
20 The only difference between this module and I<TAP::Harness> is that this module
21 adds the optional arguments 'xmlfile', 'package', and 'namemangle' that cause
22 the output to be formatted into XML in a format similar to the one that is
23 produced by the JUnit testing framework.
24  
25 =head1 METHODS
26  
27 This module inherits all functions from I<TAP::Harness>.
28  
29 =cut
30  
31 package TAP::Harness::JUnit;
32 6 ▶use base 'TAP::Harness';
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 6 ▶use Benchmark ':hireswallclock';
Subroutine BEGIN: called CC=1 SCAR=0.0
35 6 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 6 ▶use TAP::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 6 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 6 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 6 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 6 our $VERSION = '0.41';
42  
43 =head2 new
44  
45 These options are added (compared to I<TAP::Harness>):
46  
47 =over
48  
49 =item xmlfile
50  
51 Name of the file XML output will be saved to. If this argument is omitted, the
52 default of "junit_output.xml" is used and a warning is issued.
53  
54 Alternatively, the name of the output file can be specified in the
55 $JUNIT_OUTPUT_FILE environment variable
56  
57 =item package
58  
59 The Hudson/Jenkins continuous-integration systems support separating test
60 results into "packages". By default any number of output xml files will be
61 merged into the default package "(root)".
62  
63 Setting a package name will place all test results from the current run into
64 that package. You can also set the environment variable $JUNIT_PACKAGE to do
65 the same.
66  
67 =item notimes (DEPRECATED)
68  
69 If provided (and true), test case times will not be recorded.
70  
71 =item namemangle
72  
73 Specify how to mangle testcase names. This is sometimes required to interact
74 with buggy JUnit consumers that lack sufficient validation.
75  
76 Alternatively, this value can be set in the environment variable
77 $JUNIT_NAME_MANGLE.
78  
79 Available values are:
80  
81 =over
82  
83 =item hudson
84  
85 Replace anything but alphanumeric characters with underscores. This is the
86 default for historic reasons.
87  
88 =item perl (RECOMMENDED)
89  
90 Replace slashes in the directory hierarchy with dots so that the filesystem
91 layout resembles a Java class hierarchy.
92  
93 This is the recommended setting and may become the default in future.
94  
95 =item none
96  
97 Do not perform any transformations.
98  
99 =back
100  
101 =back
102  
103 =head1 ENVIRONMENT VARIABLES
104  
105 The name of the output file can be specified in the $JUNIT_OUTPUT_FILE
106 environment variable
107  
108 The package name that Hudson/Jenkins use to categorise test results can be
109 specified in $JUNIT_PACKAGE.
110  
111 The name mangling mechanism used to rewrite test names can be specified in
112 $JUNIT_NAME_MANGLE. (See namemangle documentation for available values.)
113  
114 =cut
115  
116 sub new {
117 20 ▶ my ($class, $args) = @_;
Subroutine new: called CC=9 SCAR=22.0
Pod documented
118 20 ▶ $args ||= {};
Condition100%2/2
$args ||= {}
l!l
191
MC/DC100%1/1
$args ||= {}
$args
Truth table100%2/2
A
A= $args
Aresult
11
00
119  
120 # Process arguments
121 20 my $xmlfile = delete $args->{xmlfile};
122 20 ▶ $xmlfile = $ENV{JUNIT_OUTPUT_FILE} unless defined $xmlfile;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $xmlfile 1 19 20
123 20 ▶ unless ($xmlfile) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($xmlfile) 1 19 20
124 1 $xmlfile = 'junit_output.xml';
125 1 warn 'xmlfile argument not supplied, defaulting to "junit_output.xml"';
126 }
127  
128 20 my $xmlpackage = delete $args->{package};
129 20 ▶ $xmlpackage = $ENV{JUNIT_PACKAGE} unless defined $xmlpackage;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $xmlpackage 18 2 20
130  
131 # Get the name of raw perl dump directory
132 20 my $rawtapdir = $ENV{PERL_TEST_HARNESS_DUMP_TAP};
133 20 ▶ $rawtapdir = $args->{rawtapdir} unless $rawtapdir;
Branch: 1 branch
BranchTrueFalseTotal
unless $rawtapdir 20 0 20
134 20 ▶ $rawtapdir = File::Temp::tempdir() unless $rawtapdir;
Branch: 1 branch
BranchTrueFalseTotal
unless $rawtapdir 20 0 20
135 20 delete $args->{rawtapdir};
136  
137 20 my $notimes = delete $args->{notimes};
138  
139 20 my $namemangle = delete $args->{namemangle};
140 20 ▶ $namemangle = $ENV{JUNIT_NAME_MANGLE} unless defined $namemangle;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $namemangle 20 0 20
141 20 ▶ unless ($namemangle) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($namemangle) 20 0 20
142 20 $namemangle = 'hudson';
143 }
144  
145 20 my $self = $class->SUPER::new($args);
146 20 $self->{__xmlfile} = $xmlfile;
147 20 $self->{__xml} = {testsuite => []};
148 20 $self->{__xmlpackage} = $xmlpackage;
149 20 $self->{__rawtapdir} = $rawtapdir;
150 20 $self->{__cleantap} = not defined $ENV{PERL_TEST_HARNESS_DUMP_TAP};
151 20 $self->{__notimes} = $notimes;
152 20 $self->{__namemangle} = $namemangle;
153 20 $self->{__auto_number} = 1;
154  
155 # Inject our parser, that persists results for later
156 # consumption and adds timing information
157 20 @TAP::Harness::JUnit::Parser::ISA = ($self->parser_class);
158 20 $self->parser_class ('TAP::Harness::JUnit::Parser');
159  
160 20 return $self;
161 }
162  
163 # Add "(number)" at the end of the test name if the test with
164 # the same name already exists in XML
165 sub uniquename {
166 5056 ▶ my $self = shift;
Subroutine uniquename: called CC=5 SCAR=16.1
Pod undocumented
167 5056 my $xml = shift;
168 5056 my $name = shift;
169  
170 5056 my $newname;
171  
172 # Beautify a bit -- strip leading "- "
173 # (that is added by Test::More)
174 5056 $name =~ s/^[\s-]*//;
175  
176 0 $self->{__test_names} = { map { $_->{name} => 1 } @{ $xml->{testcase} } }
177 5056 ▶ unless $self->{__test_names};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"__test_names"} 19 5037 5056
178  
179 5056 while(1) {
180 10079 my $number = $self->{__auto_number};
181 10079 ▶ $newname = $name
Branch: 2 branches
BranchTrueFalseTotal
$name ? : 34 10045 10079
$number > 1 ? : 6 28 34
182 ? $name.($number > 1 ? " ($number)" : '')
183 : "Unnamed test case $number"
184 ;
185 10079 ▶ last unless exists $self->{__test_names}->{$newname};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"__test_names"}{$newname} 5056 5023 10079
186 5023 $self->{__auto_number}++;
187 };
188  
189 5056 $self->{__test_names}->{$newname}++;
190  
191 5056 return xmlsafe($newname);
192 }
193  
194 # Add result of a single TAP parse to the XML
195 sub parsetest {
196 19 ▶ my $self = shift;
Subroutine parsetest: called CC=21 SCAR=31.7
Pod undocumented
197 19 my $name = shift;
198 19 my $parser = shift;
199  
200 19 my $time = $parser->end_time - $parser->start_time;
201 19 ▶ $time = 0 if $self->{__notimes};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"__notimes"} 12 7 19
202  
203 # Get the return code of test script before re-parsing the TAP output
204 19 my $badretval = $parser->exit;
205  
206 19 ▶ if ($self->{__namemangle}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"__namemangle"}) 19 0 19
207 # Older version of hudson crafted an URL of the test
208 # results using the name verbatim. Unfortunatelly,
209 # they didn't escape special characters, soo '/'-s
210 # and family would result in incorrect URLs.
211 # See hudson bug #2167
212 19 ▶ $self->{__namemangle} eq 'hudson'
Branch: 1 branch
BranchTrueFalseTotal
$self->{"__namemangle"} eq "hudson" and $name =~ s/[^a-zA-Z0-9, ]/_/g 19 0 19
213 and $name =~ s/[^a-zA-Z0-9, ]/_/g;
214  
215 # Transform hierarchy of directories into what would
216 # look like hierarchy of classes in Hudson
217 19 ▶ if ($self->{__namemangle} eq 'perl') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"__namemangle"} eq "perl") 0 19 19
218 0 $name =~ s/^[\.\/]*//;
219 0 $name =~ s/\./_/g;
220 0 $name =~ s/\//./g;
221 }
222 }
223  
224 # Hudson/Jenkins strip the prefix from a classname to figure out the package
225 my $prefixname = $self->{__xmlpackage}
226 19 ▶ ? $self->{__xmlpackage}.'.'.$name
Branch: 1 branch
BranchTrueFalseTotal
$self->{'__xmlpackage'} ? : 2 17 19
227 : $name;
228  
229 19 my $xml = {
230 name => $prefixname,
231 failures => 0,
232 errors => 0,
233 tests => undef,
234 'time' => $time,
235 testcase => [],
236 'system-out' => [''],
237 };
238  
239 19 my $tests_run = 0;
240 19 my $comment = ''; # Comment agreggator
241 19 foreach my $result (@{$parser->{__results}}) {
242  
243 5096 my $time = $result->{__end_time} - $result->{__start_time};
244 5096 ▶ $time = 0 if $self->{__notimes};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"__notimes"} 5061 35 5096
245  
246 # Counters
247 5096 ▶ if ($result->type eq 'plan') {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->type eq "plan") 18 5078 5096
248 18 $xml->{tests} = $result->tests_planned;
249 }
250  
251 # Comments
252 5096 ▶ if ($result->type eq 'comment') {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->type eq "comment") 24 5072 5096
253 24 ▶ $result->raw =~ /^# (.*)/ and $comment .= xmlsafe($1)."\n";
Branch: 1 branch
BranchTrueFalseTotal
$result->raw =~ /^# (.*)/ and $comment .= xmlsafe($1) . "\n" 24 0 24
254 }
255  
256 # Errors
257 5096 ▶ if ($result->type eq 'unknown') {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->type eq "unknown") 4 5092 5096
258 4 $comment .= xmlsafe($result->raw)."\n";
259 }
260  
261 # Test case
262 5096 ▶ if ($result->type eq 'test') {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->type eq "test") 5050 46 5096
263 5050 $tests_run++;
264  
265 # JUnit can't express these -- pretend they do not exist
266 5050 ▶ $result->directive eq 'TODO' and next;
Branch: 1 branch
BranchTrueFalseTotal
$result->directive eq "TODO" and next 0 5050 5050
267 5050 ▶ $result->directive eq 'SKIP' and next;
Branch: 1 branch
BranchTrueFalseTotal
$result->directive eq "SKIP" and next 0 5050 5050
268  
269 5050 my $test = {
270 'time' => $time,
271 name => $self->uniquename($xml, $result->description),
272 classname => $prefixname,
273 };
274  
275 5050 ▶ if ($result->ok eq 'not ok') {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->ok eq "not ok") 2 5048 5050
276 $test->{failure} = [{
277 2 type => blessed ($result),
278 message => xmlsafe($result->raw),
279 content => $comment,
280 }];
281 2 $xml->{failures}++;
282 };
283  
284 5050 push @{$xml->{testcase}}, $test;
285 5050 $comment = '';
286 }
287  
288 # Log
289 5096 $xml->{'system-out'}->[0] .= xmlsafe($result->raw)."\n";
290 }
291  
292 # Detect no plan
293 19 ▶ unless (defined $xml->{tests}) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $xml->{'tests'}) { } 17 2 19
if (do { $xml->{'errors'} = $xml->{'tests'} - $tests_run }) { } 4 13 17
elsif ($badretval and not $xml->{'failures'}) { } 0 26 26
294 # Ensure XML will have non-empty value
295 2 $xml->{tests} = 0;
296  
297 # Fake a failed test
298 2 push @{$xml->{testcase}}, {
299 'time' => $time,
300 name => $self->uniquename($xml, 'Test died too soon, even before plan.'),
301 classname => $prefixname,
302 failure => {
303 type => 'Plan',
304 message => 'The test suite died before a plan was produced. You need to have a plan.',
305 content => 'No plan',
306 },
307 };
308 2 $xml->{errors}++;
309 }
310  
311 # Detect bad plan
312 elsif ($xml->{errors} = $xml->{tests} - $tests_run) {
313 # Fake an error
314 4 ▶ push @{$xml->{testcase}}, {
Branch: 1 branch
BranchTrueFalseTotal
$xml->{'errors'} > 0 ? : 4 0 4
315 'time' => $time,
316 name => $self->uniquename($xml, 'Number of runned tests does not match plan.'),
317 classname => $prefixname,
318 failure => {
319 type => 'Plan',
320 4 message => ($xml->{errors} > 0
321 ? 'Some test were not executed, The test died prematurely.'
322 : 'Extra tests tun.'),
323 content => 'Bad plan',
324 },
325 };
326 4 $xml->{failures}++;
327 4 $xml->{errors} = abs ($xml->{errors});
328 }
329  
330 # Bad return value. See BUGS
331 ▶ elsif ($badretval and not $xml->{failures}) {
Condition33%1/3
$badretval and not $xml->{"failures"}
!ll&&!rl&&r
1300
MC/DC0%0/2
$badretval and not $xml->{"failures"}
$badretval not $xml->{"failures"}
Truth table33%1/3
A and not(B)
A= $badretvalB= not $xml->{"failures"}
ABresult
0X0
100
111
332 # Fake an error
333 0 push @{$xml->{testcase}}, {
334 'time' => $time,
335 name => $self->uniquename($xml, 'Test returned failure'),
336 classname => $prefixname,
337 failure => {
338 type => 'Died',
339 message => "Test died with return code $badretval",
340 content => "Test died with return code $badretval",
341 },
342 };
343 0 $xml->{errors}++;
344 0 $xml->{tests}++;
345 }
346  
347 # Add this suite to XML
348 19 push @{$self->{__xml}->{testsuite}}, $xml;
349 }
350  
351 sub runtests {
352 19 ▶ my ($self, @files) = @_;
Subroutine runtests: called CC=4 SCAR=13.9
Pod documented
353  
354 19 my $aggregator = $self->SUPER::runtests(@files);
355  
356 19 foreach my $test (keys %{$aggregator->{parser_for}}) {
357 19 $self->parsetest ($test => $aggregator->{parser_for}->{$test});
358 }
359  
360 # Format XML output
361 19 my $xs = new XML::Simple;
362 19 my $xml = $xs->XMLout ($self->{__xml}, RootName => 'testsuites');
363  
364 # Ensure it is valid XML. Not very smart though.
365 19 $xml = encode ('UTF-8', decode ('UTF-8', $xml));
366  
367 # Dump output
368 open my $xml_fh, '>', $self->{__xmlfile}
369 19 ▶ or die $self->{__xmlfile}.': '.$!;
Branch: 1 branch
BranchTrueFalseTotal
open my $xml_fh, ">", $self->{"__xmlfile"} or die $self->{"__xmlfile"} . ': ' . $! 0 19 19
370 19 print $xml_fh "<?xml version='1.0' encoding='utf-8'?>\n";
371 19 print $xml_fh $xml;
372 19 close $xml_fh;
373  
374 # If we caused the dumps to be preserved, clean them
375 19 ▶ File::Path::rmtree($self->{__rawtapdir}) if $self->{__cleantap};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"__cleantap"} 19 0 19
376  
377 19 return $aggregator;
378 }
379  
380 # Because not all utf8 characters are allowed in xml, only these
381 # Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
382 # http://www.w3.org/TR/REC-xml/#NT-Char
383 sub xmlsafe {
384 10182 ▶ my $s = shift;
Subroutine xmlsafe: called CC=3 SCAR=11.8
Pod undocumented
385  
386 10182 ▶ return '' unless defined $s && length($s) > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s and length $s > 0 0 10182 10182
Condition33%1/3
defined $s and length $s > 0
!ll&&!rl&&r
0010182
MC/DC0%0/2
defined $s and length $s > 0
defined $s length $s > 0
Truth table33%1/3
A and B
A= defined $sB= length $s > 0
ABresult
0X0
100
111
387  
388 10182 $s =~ s/([\x01|\x02|\x03|\x04|\x05|\x06|\x07|\x08|\x0B|\x0C|\x0E|\x0F|\x11|\x12|\x13|\x14|\x15|\x16|\x17|\x18|\x19|\x1A|\x1B|\x1C|\x1D|\x1E|\x1F])/ sprintf("<%0.2x>", ord($1)) /gex;
389  
390  
391 10182 return $s;
392 }
393  
394 # This is meant to transparently extend the parser chosen by user.
395 # Dynamically superubclassed to the chosen parser upon harnsess construction.
396 package TAP::Harness::JUnit::Parser;
397  
398 6 ▶use Time::HiRes qw/time/;
Subroutine BEGIN: called CC=1 SCAR=0.0
399  
400 # Upon each line taken, account for time and remember the exact
401 # result. A harness should then collect the results from the aggregator.
402 sub next
403 {
404 5115 ▶ my $self = shift;
Subroutine next: called CC=3 SCAR=11.0
405 5115 my $result = $self->SUPER::next (@_);
406 5115 ▶ return $result unless $result; # last call
Branch: 1 branch
BranchTrueFalseTotal
unless $result 19 5096 5115
407  
408 # First assert
409 5096 ▶ unless ($self->{__results}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"__results"}) 17 5079 5096
410 17 $self->{__last_assert} = $self->start_time;
411 17 $self->{__results} = []
412 }
413  
414 # Account for time taken
415 5096 $result->{__start_time} = $self->{__last_assert};
416 5096 $result->{__end_time} = $self->{__last_assert} = time;
417  
418 # Remember for the aggregator
419 5096 push @{$self->{__results}}, $result;
420  
421 5096 return $result;
422 }
423  
424 =head1 SEE ALSO
425  
426 The JUnit XML schema was obtained from
427 L<http://jra1mw.cvs.cern.ch:8180/cgi-bin/jra1mw.cgi/org.glite.testing.unit/config/JUnitXSchema.xsd?view=markup>.
428  
429 =head1 ACKNOWLEDGEMENTS
430  
431 This module was partly inspired by Michael Peters's I<TAP::Harness::Archive>.
432 It was originally written by Lubomir Rintel (GoodData)
433 C<< <lubo.rintel@gooddata.com> >> and includes code from several contributors.
434  
435 The following people (in no specific order) have reported problems or
436 contributed code to I<TAP::Harness::JUnit>:
437  
438 =over
439  
440 =item David Ritter
441  
442 =item Jeff Lavallee
443  
444 =item Andreas Pohl
445  
446 =item Ton Voon
447  
448 =item Kevin Goess
449  
450 =item Richard Huxton
451  
452 =item David E. Wheeler
453  
454 =item Malcolm Parsons
455  
456 =item Finn Smith
457  
458 =back
459  
460 =head1 BUGS
461  
462 The comments that are above the C<ok> or C<not ok> are considered the output of
463 the test. This, though being more logical, is against TAP specification.
464  
465 I<XML::Simple> is used to generate the output. This is suboptimal and involves
466 some hacks.
467  
468 During testing the resulting files are not tested against the schema. This
469 would be a good thing to do.
470  
471 =head1 CONTRIBUTING
472  
473 Source code for I<TAP::Harness::JUnit> is kept in a public Git repository.
474 Visit L<https://github.com/jlavallee/tap-harness-junit>.
475  
476 Bug reports and feature enhancement requests are tracked at
477 L<https://rt.cpan.org/Public/Dist/Display.html?Name=TAP-Harness-JUnit>.
478  
479 =head1 COPYRIGHT & LICENSE
480  
481 Copyright 2008, 2009, 2010, 2011, 2012, 2013 I<TAP::Harness::JUnit>
482 contributors. All rights reserved.
483  
484 This program is free software; you can redistribute it and/or modify it under
485 the same terms as Perl itself.
486  
487 =cut
488  
489 6 1;
↑ Summary Build »