代码之家  ›  专栏  ›  技术社区  ›  aczarnowski

我能知道所有的测试是否通过Perl的测试::更多?

  •  6
  • aczarnowski  · 技术社区  · 16 年前

    我有一个用 Test::More . 在退出之前,如果所有测试都通过了,我想执行一些清理操作。如果有任何测试失败,我想保留所有的地方进行故障排除。

    里面有旗子吗 测试:更多 ,或者在单个测试脚本中使用其他一些最佳实践,以在测试本身完成后判断“一切正常”吗?

    2 回复  |  直到 16 年前
        1
  •  10
  •   Ether    16 年前

    您可以访问测试的当前状态。 Test::Builder ,可通过 Test::More->builder :

    use strict;
    use warnings;
    use Test::More tests => 1;
    
    ok(int rand 2, 'this test randomly passes or fails');
    
    if (Test::More->builder->is_passing)
    {
        print "hooray!\n";
    }
    else
    {
        print "aw... :(\n";
    }
    

    或者,你可以在脚本末尾进行清理,但是如果事情出错,你可以提前退出。 Test::More BAIL_OUT("reason why you are bailing");

    关于测试的状态,你可以收集很多其他的数据和统计数据; 测试::生成器 .

        2
  •  0
  •   philant    16 年前

    这里是我为了避免“无法定位对象方法”错误而提出的答案:

    #! /usr/bin/perl 
    
    use strict;
    use warnings;
    use Test::More tests => 1;
    
    ok(int rand 2, 'this test randomly passes or fails');
    
    my $FAILcount = 0;
    foreach my $detail (Test::Builder->details()) {
        if (${%$detail}{ok}==0) { $FAILcount++; }
    }
    
    if ($FAILcount == 0) {
        print "hooray!\n";
    } else {
        print "aw... :(\n";
    }
    

    在solaris 10上,使用perl v5.8.4(有31个注册的补丁),我得到了以下结果

    Can't locate object method "is_passing" via package "Test::Builder"