Section 17.7.  A Testing To-Do List

Table of Contents

17.7. A Testing To-Do List

When we write tests before we write the code, the tests will initially fail. We might even add new features that temporarily fail while we are developing. There are several situations where we realize a test is going to fail, but we don't want to pay attention to its failure. The Test::More module realizes this and allows us to mark a test as TODO, meaning that we expect it to fail and we'll get to it later.

In this example, we know that we want to add the talk( ) method to our Horses class, but we haven't actually done it. We wrote the test already, since it was part of our specification. We know the test is going to fail, and that's okay. Test::More won't really count it as a failure.

use Test::More 'no_plan';

my $tv_horse = Horse->named('Mr. Ed');

  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!');

The naked block of code we labeled with TODO to mark the section of the tests that we expect to fail. Inside the block, we create a local version of $TODO, which holds as its value the reason we think the tests will fail. Test::More marks the test as a TODO test in the output, and the test harness[*] notices it and doesn't penalize us for the failure.[Section 17.7.  A Testing To-Do List]

[*] 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 CPAN.

[Section 17.7.  A Testing To-Do List] Although, if the test passes when we said it should fail, it warns us that the test unexpectedly passed. That might mean that the test doesn't fail when it should.

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!

Table of Contents
© 2000- NIV