Skipping Tests

     

Some tests should run only under certain conditions. For example, a network test to an external service makes sense only if an Internet connection is available, or an OS-specific test may run only on a certain platform. This lab shows how to skip tests that you know will never pass.

How do I do that?

Suppose that you're writing an English-to-Dutch translation program. The Phrase class stores some text and provides a constructor, an accessor, and an as_dutch( ) method that returns the text translated to Dutch.

Save the following code as Phrase.pm :

 package Phrase;     use strict;     sub new     {         my ( $class, $text ) = @_;         bless $text, $class;     }     sub text     {         my $self = shift;         return $$self;     }     sub as_dutch     {         my $self = shift;         require WWW::Babelfish;         return WWW::Babelfish->new->translate(             source      => 'English',             destination => 'Dutch',             text        => $self->text(  ),         );     }     1; 

A user may or may not have the WWW::Babelfish TRanslation module installed. That's fine; you've decided that Phrase 's as_dutch( ) feature is optional. How can you test that, though?

Save the following code as phrase.t :

 #!perl     use strict;     use Test::More tests => 3;     use Phrase;     my $phrase = Phrase->new('Good morning!');     isa_ok( $phrase, 'Phrase' );     is( $phrase->text(  ), 'Good morning!', "text(  ) access works" );     SKIP:     {         eval 'use WWW::Babelfish';         skip( 'because WWW::Babelfish required for as_dutch(  )', 1 ) if $@;         is(             $phrase->as_dutch,             'Goede ochtend!',             "successfully translated to Dutch"           );     } 

Run the test file with prove in verbose mode. If you have WWW::Babelfish installed, you will see the following output:

 $  prove -v phrase.t  phrase....1..3     ok 1 - The object isa Phrase     ok 2 - text(  ) access works     ok 3 - successfully translated to Dutch     ok     All tests successful.     Files=1, Tests=3,  1 wallclock secs ( 0.16 cusr +  0.01 csys =  0.17 CPU) 

If you run the test without WWW::Babelfish , you will see a different result:

 $  prove -v phrase.t  phrase....1..3     ok 1 - The object isa Phrase     ok 2 - text(  ) access works     ok 3 # skip because WWW::Babelfish required for as_dutch(  )     ok             1/3 skipped: because WWW::Babelfish required for as_dutch(  )     All tests successful, 1 subtest skipped.     Files=1, Tests=3,  0 wallclock secs ( 0.02 cusr +  0.00 csys =  0.02 CPU) 

What just happened ?

The test file begins with a Test::More declaration, as you've seen in the previous labs. The test file creates a sample Phrase object and also tests its constructor and text( ) accessor.

To skip the test for as_dutch( ) if the user does not have the WWW::Babelfish module installed requires a bit of special syntax. The test has a single block labeled SKIP , which begins by attempting to load the WWW::Babelfish module.


Note: You can have as many blocks labeled SKIP as you need. You can even nest them, as long as you label every nested block SKIP as well .

If trying to use WWW::Babelfish fails, eval will catch such an error and put it in the global variable $@ . Otherwise, it will clear that variable. If there's something in $@ , the function on the next line executes. skip( ) , yet another function helpfully exported by Test::More , takes two arguments: the reason to give for skipping the tests and the number of tests to skip. The previous case skips one test, explaining that the optional module is not available.

Even though the test for as_dutch( ) did not run, it counts as a success because marking it as a skipped test means that you expect it will never run under the given circumstances. If WWW::Babelfish were available, the test would run normally and its success or failure would count as a normal test.


Note: Test::Harness reports all skipped tests as successes because it's behavior that you anticipated .


Perl Testing. A Developer's Notebook
Perl Testing: A Developers Notebook
ISBN: 0596100922
EAN: 2147483647
Year: 2003
Pages: 107

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net