Problems with Test::Class startup / setup inheritance
G'day,
I was using Test::Class perl module for some testing recently and ran into a strange problem. Basically, I have a base class inheriting from Test::Class
package Base::Class;
use base qw(Test::Class);
setup : Test(startup) {
# Create a DB from scratch
}
teardown : Test(shutdown) {
# DROP database
}
And then I have a whole bunch of test classes inheriting this base class,
package Some::Class;
use base qw(Base::Class);
sub actually_relevant_tests { }
But when I run my test script:
use Some::Class;
Test::Class->runtests;
The DB is created and dropped TWICE? Once for the base class and once for the sub-class! How do you avoid this without the solution being an ugly hack?
Thanks.
Edit: The closest thing to elegance I have right now is -
use Test::Class;
my $object = Some::Class->new();
Test::Class->runtests($object);
package Some::Class;
use Base::Class;
sub actually_re开发者_开发知识库levant_tests { }
But keeping question open for better solutions.
In your base class use:
sub SKIP_CLASS { shift eq __PACKAGE__ }
This ignores the Base::Class
during runtests as an acutal Test::Class
and accordingly startup/shutdown methods will only be called for Some::Class
.
Can you eliminate a layer of the inheritance hierarchy? Why not delegate setup and teardown to helper functions?
package My::DB::Helpers;
sub setup_db {...}
sub teardown_db {...}
and then
package Some::Class;
use My::DB::Helpers;
use base 'Test::Class';
setup : Test(startup) {
My::DB::Helpers::setup_db;
}
teardown : Test(shutdown) {
My::DB::Helpers::teardown_db;
}
精彩评论