in reply to Error: Can't locate Test/Exception.pm in @INC (you may need to install the Test::Exception module) on CPANPLUS

I am impressed that this module is not installed

You should add it as a dependency to your Makefile.PL, as you should with all your dependencies (see ExtUtils::MakeMaker):

use ExtUtils::MakeMaker 6.64; WriteMakefile( ... PREREQ_PM => { # your module's runtime dependencies }, TEST_REQUIRES => { 'Test::Exception' => 0, }, );
I assumed that by default this is a core module and it would be installed by default but I guess I was wrong.

It is not a core module, as you can check with corelist:

$ corelist "/Test::Exception/" /Test::Exception/ has no match in CORE (or so I think)
Does any one know any other way of testing croak?

Personally I use Test::Fatal, but I think Test::Exception is a good choice too.

use Test::More; use Test::Fatal 'exception'; like exception { die "Blam"; }, qr/\bBlam\b/, 'exception is as expected';
  • Comment on Re: Error: Can't locate Test/Exception.pm in @INC (you may need to install the Test::Exception module) on CPANPLUS
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: Error: Can't locate Test/Exception.pm in @INC (you may need to install the Test::Exception module) on CPANPLUS
by thanos1983 (Parson) on Sep 27, 2017 at 09:20 UTC

    Hello haukex,

    Thanks for the complete analysis and solution to my problem.

    I kept looking online and I found also that most people also propose to use Test::Fatal I will give it a try. Thanks again on showing me how to include it on my Makefile.PL I completely forgot about that, since the last module that I wrote was almost 4 years ago.

    On the mean time, I was experimenting and I also found a successful way of testing croak without the use of any secondary test module but with the use of Test::More/like (fellow monk toolic proposed this on Testing error handling that calls "die"), sample below:

    $@ = '';
    eval { hexDumperOutput('Test', '這是一個測試') };
    like( $@, qr/Unknown encoding format 'Test'/,
          'Check encoding formats hexDumperOutput' );
    

    Again thanks a lot for your time and effort, it helped me a lot.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
      like( $@, qr/Unknown encoding format 'Test'/,

      Yes, despite some long-standing issues with $@, since this code is explicitly checking for a specific value, that should work ok too, without any extra modules. Test::Fatal uses Try::Tiny internally, which works around some of the issues with $@. Three of my four CPAN modules use Test::Fatal extensively for testing with no problems across all of CPAN Testers.

      testing croak without the use of any secondary test module

      I realized the other day that since all I was using from Test::Fatal was its exception function, I could successfully replace it with the following, with no changes to my tests necessary. However, this still theoretically suffers from the same problems I linked to earlier.

      sub exception (&) { return eval { shift->(); 1 } ? undef : ($@ || confess "\$@ was false"); }