xml - 如何使用 Perl 将生成的文本文件转换为 Junit 格式(XML)

标签 xml perl text junit perlscript

enter image description here如何使用 Perl 将生成的文本文件转换为 Junit 格式(XML)

我生成了一个文本文件,其格式为:

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

预期的 JUnit 格式:

请查找随附的具有预期 xml 格式的片段

我想使用 Perl 脚本将上面的文本文件从 Perl 脚本生成后转换为 XML 文件。

如有任何帮助,我们将不胜感激。提前致谢!!

enter image description here

最佳答案

TAP::Formatter::JUnittap2junit 命令可以转换 TAP format文本转换为 JUnit XML。您所要做的就是创建一个可以读取您的测试结果并将其转换为 TAP 格式的过滤器,就像:

custom2tap.pl

#!/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;

将测试结果保存为 customtest.txt,然后运行 ​​cat customtest.txt | perl custom2tap.pl | tap2junit - ,您可以得到以下输出:

<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>

窗口

安装Strawberry Perl ,以便您可以使用cpan命令。

从命令提示符安装TAP::Formatter::JUnit:

> cpan -i TAP::Formatter::JUnit

运行输入customtest.txt | perl custom2tap.pl | tap2junit -

enter image description here

关于xml - 如何使用 Perl 将生成的文本文件转换为 Junit 格式(XML),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52953305/

相关文章:

java - JAXB 返回 "null"字符串而不是 null

java - Android Layout内容显示问题

c# - 序列化强类型对象列表

perl - 如何在 Perl 中检查多个模式匹配

css - 由文本表示的图像 - 它叫什么?

python - 找不到模型 'en_core_web_md' 。它似乎不是快捷方式链接、Python 包或数据目录的有效路径

Android TextView 对齐文本

xml - 如何使用 XStream 框架对 UTF-8 进行编码?

regex - 解析字符串时的Perl while循环

json - 逐 block 解析 JSON