On 31 May 2005, at 09:47, Adam Kennedy wrote:
[snip]
Something exist already that I'm missing?
[snip]

I'd use Test::Class (but I would say that :-) So the example from your POD would be something like:

----
{   package Foo::Test;
    use base qw( Test::Class );
    use Test::More;

# we take the ::Test suffix off to get the name of the class we're testing # (this should really be in a Test::Class base class - and will be soon)
    sub class_under_test {
        my $self = shift;
        my $test_class = ref $self;
        $test_class =~ s/::Test$//s;
        return $test_class;
    };

    # here is where we create our object under test
    sub create_fixture :Test( setup ) {
        my $self = shift;
        $self->{object} = $self->class_under_test->new;
    };

    # here we make sure foo returns true
    sub foo_returns_correct_value : Test {
        my $object = shift->{object};
        ok( $object->foo );
    };

# here we check answer returns 42 (just for the sake of another test)
    sub the_answer_to_live_the_universe_and_everything : Test {
        my $object = shift->{object};
        is( $object->answer, 42 );
    };
}

{   package FooBar::Test;
    use base qw( Foo::Test );
    use Test::More;

    # we just have to say what's different about FooBar objects, the
    # common behaviour stays the same
    sub foo_returns_correct_value : Test {
        my $object = shift->{object};
        is( $object->foo, 'bar' );
    };
}

Test::Class->runtests;
----

which would give us:

# FooBar::Test->foo_returns_correct_value
ok 1 - foo returns correct value
#
# FooBar::Test->the_answer_to_live_the_universe_and_everything
ok 2 - the answer to live the universe and everything
#
# Foo::Test->foo_returns_correct_value
ok 3 - foo returns correct value
#
# Foo::Test->the_answer_to_live_the_universe_and_everything
ok 4 - the answer to live the universe and everything

Cheers,

Adrian


Reply via email to