Friday, June 19, 2009

Using Apache::Test with Test::Class

I've recently switched from simple Test::More scripts to trying Test::Class. I'm also revisiting some apache module testing with Apache::Test and friends. Today I tried to merge the two.

With Test::Class, you make a test runner script that imports modules and then executes tests. The tests are defined in modules, and then run in module import order by the Test::Class test harness. Test::Class has some nice convenience methods and sugar. You defined test subroutines by adding :Test(n) where n is the number of tests run in the method. Methods can be tagged to run at the start and finish of each test as well as at startup and shutdown, and you get a handy $self object passed around to the methods that you can use to store configuration and etc.

With Apache::TestRun, the standard boiler plate is to create a TEST.pl script which isa Apache::Test and calls __PACKAGE__->run_tests, which then runs all the .t tests in the directory.

To get them working together, I've created two wrappers. TEST.pl uses Apache::TestRun and then I have a test_wrapper.t which uses Test::Class and loads my tests where are defined in modules.

As I get this smoothed out a bit, I'll update. I've been planning to write a talk on "Using Apache::Test to test Apache modules" talk to have handy in case I need something for perl mongers or similar. I think adding Test::Class to the mix will nicely improve the signal-to-noise, assuming I can get it worked out in a non-cludgey manner.

#!/usr/bin/perl

use strict;
use warnings FATAL => 'all';

use FindBin;
use lib "$FindBin::Bin";

package My::TestRun;
use base 'Apache::TestRun';
use Apache::TestConfig;
__PACKAGE__->new->run(@ARGV)

No comments: