[ Team LiB ] Previous Section Next Section

14.4 Conditional Tests

If you write tests directly from the specification before you've written the code, the tests are expected to fail. You can include some of your tests inside a TODO block to include them for test count but denote them as unavailable at the same time. For example, suppose you haven't taught your horses how to talk yet:

use Test::More 'no_plan';

use_ok("Horse");
my $tv_horse = Horse->named("Mr. Ed");
TODO: {
  local $TODO = "haven't taught Horses to talk yet";

  can_ok($tv_horse, "talk");  # he can talk!
}
is($tv_horse->name, "Mr. Ed", "I am Mr. Ed!");

Here, the test is inside a TODO block, setting a package $TODO variable with the reason why the items are unfinished:[13]

[13] TODO tests require Test::Harness Version 2.0 or later, which comes with Perl 5.8, but in earlier releases, they have to be installed from the CPAN .

ok 1 - use Horse;
not ok 2 - Horse->can('talk') # TODO haven't taught Horses to talk yet
#     Failed (TODO) test (1.t at line 7)
#     Horse->can('talk') failed
ok 3 - I am Mr. Ed!
1..3

Note that the TODO test counts toward the total number of tests. Also note that the message about why the test is a TODO test is displayed as a comment. The comment has a special form, noted by the test harness, so you will see it during a make test run.

You can have multiple TODO tests in a given block, but only one reason per block, so it's best to group things that are related but use different blocks for different reasons.

    [ Team LiB ] Previous Section Next Section