Silence warning in TAP

There is a test for bad API call in my code, fortunately, this code leads to a warning of the module itself. But when I test a failed API call, I don't want to see a warning in 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 

Here is the actual code

 #!/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' ); 

is there any way to cancel the warning and prevent it from completing in TAP (outside no warnings in the source module).

+4
source share
3 answers
 local $SIG{__WARN__} = sub {}; 

temporarily disables alerts.

+4
source

You may be looking for Test :: Warn . It is easy to use:

 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; 

So, you are turning a warning from trouble into something expected.

If this is not what you want, look at IO :: CaptureOutput or - de prรฉfรฉrence, according to the author of both modules, David Golden - at Capture :: Tiny .

You can also encode everything manually by redirecting STDERR to the buffer while you call the call that issues the warning.

+6
source

Another warning module is Test :: NoWarnings . This checks that your code does not raise any warnings, and if the test fails, you cannot run the test. You can ignore known warnings, such as OP, assuming that this is the desired behavior (when working with $, we ignore warnings from several noisy CPAN modules, such as PDF :: API2). He can

In general, I would agree with Schwern, although I will try to fix the warnings, not the cover of $SIG{__WARN__} . One of the main advantages of tests is to detect errors that occur when changing another code - your test file does not just check that the code you just wrote is ok, but that the code you wrote will still be ok in the future when the CPAN modules and the rest of your application are updated.

0
source

All Articles