Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: Test::Class and setup methods

by adrianh (Chancellor)
on Mar 14, 2007 at 12:11 UTC ( #604791=note: print w/replies, xml ) Need Help??

in reply to Test::Class and setup methods

Does this look like a bug in Test::Class or have I missed something here?
I think you've missed the little-used "+1" syntax for extending the number of tests run by a method in a subclass. For example:
{ package BaseClass; use base qw( Test::Class ); use Test::More; sub setup : Tests( setup => 1 ) { pass "base setup"; } sub test: Test { pass "the test"; } } { package SubClass; use base qw( BaseClass ); use Test::More; sub setup : Tests( setup => +1 ) { my $self = shift; $self->SUPER::setup; pass "subclass setup"; } } Test::Class->runtests; __END__ prove -v foo.t # # SubClass->test 1..5 ok 1 - base setup ok 2 - subclass setup ok 3 - the test # # BaseClass->test ok 4 - base setup ok 5 - the test ok 81 ms
Update: you can get the superclass setup methods to be called if if you don't override them by providing a subclass method of the same name, but then they're called in alphabetical order. As I've painfully discovered, that breaks things if your subclass setup method relies on something your superclass setup method creates.

I'm possible being dim - but I'm not sure what you're getting at here.

Replies are listed 'Best First'.
Re^2: Test::Class and setup methods
by Ovid (Cardinal) on Mar 14, 2007 at 12:25 UTC

    (setup => +1) syntax solves everything quite nicely. Thanks!

    As for the second thing, if a parent and derived class both have setup methods but the parent setup methods are not overridden in the child, the calling order should be parent methods first, not alphabetical first. Otherwise, when a subclasses' setup method just happens to have a test name that's alphabetically prior to parent classes' setup method names, the setup methods may very well get called in an inappropriate order.


    New address of my CGI Course.

      Check. Already on the todo list as the rather opaque "Use JUnit 4.0's inheritance model for before/after running order?" :-)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://604791]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2018-05-22 01:06 GMT
Find Nodes?
    Voting Booth?