gpt4 book ai didi

perl - 使用 TAP Harness 中的函数而不是测试文件

转载 作者:行者123 更新时间:2023-11-28 19:52:12 27 4
gpt4 key购买 nike

这是我目前使用 TAP 的测试工具:

use TAP::Harness;
my $harness = TAP::Harness->new();
$harness->runtests(['sequential.t']);

我想避免使用测试文件,而是直接调用 Perl 函数。像这样的东西:

my %args = (
exec => run_all_tests(),
);
$harness->runtests();

最佳答案

主要问题是所有现代test模块使用 Test::Builder在引擎盖下。
Test::Builder它本身假定您只会在一个文件中进行一组测试。
所以我们需要为每组测试重置单例(Test::More->builder->reset)。

use strict;
use warnings;

use TAP::Harness;
use Test::More;

my %tests = (
a => sub{
plan tests => 4;
ok 5==5, '5 == 5';
is 5, "5", 'is 5, 5';
like 5, qr'^\d$', '5 =~ /^\d$/';
is 5, 4, 'is 5, 4';
},
b => sub{
plan tests => 3;
ok !0;
ok !0;
ok !0;
},
c => sub{
plan 'no_plan';
ok !0;
done_testing;
},
d => sub{
ok !0;
done_testing;
},
);

sub runner{
my($harness,$test) = @_;

my $builder = Test::More->builder;

# reset the Test::Builder object for every "file"
$builder->reset;
$builder->{Indent} = ''; # may not be needed

# collect the output into $out
$builder->output(\my($out)); # STDOUT
$builder->failure_output(\$out); # STDERR
$builder->todo_output(\$out); # STDOUT

# run the test
$tests{$test}->();

# the output ( needs at least one newline )
return $out;
}

my $harness = TAP::Harness->new(
{
exec => \&runner,
verbosity => 1,
}
);

$harness->runtests(sort keys %tests);
a .. 
1..4
ok 1 - 5 == 5
ok 2 - is 5, 5
ok 3 - 5 =~ /^\d$/
not ok 4 - is 5, 4

# Failed test 'is 5, 4'
# at test.pl line 13.
# got: '5'
# expected: '4'
Failed 1/4 subtests
b ..
1..3
ok 1
ok 2
ok 3
ok
c ..
ok 1
1..1
ok
d ..
ok 1
1..1
ok

Test Summary Report
-------------------
a (Wstat: 0 Tests: 4 Failed: 1)
Failed test: 4
Files=4, Tests=9, 0 wallclock secs ( 0.02 usr + 0.00 sys = 0.02 CPU)
Result: FAIL

关于perl - 使用 TAP Harness 中的函数而不是测试文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16584001/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com