thanos1983 has asked for the wisdom of the Perl Monks concerning the following question:

Hello fellow Monks,

Recently I released the Unicode::Peek thanks to everyone suggestions and ideas. While I was still adding some test cases that every now and then that come to my mind I got this error today Can't locate Test/Exception.pm in....

I know what the error stands for, but I am impressed that this module is not installed on CPANPLUS. I have added some test cases on my module that I want to capture croak error message from Carp module.

When I searched online most articles that I found they suggest that this is the module that someone should use on die test cases. Sample of successful compiling test case code (test.t):

#########################

use utf8;
use strict;
use warnings;

use Test::More tests => 3;
BEGIN { use_ok('Test::Exception') };
BEGIN { use_ok('Unicode::Peek', qw( :all )) };

#########################

throws_ok { hexDumperOutput('Test', '這是一個測試') }
	  qr/Unknown encoding format 'Test'/,
	  'Check encoding formats hexDumperOutput';

__END__

$ perl t/test.t
1..3
ok 1 - use Test::Exception;
ok 2 - use Unicode::Peek;
ok 3 - Check encoding formats hexDumperOutput

I assumed that by default this is a core module and it would be installed by default but I guess I was wrong.

Does any one know any other way of testing croak? I have tried a few other suggestions e.g. Testing error handling that calls "die" but so far not successfully.

Thanks in advance for everyone time and effort reading and replying to my question.

Seeking for Perl wisdom...on the process of learning...not there...yet!
  • Comment on Error: Can't locate Test/Exception.pm in @INC (you may need to install the Test::Exception module) on CPANPLUS
  • Download Code

Replies are listed 'Best First'.
Re: Error: Can't locate Test/Exception.pm in @INC (you may need to install the Test::Exception module) on CPANPLUS
by haukex (Archbishop) on Sep 27, 2017 at 09:04 UTC
    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';

      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"); }
Re: Error: Can't locate Test/Exception.pm in @INC (you may need to install the Test::Exception module) on CPANPLUS
by hippo (Archbishop) on Sep 27, 2017 at 09:03 UTC
    I assumed that by default this is a core module and it would be installed by default but I guess I was wrong.

    Why assume when you can easily check? Also, do not assume that just because a module is in core that everyone has it installed. The solution is to declare all of your direct dependencies in the metadata, whether they are currently in core or not.

    (Updated: link to previous thread about core modules being absent)

      Hello hippo,

      Thanks for the link, I did not know how to check the core modules based on each version. Very handy... :D

      Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: Error: Can't locate Test/Exception.pm in @INC (you may need to install the Test::Exception module) on CPANPLUS
by 1nickt (Canon) on Sep 27, 2017 at 11:03 UTC

    • You need to add your dependencies to your Makefile, as others have said. Additionally, you should add the minimum Perl version required, so that installers like cpan don't try to install it on Perl versions older than you support. I see that you have use 5.013002; at the top of Makefile.PL, but the standard way to do it is with the MIN_PERL_VERSION setting.
      MIN_PERL_VERSION => "5.013002", PREREQ_PM => { # your module's runtime dependencies }, TEST_REQUIRES => { 'Test::Exception' => 0, }, ...
    • Also, your MANIFEST lists a test file t/Unicode-Peek.t but it is not present in the distro. You may want to explore automated distribution creation with Dist::Zilla or Dist::Milla or something like that, so the housekeeping (eg writing the MANIFEST) is taken care of for you.


    The way forward always starts with a minimal test.

      Hello 1nickt,

      Thanks a lot for your time and effort unpacking my module and suggesting all the necessary modifications.

      I was not aware that I should contain t/Unicode-Peek.t because I renamed my own test cases based on the names that I thought would describe and maintain the module easier. I will update the MANIFEST file as well.

      Again thanks for your time and effort reading and replying to my question, BR.

      Seeking for Perl wisdom...on the process of learning...not there...yet!