Skipping All Tests

     

The preceding lab demonstrated how to skip certain tests under certain conditions. You may find cases where an entire test file shouldn't run ”for example, when testing platform X-specific features on platform Y will produce no meaningful results. Test::More provides a bit of useful syntax for this situation.

How do I do that?

Use the plan function on its own instead of specifying the tests in the use( ) statement. The following code checks to see if the current weekday is Tuesday. If it is not, the test will skip all of the tests. Save it as skip_all.t :

 use Test::More;     if ( [ localtime ]->[6] != 2 )     {         plan( skip_all => 'only run these tests on Tuesday' );     }     else     {         plan( tests => 1 );     }     require Tuesday;     my $day = Tuesday->new(  );     ok( $day->coat(  ), 'we brought our coat' ); 

Tuesday.pm is very simple:

 package Tuesday;     sub new     {         bless {  }, shift;     }     # wear a coat only on Tuesday     sub coat     {         return [ localtime ]->[6] =  = 2;     }     1; 

Run this test file on a Tuesday to see the following output:

 $  prove -v skip_all.t  chapter_01/skipping_all_tests....1..1     ok 1 - we brought our coat     ok     All tests successful.     Files=1, Tests=1,  1 wallclock secs ( 0.13 cusr +  0.04 csys =  0.17 CPU) 


Note: A real test file would have more tests; this is just an example .

Run it on any other day of the week to skip all of the tests:

 $  prove -v skip_all.t  chapter_01/skipping_all_tests....1..0 # Skip only run these tests on Tuesday skipped             all skipped: only run these tests on Tuesday     All tests successful, 1 test skipped.     Files=1, Tests=0,  0 wallclock secs ( 0.14 cusr +  0.05 csys =  0.19 CPU) 

What just happened ?

Instead of immediately reporting the test plan by passing extra arguments to the use keyword, skip_all.t uses Test::More 's plan( ) function to determine the test plan when the script runs. If the current weekday is not Tuesday, the code calls plan( ) with two arguments: an instruction to run no tests and a reason why. If it is Tuesday, the code reports the regular test plan and execution continues as normal.



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