diff options
author | Peter Hutterer <peter.hutterer@who-t.net> | 2011-07-29 14:05:12 +1000 |
---|---|---|
committer | Peter Hutterer <peter.hutterer@who-t.net> | 2011-08-02 15:09:32 +1000 |
commit | 384b42dfc00576d85a4908a8edb66af8b527ba33 (patch) | |
tree | b62d5ac294f42b33596341b2fd6b73aa82719da4 | |
parent | fb4198a11fcbac51feb9137b794d85741b868199 (diff) |
Add support for Test suites.
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
-rw-r--r-- | ILTest/ILTest.pm | 86 | ||||
-rw-r--r-- | ILTest/TestSuite.pm | 44 | ||||
-rwxr-xr-x | iltest | 24 |
3 files changed, 138 insertions, 16 deletions
diff --git a/ILTest/ILTest.pm b/ILTest/ILTest.pm index 30d4266..890835c 100644 --- a/ILTest/ILTest.pm +++ b/ILTest/ILTest.pm @@ -63,13 +63,15 @@ sub new { bless ($self, $class); $self->{verbosity} = 0 if (not defined $self->{verbosity}); - if (not $self->{testee} or not $self->{tester}) { - $self->error("Invalid tester/testee\n"); - return undef; + if ($self->{suite}) { + $self->println(10, "Test suite: $self->{suite}"); + } else { + if (not $self->{testee} or not $self->{tester}) { + $self->error("Invalid tester/testee\n"); return undef; + } + $self->println(10, "Testee: $self->{testee} vs Tester: $self->{tester}"); } - $self->println(10, "Testee: $self->{testee} vs Tester: $self->{tester}"); - return $self; } @@ -190,7 +192,7 @@ Return the ILTest::TestCase Testee for this test. sub testee { my $self = shift; - if (not $self->{testee_ref}) { + if (not $self->{testee_ref} and defined $self->{testee}) { load $self->{testee}; $self->{testee_ref} = $self->{testee}->new(iltest => $self); } @@ -214,6 +216,21 @@ sub tester { return $self->{tester_ref}; } +=head2 B<$iltest-E<gt>suite> + +Return the ILTest::Suite for this test (if any). + +=cut +sub suite { + my $self = shift; + + if (not $self->{suite_ref} and defined $self->{suite}) { + load $self->{suite}; + $self->{suite_ref} = $self->{suite}->new(iltest => $self); + } + return $self->{suite_ref}; +} + sub _stage_pre() { my $self = shift; my $testee = shift; @@ -275,7 +292,7 @@ sub _stage_post() { =head2 B<$iltest-E<gt>test> -Run the test. +Run the test or test suite. Returns 0 on failure or 1 on success. @@ -285,10 +302,14 @@ sub test { my $testee = shift; my $tester = shift; - $testee = $self->testee if not defined $testee; - $tester = $self->tester if not defined $tester; + if ($self->suite) { + return $self->test_suite($self->suite); + } else { + $testee = $self->testee if not defined $testee; + $tester = $self->tester if not defined $tester; - return $self->test_case($testee, $tester); + return $self->test_case($testee, $tester); + } } sub test_case{ @@ -306,5 +327,50 @@ sub test_case{ return $rc; } +sub test_suite { + my $self = shift; + my $suite = shift; + my $error; + + $self->println(5, "SUITE STAGE PRE: ".$suite->name); + + $error = $suite->pre(); + if ($error) { + $self->error($error->msg); + return 0; + } + + $self->println(5, "SUITE STAGE RUN: ".$suite->name); + + $error = $suite->run(); + if ($error) { + $self->error($error->msg); + } + + $self->println(5, "SUITE STAGE POST: ".$suite->name); + $suite->post(); + + return (defined $error) ? 1 : 0; +} + +=head2 B<$iltest-E<gt>create_test($class, \%parameters)> + +Create and return the ILTest::TestCase $class, initialized with the +parameters provided. ILTest::ILTest may add madditional parameters. + +=cut + +sub create_test { + my $self = shift; + my $class = shift; + my $ref = shift; + + my %params = %{$ref} if $ref; + + $params{iltest} = $self; + + return $class->new(%params); +} + 1; # vim: set noexpandtab shiftwidth=8 tabstop=8: diff --git a/ILTest/TestSuite.pm b/ILTest/TestSuite.pm new file mode 100644 index 0000000..70ea72a --- /dev/null +++ b/ILTest/TestSuite.pm @@ -0,0 +1,44 @@ +#!/usr/bin/perl + +# Copyright © 2011 by Red Hat, Inc. +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +# +# Authors: Peter Hutterer <peter.hutterer@redhat.com> + + +=head1 NAME + +ILtest::TestSuite - A set of tests to run against a ILTest::TestCase. + +=head1 DESCRIPTION + +A TestSuite is a TestCase itself, but instead of the interleaved C<pre>, +C<run>, C<post> stages, each stage is called in-order. The TestSuite is +expected to run all tests itself in the C<run> stage or alternatively call +$self->iltest->test_case(testee, tester). + +If the C<pre> stage of a TestSuite fails, stages C<run> and C<post> are not +invoked. + +=cut + +package ILTest::TestSuite; + +use ILTest::TestCase; + +our @ISA = qw(ILTest::TestCase); + +1; @@ -46,7 +46,8 @@ sub usage () { print "Options:\n"; print " --verbose be verbose, very verbose\n"; print " --quiet don't print anything but test results\n"; - print " --suite tester is a ILTest::TestSuite\n"; + print " --suite tester is a ILTest::TestSuite. If running a suite,\n"; + print " the testee may be optional.\n"; exit(1); } @@ -63,12 +64,23 @@ $verbosity = 0 if ($quiet); my $testee = shift; my $tester = shift; -usage() if (not $testee or not $tester); +usage() if not $testee; +my $iltest; + +if ($suite) { + my $suite; + $suite = $tester ? $tester : $testee; + $testee = $tester ? $testee : undef; + $iltest = ILTest::ILTest->new(testee => $testee, + suite => $suite, + verbosity => $verbosity); +} else { + usage() if (not $testee or not $tester); + $iltest = ILTest::ILTest->new(testee => $testee, + tester => $tester, + verbosity => $verbosity); +} -my $iltest = ILTest::ILTest->new(testee => $testee, - tester => $tester, - suite => $suite, - verbosity => $verbosity); my $rc = $iltest->test(); if ($rc) { print "Test failed with code $rc\n"; |