O'Reilly logo

Perl Testing: A Developer's Notebook by Chromatic, Ian Langworth

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Inheriting Tests

Your boss thinks highly of your new, shiny Queue module. “Great,” she says, “but we need a subclass that will enqueue only single, unhyphenated words.” Before you became a confident tester, this might have worried you. It’s not scary anymore, though.[1] Thanks to Test::Class, there’s not much more to do.

This lab explains how to write tests for subclasses when you already have Test::Class tests for their parents.

How do I do that?

A subclass inherits from a parent class, so why not have tests inherit from a parent test? Except for the enqueue() method, the features of the two classes are the same. Because the tests for Queue enqueue only words, you can reuse the test methods declared in Queue::Test.

Create the directory Queue/Word/, and save the following as Queue/Word/Test.pm:

 package Queue::Word::Test; use base 'Queue::Test'; use Queue::Word; use Test::More; use Test::Exception; sub setup_queues : Test( setup => 2 ) { my ($self) = @_; $self->{empty} = Queue::Word->new(); $self->{twoitems} = Queue::Word->new(qw( howdy bonjour )); isa_ok( $self->{$_}, 'Queue::Word' ) for qw( empty twoitems ); } sub check_only_words : Test(5) { my ($self) = @_; lives_ok { $self->{empty}->enqueue('wassup') } "can enqueue words"; lives_ok { $self->{empty}->enqueue('HeLlO') } "case doesn't matter"; dies_ok { $self->{empty}->enqueue(1981) } "can't enqueue integers"; dies_ok { $self->{empty}->enqueue(10.9) } "can't enqueue decimal"; dies_ok { $self->{empty}->enqueue('Transzorp Diode') } "can't ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required