【问题标题】:How to convert a generated text file to Junit format(XML) using Perl如何使用 Perl 将生成的文本文件转换为 Junit 格式(XML)
【发布时间】:2019-01-25 02:27:17
【问题描述】:

如何使用 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 文件。

任何帮助将不胜感激。提前致谢!!

【问题讨论】:

  • 你有什么问题?
  • 您应该将预期的输出放在您的问题中,格式正确,以便阅读。
  • @randomguy 不管怎样,根据上面的 junit5,我创建了一个补丁 (gist.github.com/ernix/c692ecb3233d60947d19cac10ec9fa2a) 来解析以 TAP 格式 (testanything.org/tap-specification.html#skipping-tests) 描述的 SKIP 指令。请修改 custm2tap.pl 解析 TO BE VALIDATED 并将 ['ok' => '# SKIP $msg']; 推送到 @t,修补 TAP::Formatter::JUnittap2junit 命令现在可以正确解析 SKIP 指令。
  • @randomguy 您需要安装TAP::Formatter::JUnit 的补丁版本,TAP::Formatter::JUnit 的原始版本(0.11)不解析 SKIP/TODO 指令。您还需要将['ok' => '# SKIP $msg'] 更改为['ok' => "$msg # SKIP"] 以满足您的期望。而且我们非常需要生成 XML 规范(XSD),而不是示例。你在哪里使用你的 JUnit 报告?詹金斯?

标签: xml perl text junit perlscript


【解决方案1】:

TAP::Formatter::JUnit 具有将 TAP format 文本转换为 JUnit XML 的 tap2junit 命令。您所要做的就是创建一个过滤器,它可以读取您的测试结果并将其转换为 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

运行type customtest.txt | perl custom2tap.pl | tap2junit -

【讨论】:

  • 非常感谢@ernix 的回答。但是当我在 cmd 提示符下执行命令时,出现以下错误:“'tap2junit' 未被识别为内部或外部命令”。我需要安装库“TAP::Formatter::JUnit”吗?
  • @DhinakaranKannan 你能给我你的系统信息吗?你是如何安装 Perl 的?
  • 我的 Windows 系统有 ActivePerl 5.14.2 Build 1402。它是一个 .msi 文件。刚刚安装好了。
  • @DhinakaranKannan 尝试重定向:type customtest.txt | perl custom2tap.pl | tap2junit - &gt; result.xml
  • 关注:stackoverflow.com/questions/4727480/…。仔细检查您的 PATH 环境变量。关闭命令提示符窗口并重新打开它。 ...或尝试type customtest.txt | perl custom2tap.pl | perl path/to/perl/bin/tap2junit.fatpack.pl - &gt; result.xml
猜你喜欢
  • 2017-03-08
  • 1970-01-01
  • 2012-06-05
  • 1970-01-01
  • 2021-10-03
  • 2021-04-29
  • 1970-01-01
  • 1970-01-01
  • 2021-12-08
相关资源
最近更新 更多