Section 17.8.  Skipping Tests

Table of Contents

17.8. Skipping Tests

In some cases, we want to skip tests. For instance, some of our features may only work for a particular version of Perl, a particular operating system, or only when optional modules are available. To skip tests, we do much the same thing we did for the TODO tests, but Test::More does something much different.

In this example, we again use a naked block to create a section of code to skip, and we label it with SKIP. While testing, Test::More will not execute these tests, unlike the TODO block where it ran them anyway. At the start of the block, we call the skip( ) function to tell it why we want to skip the tests and how many tests we want to skip.

In this example, we check if the Mac::Speech module is installed before we try to test the say_it_aloud( ) method. If it isn't, the eval( ) block returns false and we execute the skip( ) function.

        skip 'Mac::Speech is not available', 1
                unless eval { require 'Mac::Speech' };

        ok( $tv_horse->say_it_aloud( 'I am Mr. Ed' );

When Test::More skips tests, it outputs special ok messages to keep the test numbering right and to tell the test harness what happened. Later, the test harness can report how many tests we skipped.

Don't skip tests because they aren't working right. Use the TODO block for that. We use SKIP when we want to make tests optional in certain circumstances.

Table of Contents
© 2000- NIV