When running an applications test suite I want to promote all Perl compile and run-time warnings (eg the "Uninitialized variable" warning) to fatal errors so that I and the other developers investigate and fix the code generating the warning. But I only want to do this during development and CI testing. In production, the warnings should just stay as warnings.
I tried the following: In "t/lib" I created a module TestHelper.pm:
# TestHelper.pm
use warnings FATAL => qw( all );
1;
Then called the test suite like this:
$ PERL5LIB=$PERL5LIB:./t/lib PERL5OPT=-MTestHelper.pm prove -l t/*.t
But this did not have the desired effect of promoting all warnings to fatal errors. I got the warnings as normal but the warnings did not appear to be treated as fatal. Note that all my test.t scripts have the line "use warnings;" in them -- perhaps this over-rides the one in TestHelper.pm because "use warnings" has a local scope?
Instead I've done this:
# TestHelper.pm
# Promote all warnings to fatal
$SIG{__WARN__} = sub { die @_; };
1;
This works but has a code smell about it. I also don't like that it bombs out on the first warning. I'd rather the test suite ran in full, logging all warnings, but that the test status ultimately failed because the code ran with warnings.
Is there a better way to achieve this end result?
Warning in Perl is the most commonly used Pragma in Perl programming and is used to catch 'unsafe code'. A pragma is a specific module in Perl package which has the control over some functions of the compile time or Run time behavior of Perl, which is strict or warnings.
Ans: The -w Command-line option: It will display the list if warning messages regarding the code. strict pragma: It forces the user to declare all variables before they can be used using the my() function.
I think you're looking for Test::NoWarnings.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With