Just to let folks know, at the Perl-QA Hackathon, I've implemented nested TAP 
in Test::Builder.  Andy Armstrong is working on the Test::Harness side.  It's 
completely backwards compatible and works with the current Test::Harness, but 
we're trying to expose more information via the harness.

This should solve many of the old 'plan/no plan' debates.  Here's how to assert 
a plan when you don't know how many tests are run.


#!/usr/bin/env perl

use strict;
use warnings;

# Gotta have a plan!
use Test::More tests => 3;

ok 1;
subtest 'some name' => sub {
    my $num_tests = 2 + int( rand(3) );
    plan tests => $num_tests;
    ok 1 for 1.. $num_tests - 1;
    subtest 'some name' => sub {
        plan 'no_plan';
        ok 1 for 1.. 2 + int( rand(3) );
    };
};
ok 1;
Note
how we have random plans and no plans, but we can still have a
top-level assertion of the number of tests. The output looks something
like this:
1..3
ok 1
   1..3
   ok 1
   ok 2
      ok 1
      ok 2
      ok 3
      ok 4
      1..4
   ok 3 - some name
ok 2 - some name
ok 3
ok
All tests successful.
Files=1, Tests=3,  0 wallclock secs ( 0.02 usr  0.00 sys +  0.02 cusr  0.00 
csys =  0.04 CPU)
Result: PASSA subtest which sets 'skip_all' will be reported as a skipped test 
and none of the code in the subtest after the plan will be executed:

  subtest 'skip me, baby', sub {
      plan skip_all => 'cuz I said so';
      ok 1, 'never gets run';
  };

So you can group difficult tests into subtests, call that one test, and not 
worry about how many tests are in the subtest unless you really want to (which 
you should, but it's not always cut and dried).

This will probably be merged into Test::Builder/Test::More soon, but until that 
time, you can get it at:

  http://github.com/Ovid/test-more/tree/master

 
Cheers,
Ovid
--
Buy the book         - http://www.oreilly.com/catalog/perlhks/
Tech blog            - http://use.perl.org/~Ovid/journal/
Twitter              - http://twitter.com/OvidPerl
Official Perl 6 Wiki - http://www.perlfoundation.org/perl6

Reply via email to