我正在尝试编写一个脚本来从文本文件中读取并根据JUnit格式将其转换为XML。文本文件输出类似于:
TEST TITLE GROUP PRIO R-STAT R-TIME VERDICT VERDICT-TEXT
------------------------- ---------------------------------------- -------- ---- ------ ------------ -------- --------------------------------------------------
Telex Telex type GROUP1 1 PASS 00:00:02.914 PASS Over
Time Timer GROUP2 1 PASS 00:00:06.619 PASS Over
以下是我对上述文本文件的期望输出:
<?xml version="1.0" encoding="utf-8"?>
<testsuites name="HostTests for Character" tests="TestSuite" time="">
<testsuite name="GROUP1" package="GROUP1" tests="" errors="">
<testcase classname="GROUP1.Telex" name="Telex type" time="" status="">
<skipped/>
<error message="" type=""/>
<failure message="" type=""/>
<system-out/>
<system-err/>
<testcase/>
<testcase classname="GROUP2.Time" name="Timer" time="" status="">
<skipped/>
<error message="" type=""/>
<failure message="" type=""/>
<system-out/>
<system-err/>
<testcase/>
<textsuite/>
<testsuites/>
如何在Jenkins中编写Perl脚本来执行此操作?
我也尝试过:
#!/usr/bin/perl
use strict;
use warnings;
my $parser = XML::LibXML->new();
my $xslt = XML::LibXSLT->new();
my $textroot = $xslt ->parse_verdict_file("\\\~/jenkins/workspace/HostTestOAM/trunk/nash/test/regression/logs/verdict.1.txt");
my $checker = 1;
my @txtfile = logj('logs/verdict*.txt');
foreach (@txtfile){
my $txtfile = $_;
$txtfile =~ s/([ ()])/\\$1/g;
$txtroot2 = "\\\~/jenkins/workspace/HostTestOAM/trunk/nash/test/regression/logs/$txtfile";
my $source = $parser->parse_file($txtroot2);
my $results = $textroot->transform($source);
my $xmlparsed = "\\\~/jenkins/workspace/HostTestOAM/trunk/nash/test/regression/logs/results$checker.xml";
unless(open FILE, '>'.$xmlparsed) {
die "\Creating failed\n";
}
$textroot -> output_file($results, FILE);
close FILE;
$checker++;
}
答案 0 :(得分:0)
在我看来,您需要做的是为您希望XML的外观构建一个模板,并为可能发生变化的位置放置占位符。说,
my $template = '<testsuite name="XXXGROUPXXX" package="XXXGROUPXXX" tests="" errors="">
<testcase classname="XXXGROUPXXX.XXXTESTXXX" name="XXXTITLEXXX" time="" status="">';
接下来,您必须在文件中读取表格数据,这在Perl中很容易:通常的习惯用法。 (为了节省时间,我不在这里写语法Perl ......)
open(IN, $file);
while ($line = <IN>)
{
chomp($line);
# skip noise, blanks: next if (blah)
# Now split the line into fields so you can extract what you need
($test, $title, $group, $the_rest) = split($line);
# and substitute the values read for test, title, group into a template
# rinse and repeat;
($foo = $template) =~ s/XXXGROUPXXX/$group);
#print the new text out
print $foo;
}
close(IN);
答案 1 :(得分:0)
现在我尝试了另一种方式,我觉得这很好。以下是代码行:
#!/usr/bin/perl
use strict;
use warnings;
use XML::Writer;
use XML::Simple;
my $file = 'logs/verdict*.txt';
my %testsuite = (testcase => [
{classname => 'CELL.NbapCellDelete', name => 'Cell deletion', time => '%file', status => '%file'},]
);
my $xmlObj = XML::Simple->new(RootName => 'testsuite');
print $xmlObj->XMLout (\%testsuite, noattr => 1, xmldecl => => '<?xml version="1.0" encoding="UTF-8"?>');
问题在于时间和状态,我的想法是创建一个动态数组或动态哈希来存储时间和状态,然后将它们推入xml文件,也许比较该动态数组和变量之间的值。你能帮忙吗?