Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

hide (4398)

  (email not shown publicly)

hide has been programming in Perl since 1998. Much of this time has been focused on system automation and data warehousing.


Journal of hide (4398)

Saturday January 08, 2005
10:20 PM

Testing the exception.

[ #22634 ]

When writing tests for my modules, my usual method is to create a separate .t file for each method within a module. This method does create quite a few .t files with generally few tests in each. I don't know whether this is a good or bad method but it's works great for me and the way I develop. .t files are created as I need each new method (starting with new), and tested with prove -lv (thanks for the -l Andy).

I tend to put Carp to good use. croak can be found throughout the modules I write wherever a method requires something (write access to a file, parameters passed, etc). To test that each croak works the way I expect I use Test::Exception's dies_ok.

In each .t file I'd have the following after the tests for proper use:

  eval { use Test::Exception };
  skip 'Test::Exception not installed', 1 if $@;

  dies_ok { test code here }, 'message';

Test::Exception is installed on all of my development boxes, and all of our production boxes to ensure full testing before a module gets installed. This caused me not to realize that the above code does not work if you don't have Test::Exception installed. Something I did not notice until I released CPAN::Mini::Inject onto CPAN.

One of the cpantesters and rjbs brought this to my attention on the Windows platform. I'd thought I'd fixed it with the last release, until today.

I spent today installing Perl and configuring remote access on one of the kids Windows XP systems (my plan is to use it to test my code and to as a cpantester). First thing, install CPANPLUS, second try to install CPAN::Mini::Inject. Failures all over the place. Can not find Test::Exception.

I ended up moving all of the exception tests out of each method's .t file and into a zz.exceptions.t file. The following code is at the top skipping every test if Test::Exception isn't installed:

use Test::More;

  eval "use Test::Exception";

  plan skip_all => "Test Exceptions required to test croaks" if $@;
  plan tests => 9; # # of tests.

Now the tests all pass on boxes with or without Test::Exception installed. Needless to say expect CPAN::Mini::Inject 0.16 shortly.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • Why not just bundle Test::Exception [] in with your module (purely for testing) so as to avoid the skipping of tests? It's only a single [] module file and passes tests across the board. It'd also save the user having to go and download it if they want to check all the tests, which enables the users to maximise their laziness while minimise their thought processes.

    broquaint out

    • Be careful to put your local copy of Test::Exception under t/ and not lib/ so that you don't accidentally install it as part of your distro.
      • Randal L. Schwartz
      • Stonehenge