Testing the exception.

hide on 2005-01-09T03:20:01

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:

SKIP: {
  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; BEGIN { 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.


Include Test::Exception?

broquaint on 2005-01-10T01:35:04

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.

Re:Include Test::Exception?

merlyn on 2005-01-10T02:01:11

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.