我有一组为Jenkins生成JUnit输出的脚本。
我执行的代码看起来像这样(这只是一个片段,所以你明白了):
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
use TAP::Harness;
use Test::Builder;
my $runner = sub {
my ($harness,$test) = @_;
sleep(2);
my $builder = Test::Builder->new;
$builder->reset;
$builder->output( \my ($out) );
$builder->failure_output( \$out );
$builder->todo_output( \$out );
$builder->is_eq($test,'test', 'Test is test');
done_testing();
return $out;
};
my $h = TAP::Harness->new( {
formatter_class => 'TAP::Formatter::JUnit',
merge => 1,
exec => $runner,
verbosity => 1,
timer => 1,
});
$h->runtests( ['test']);
当我使用解释器运行时,我得到以下输出:
<testsuites>
<testsuite failures="0"
errors="0"
time="0.000340938568115234"
tests="1"
name="test">
<testcase time="9.79900360107422e-05" name="1 - Test is test"></testcase>
<testcase time="8.29696655273438e-05" name="(teardown)" />
<system-out><![CDATA[ok 1 - Test is test
1..1
]]></system-out>
<system-err></system-err>
</testsuite>
</testsuites>
这里的主要问题是JUnit输出似乎错误。根据sleep(2)指令,它应该报告2s。
有没有办法让JUnit文件中的计时正确?
答案 0 :(得分:0)
我有兴趣让它发挥作用,但这是我迄今为止取得的成功。当拆分成单独的测试(.t
)文件时,它似乎工作得更好。
测试运行器如下所示:
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
use TAP::Harness;
use Test::Builder;
my $harness = TAP::Harness->new({
formatter_class => 'TAP::Formatter::JUnit',
merge => 1,
verbosity => 1,
normalize => 1,
color => 1,
timer => 1,
});
$harness->runtests('simple.t');
,测试文件如下所示:
#!/usr/bin/env perl
use Test::More;
ok(sleep(2), "Sleep test");
done_testing;
这是输出:
<testsuites>
<testsuite failures="0"
errors="0"
time="2.05175995826721"
tests="1"
name="simple_t">
<testcase time="2.04811692237854" name="1 - Sleep test"></testcase>
<testcase time="0.00308394432067871" name="(teardown)" />
<system-out><![CDATA[ok 1 - Sleep test
1..1
]]></system-out>
<system-err></system-err>
</testsuite>
</testsuites>