How to convert a generated text file to Junit format(XML) using Perl
I have a text file generated which is in the format:
Tests started on Fri Oct 19 14:11:35 2018
Test File Comparison Result
========= =================
abc.msg FAILED
aa.msg PASSED
bb.msg TO BE VALIDATED
Tests finished on Fri Oct 19 14:12:01 2018
Expected JUnit Format:
Please find attached the snip with the expected xml format
I want to convert the above text file after being generated from a Perl script to an XML file using a Perl script.
Any help would be appreciated. Thanks in advance!!
TAP::Formatter::JUnit has tap2junit
command that convert TAP format text into JUnit XML. All you have to do is to create a filter that can read your test result and convert it to TAP format, just like:
#!/usr/bin/perl
use strict;
use warnings;
my @t;
while (my $line = <STDIN>) {
$line =~ s/\R//;
if (my ($msg, $result) = $line =~ /^(.*?)\s*(PASSED|FAILED)$/) {
if ($result eq 'PASSED') {
push @t, ['ok' => $msg];
}
elsif ($result eq 'FAILED') {
push @t, ['not ok' => $msg];
}
}
}
die "No test" if @t == 0;
printf "1..%d\n", scalar @t;
for my $i (0 .. $#t) {
printf "%s %d - %s\n", $t[$i]->[0], $i + 1, $t[$i]->[1];
}
1;
Save your test result as customtest.txt
then run cat customtest.txt | perl custom2tap.pl | tap2junit -
, you can have following output:
<testsuites>
<testsuite failures="1" errors="0" name="-" tests="3">
<testcase name="1 - abc.msg">
<failure message="not ok 1 - abc.msg"
type="TestFailed"><![CDATA[not ok 1 - abc.msg]]></failure>
</testcase>
<testcase name="2 - aa.msg"></testcase>
<testcase name="3 - bb.msg"></testcase>
<system-out><![CDATA[1..3
not ok 1 - abc.msg
ok 2 - aa.msg
ok 3 - bb.msg
]]></system-out>
<system-err></system-err>
</testsuite>
</testsuites>
Install Strawberry Perl, so that you can use cpan
command.
Install TAP::Formatter::JUnit
from command prompt:
> cpan -i TAP::Formatter::JUnit
Run type customtest.txt | perl custom2tap.pl | tap2junit -