首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用TAP Harness中的函数而不是测试文件

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

Stack Overflow用户
提问于 2013-05-16 17:41:25
回答 1查看 458关注 0票数 4

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

代码语言:javascript
复制
use TAP::Harness;
my $harness = TAP::Harness->new();
$harness->runtests(['sequential.t']);

我希望避免使用测试文件,而是直接调用Perl函数。类似于:

代码语言:javascript
复制
my %args = (
  exec => run_all_tests(),
);
$harness->runtests();
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-17 02:23:58

主要问题是所有现代test模块都在幕后使用Test::Builder

Test::Builder本身假设您在一个文件中只有一组测试。

因此,我们需要为每组测试(Test::More->builder->reset)重置单例。

代码语言:javascript
复制
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);
代码语言:javascript
复制
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
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16584001

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档