Perl测试 - 获取编译警告但测试仍然顽固地显示一切正常

时间:2011-08-10 11:49:46

标签: perl testing

我在运行测试时得到了这个,但正如您所看到的,两个测试仍然显示正常。我根本不喜欢这个。如何将警告变为错误?

1..2
Allowing a native trait to automatically supply a default is deprecated. You can avoid this warning by supplying a default, builder, or making the attribute required at UserInfo.pm line 7
        require UserInfo.pm called at (eval 4) line 2
        main::BEGIN() called at UserInfo.pm line 0
        eval {...} called at UserInfo.pm line 0
        eval 'package main;
use UserInfo @{$args[0]};
1;

;' called at /packages/run.64/perl-5.14.1/lib/5.14.1/Test/More.pm line 885
        Test::More::_eval('package main;\x{a}use UserInfo @{$args[0]};\x{a}1;\x{a}', 'ARRAY(0x16fa110)') called at /packages/run.64/perl-5.14.1/lib/5.14.1/Test/More.pm line 860
        Test::More::use_ok('UserInfo') called at UserInfo.t line 7
ok 1 - use UserInfo;
ok 2 - require UserInfo;

3 个答案:

答案 0 :(得分:3)

将警告变为错误?你来了!

use warnings FATAL=>"all";

如果您希望脚本不会死,但要使特定测试失败,您可以将其添加到上面:

use Test::Exception; 

# later
lives_and {
     is ($this->value, 42);
} "Value is 42 (and no warnings)";

Test::Exception

另一种方式(正如我在下面的评论中所述)是定义一个警告伪信号处理程序:

my @warn;
$SIG{__WARN__} = sub { push @warn, shift; };

# later
ok (!@warn, "No warnings were emitted");

答案 1 :(得分:3)

不推荐使用的功能仍有效,但会向您发出信号,表示它们会消失。停止使用已弃用的功能来消除警告。

要测试代码是否在没有警告的情况下运行,请尝试Test::NoWarnings

答案 2 :(得分:-1)

我最终使用$SIG{__WARN__} = sub { warn $_[0]; die };

即使发出警告,这也会强制测试报告错误,并且仍然会自行打印警告。