我的代码测试了一个糟糕的API调用,幸运的是代码会导致模块本身发出警告.但是当我测试失败的API调用时,我希望在TAP中看不到警告.
t/01-pass.t .............. ok
t/02-fail.t .............. ok
t/03-noversion.t ......... ok
t/04-no-file.t ........... ok
Use of uninitialized value $file in concatenation (.) or string at /home/xenoterracide/projects/Test-Version/lib/Test/Version.pm line 29.
t/05-file-not-defined.t .. ok
# unsorted oks: 001
t/06-all.t ............... ok
All tests successful.
Files=6, Tests=37, 1 wallclock secs ( 0.04 usr 0.02 sys + 0.35 cusr 0.04 csys = 0.45 CPU)
Result: PASS
Run Code Online (Sandbox Code Playgroud)
这是实际的代码
#!/usr/bin/perl
use 5.006;
use strict;
use warnings;
use Test::Tester tests => 7;
use Test::Version qw( version_ok );
check_test(
sub {
version_ok; # correct call version_ok( $file )
},
{
ok => 0,
name => 'check version in ',
diag => 'FILE_NOT_DEFINED',
},
'$file not defined'
);
Run Code Online (Sandbox Code Playgroud)
有没有办法压制警告并防止它在TAP中结束(no warnings
在原始模块之外).
你可能正在寻找Test :: Warn.它易于使用:
use strict;
use warnings;
use Test::More;
use Test::Warn;
# You might also find the following modules interesting:
# use Test::Exception;
# use Test::NoWarnings;
sub bla { warn 'bla' }
warning_is { bla() } 'bla';
done_testing;
Run Code Online (Sandbox Code Playgroud)
因此,您正在将警告从麻烦转变为预期的事物.
如果这不是你想要的,那么请看看IO :: CaptureOutput或 - depréférence,根据两个模块的作者David Golden - 在Capture :: Tiny.
您还可以手动编写所有代码,将STDERR重定向到缓冲区,以便进行发出警告的调用.
归档时间: |
|
查看次数: |
220 次 |
最近记录: |