File Coverage

blib/lib/Test/Unit/TestSuite.pm
Criterion Covered Total %
statement 95 122 77.8
branch 20 30 66.6
condition 11 17 64.7
subroutine 17 23 73.9
pod 6 15 40.0
total 149 207 71.9


line stmt bran cond sub pod time code
1             package Test::Unit::TestSuite;
2             BEGIN {
3 2     2   63 $Test::Unit::TestSuite::VERSION = '0.25_1325'; # added by dist-tools/SetVersion.pl
4             }
5 2     2   9 use strict;
  2         5  
  2         70  
6              
7             =head1 NAME
8              
9             Test::Unit::TestSuite - unit testing framework base class
10              
11             =cut
12              
13 2     2   9 use base 'Test::Unit::Test';
  2         5  
  2         1170  
14              
15 2     2   18 use Carp;
  2         5  
  2         138  
16              
17 2     2   13 use Test::Unit::Debug qw(debug);
  2         4  
  2         99  
18 2     2   1442 use Test::Unit::TestCase;
  2         5  
  2         68  
19 2     2   48 use Test::Unit::Loader;
  2         3  
  2         52  
20 2     2   1304 use Test::Unit::Warning;
  2         6  
  2         2969  
21              
22             =head1 SYNOPSIS
23              
24             package MySuite;
25              
26             use base qw(Test::Unit::TestSuite);
27              
28             sub name { 'My very own test suite' }
29             sub include_tests { qw(MySuite1 MySuite2 MyTestCase1 ...) }
30              
31             This is the easiest way of building suites; there are many more. Read on ...
32              
33             =head1 DESCRIPTION
34              
35             This class provides the functionality for building test suites in
36             several different ways.
37              
38             Any module can be a test suite runnable by the framework if it
39             provides a C method which returns a C
40             object, e.g.
41              
42             use Test::Unit::TestSuite;
43              
44             # more code here ...
45              
46             sub suite {
47             my $class = shift;
48              
49             # Create an empty suite.
50             my $suite = Test::Unit::TestSuite->empty_new("A Test Suite");
51             # Add some tests to it via $suite->add_test() here
52              
53             return $suite;
54             }
55              
56             This is useful if you want your test suite to be contained in the module
57             it tests, for example.
58              
59             Alternatively, you can have "standalone" test suites, which inherit directly
60             from C, e.g.:
61              
62             package MySuite;
63              
64             use base qw(Test::Unit::TestSuite);
65              
66             sub new {
67             my $class = shift;
68             my $self = $class->SUPER::empty_new();
69             # Build your suite here
70             return $self;
71             }
72              
73             sub name { 'My very own test suite' }
74              
75             or if your C is going to do nothing more interesting than add
76             tests from other suites and testcases via C, you can use the
77             C method as shorthand:
78              
79             package MySuite;
80              
81             use base qw(Test::Unit::TestSuite);
82              
83             sub name { 'My very own test suite' }
84             sub include_tests { qw(MySuite1 MySuite2 MyTestCase1 ...) }
85              
86             This is the easiest way of building suites.
87              
88             =head1 CONSTRUCTORS
89              
90             =head2 empty_new ([NAME])
91              
92             my $suite = Test::Unit::TestSuite->empty_new('my suite name');
93              
94             Creates a fresh suite with no tests.
95              
96             =cut
97              
98             sub empty_new {
99 30     30 1 120 my $this = shift;
100 30   33     135 my $classname = ref $this || $this;
101 30   100     128 my $name = shift || '';
102            
103 30         112 my $self = {
104             _Tests => [],
105             _Name => $name,
106             };
107 30         109 bless $self, $classname;
108            
109 30         147 debug(ref($self), "::empty_new($name) called\n");
110 30         83 return $self;
111             }
112              
113             =head2 new ([ CLASSNAME | TEST ])
114              
115             If a test suite is provided as the argument, it merely returns that
116             suite. If a test case is provided, it extracts all test case methods
117             from the test case (see L) into a new
118             test suite.
119              
120             If the class this method is being run in has an C method
121             which returns an array of class names, it will also automatically add
122             the tests from those classes into the newly constructed suite object.
123              
124             =cut
125              
126             sub new {
127 24     24 1 1065 my $class = shift;
128 24   100     84 my $classname = shift || ''; # Avoid a warning
129 24         104 debug("$class\::new($classname) called\n");
130              
131 24         102 my $self = $class->empty_new();
132              
133 24 100       68 if ($classname) {
134 22         86 Test::Unit::Loader::compile_class($classname);
135 22 100       64 if (eval { $classname->isa('Test::Unit::TestCase') }) {
  22 50       205  
136 21         77 $self->{_Name} = "suite extracted from $classname";
137 21         96 my @testcases = Test::Unit::Loader::extract_testcases($classname);
138 21         60 foreach my $testcase (@testcases) {
139 68         163 $self->add_test($testcase);
140             }
141             }
142 1         13 elsif (eval { $classname->can('suite') }) {
143 0         0 return $classname->suite();
144             }
145             else {
146 1         5 my $error = "Class $classname was not a test case or test suite.\n";
147             #$self->add_warning($error);
148 1         9 die $error;
149             }
150             }
151              
152 23 100       122 if ($self->can('include_tests')) {
153 2         10 foreach my $test ($self->include_tests()) {
154 2         14 $self->add_test($test);
155             }
156             }
157              
158 23         131 return $self;
159             }
160              
161             =head1 METHODS
162              
163             =cut
164              
165             sub suite {
166 1     1 0 2 my $class = shift;
167 1 50       5 croak "suite() is not an instance method" if ref $class;
168 1         5 $class->new(@_);
169             }
170              
171             =head2 name()
172              
173             Returns the suite's human-readable name.
174              
175             =cut
176              
177             sub name {
178 19     19 1 33 my $self = shift;
179 19 50       38 croak "Override name() in subclass to set name\n" if @_;
180 19         118 return $self->{_Name};
181             }
182              
183             =head2 names()
184              
185             Returns an arrayref of the names of all tests in the suite.
186              
187             =cut
188              
189             sub names {
190 0     0 1 0 my $self = shift;
191 0         0 my @test_list = @{$self->tests};
  0         0  
192 0 0       0 return [ map {$_->name} @test_list ] if @test_list;
  0         0  
193             }
194              
195             =head2 list (SHOW_TESTCASES)
196              
197             Produces a human-readable indented lists of the suite and the subsuites
198             it contains. If the first parameter is true, also lists any testcases
199             contained in the suite and its subsuites.
200              
201             =cut
202              
203             sub list {
204 0     0 1 0 my $self = shift;
205 0         0 my $show_testcases = shift;
206 0   0     0 my $first = ($self->name() || 'anonymous Test::Unit::TestSuite');
207 0 0       0 $first .= " - " . ref($self) unless ref($self) eq __PACKAGE__;
208 0         0 $first .= "\n";
209 0         0 my @lines = ( $first );
210 0         0 foreach my $test (@{ $self->tests() }) {
  0         0  
211 0         0 push @lines, map " $_", @{ $test->list($show_testcases) };
  0         0  
212             }
213 0         0 return \@lines;
214             }
215              
216             =head2 add_test (TEST_CLASSNAME | TEST_OBJECT)
217              
218             You can add a test object to a suite with this method, by passing
219             either its classname, or the object itself as the argument.
220              
221             Of course, there are many ways of getting the object too ...
222              
223             # Get and add an existing suite.
224             $suite->add_test('MySuite1');
225              
226             # This is exactly equivalent:
227             $suite->add_test(Test::Unit::TestSuite->new('MySuite1'));
228              
229             # So is this, provided MySuite1 inherits from Test::Unit::TestSuite.
230             use MySuite1;
231             $suite->add_test(MySuite1->new());
232              
233             # Extract yet another suite by way of suite() method and add it to
234             # $suite.
235             use MySuite2;
236             $suite->add_test(MySuite2->suite());
237            
238             # Extract test case methods from MyModule::TestCase into a
239             # new suite and add it to $suite.
240             $suite->add_test(Test::Unit::TestSuite->new('MyModule::TestCase'));
241              
242             =cut
243              
244             sub add_test {
245 135     135 1 206 my $self = shift;
246 135         152 my ($test) = @_;
247 135         898 debug('+ ', ref($self), "::add_test($test) called\n");
248 135 100       313 $test = Test::Unit::Loader::load_test($test) unless ref $test;
249             croak "`$test' could not be interpreted as a Test::Unit::Test object"
250 135 50       175 unless eval { $test->isa('Test::Unit::Test') };
  135         839  
251 135         178 push @{$self->tests}, $test;
  135         278  
252             }
253              
254             sub count_test_cases {
255 12     12 0 15 my $self = shift;
256 12         14 my $count;
257 12         13 $count += $_->count_test_cases for @{$self->tests};
  12         22  
258 12         45 return $count;
259             }
260              
261             sub run {
262 23     23 0 90 my $self = shift;
263 23         40 my ($result, $runner) = @_;
264              
265 23   100     178 debug("$self\::run($result, ", $runner || 'undef', ") called\n");
266              
267 23   33     75 $result ||= create_result();
268 23         85 $result->tell_listeners(start_suite => $self);
269              
270 23         69 $self->add_warning("No tests found in " . $self->name())
271 23 50       35 unless @{ $self->tests() };
272              
273 23         44 for my $t (@{$self->tests()}) {
  23         92  
274 126 100 100     672 if ($runner && $self->filter_test($runner, $t)) {
275 6         20 debug(sprintf "+ skipping '%s'\n", $t->name());
276 6         15 next;
277             }
278 119         358 debug(sprintf "+ didn't skip '%s'\n", $t->name());
279            
280 119 50       460 last if $result->should_stop();
281 119         655 $t->run($result, $runner);
282             }
283              
284 22         133 $result->tell_listeners(end_suite => $self);
285              
286 22         69 return $result;
287             }
288            
289             sub filter_test {
290 117     117 0 206 my $self = shift;
291 117         182 my ($runner, $test) = @_;
292              
293 117         734 debug(sprintf "checking whether to filter '%s'\n", $test->name);
294              
295 117         504 my @filter_tokens = $runner->filter();
296              
297 117         268 foreach my $token (@filter_tokens) {
298 16         66 my $filtered = $test->filter_method($token);
299 15 100       118 debug(" - by token $token? ", $filtered ? 'yes' : 'no', "\n");
300 15 100       69 return 1 if $filtered;
301             }
302              
303 110         431 return 0;
304             }
305              
306             sub test_at {
307 0     0 0 0 my $self = shift;
308 0         0 my ($index) = @_;
309 0         0 return $self->tests()->[$index];
310             }
311              
312             sub test_count {
313 0     0 0 0 my $self = shift;
314 0         0 return scalar @{$self->tests()};
  0         0  
315             }
316              
317             sub tests {
318 193     193 0 204 my $self = shift;
319 193         735 return $self->{_Tests};
320             }
321              
322             sub to_string {
323 0     0 0   my $self = shift;
324 0           return $self->name();
325             }
326              
327             sub add_warning {
328 0     0 0   my $self = shift;
329 0           $self->add_test(Test::Unit::Warning->new(join '', @_));
330             }
331              
332             1;
333             __END__