Hi all,

I'm using Test::Class and I'm happy with it.
I wrote several test classes which inherit from T::C,
but I wanted to avoid the "1-*.t-script-for-each-test-class"
approach.

I thought of writing a "generic" *.t script that
I called `<nnn>-test-classes.t' which looks for
test packages inside the current filesystem subtree
and executes each with a separate perl process and
Test::Class->runtests() command.

I feel it's *ugly* but it works well.
Is there a way to avoid the 1 script per test class?

I recall trying directly with loading each class
and calling runtests() method inside the script
but that didn't work. It executed no tests at all.

I'm attaching it here, in case somebody wants to
comment on it. Just be kind to me, I know it's "bad".

--
Cosimo

#
# Loads all unit test classes derived from Test::Class
# and automatically executes the tests
#
# $Id$

use strict;
use warnings;

use lib '../../lib';             # When running from './t'
use lib '../lib';                # When running from tree root

use File::Find ();
use Test::Class;
use Test::More qw(no_plan);

my $lib = '-I../../lib -I../lib -I.';

# Find all unit test classes
our %class;
find_test_classes('./Test', './t/Test');

# Execute all the unit tests
for( keys %class ) {
    my $file = $_;
    my $pack = $class{$file};
    diag('---');
    diag('--- Executing Unit Test for class ', $pack, ' ---');
    diag('---');
    # I know it's "rude", but it works for now
    ok( 0 == system("perl $lib -MTest::Class -M$pack -le 
'Test::Class->runtests()'"), "$class{$file} unit test passed");
}

# TAP::Harness requires that at least one test has
# been executed, if not the test will be marked as DUBIOUS.
# [EMAIL PROTECTED] -- [Mon  4 Feb 2008 17:52:05 CET]
ok( 1, " Dummy test. Should never fail." );

sub find_test_classes {
    my(@dir) = @_;
    File::Find::find({
        wanted => \&is_test_class,
        follow => 1,
    }, @dir);
    return;
}

sub is_test_class {
    my $file;
    my $package;

    if (! defined $File::Find::fullname) {
        return;
    }

    # Avoid run the test several times, some files in .svn folders are included 
in the test below
    if ($File::Find::fullname !~ /\.pm$/) {
        return;
    }

    if (! open($file, '<', $File::Find::fullname)) {
        return;
    }

    # Try to find 'Test::Class' inside it
    while(<$file>) {
        if( /package\s+(.*);/ ) {
            $package = $1;
        }
        if( $package && /use\s+.*Test::Class.*;/ ) {
            $class{$File::Find::fullname} = $package;
            last;
        }
    }
    close $file;
    return;
}

Reply via email to