Making sure your modules all have their proper tests

petdance on 2002-08-08T16:00:57

All of my modules are works in progress. I'm always adding new features, especially to HTML::Lint, so I want to make sure I always have valid test cases for those new features. I try to follow the principle of "write the test case first", but in case I don't, or I forget to update the MANIFEST, I've created the following test files.

For HTML::Lint, in the t/01.coverage.t file: use Test::More 'no_plan';

use_ok( 'HTML::Lint::Error' );

my @errors = keys %HTML::Lint::Error::errors; isnt( scalar @errors, 0, 'There are at least some errors to be found.' );

for my $error ( @errors ) { my $filename = "t/$error.t"; ok( -e $filename, "$filename exists" ); }


and in the new Carp::Assert::More, in the t/02.coverage.t:

use Test::More 'no_plan';

use_ok( 'Carp::Assert::More' );

my @funcs = ( @Carp::Assert::More::EXPORT, @Carp::Assert::More::EXPORT_OK );

my %deduped; $deduped{$_}++ for @funcs; @funcs = sort keys %deduped;

isnt( scalar @funcs, 0, 'There are no function names!' );

for my $func ( @funcs ) { my $filename = "t/$func.t"; ok( -e $filename, "$filename exists" ); }


In each case, the code tracks the features (possible error messages in HTML::Lint, or function names in Carp::Assert::More) and makes sure that there's an appropriate .t file that corresponds.


I like it!

ajtaylor on 2002-08-09T01:45:08

This is an excellent idea. While most of the modules I'm writing these days do not use Exporter, I'm sure I could figure out a similar method you described. Perhaps you could grep through the package namespace looking for subroutine globs?

Re:I like it!

gav on 2002-08-10T05:15:08

Devel::GetSymbols might come in handy.

Re:I like it!

ajtaylor on 2002-08-13T16:36:03

Thanks for the idea. It looks quite useful.