~drizzle-trunk/drizzle/development

0.67.24 by Bernt M. Johnsen
Added unit test framework and started on unit tests
1
##
2
## We use our own Runner class for better reporting and exit status
3
## that works with Hudson.
4
##
5
##
6
7
package RQGRunner;
8
use strict;
9
10
use base qw(Test::Unit::Runner); 
11
12
use Test::Unit; # for copyright & version number
13
use Test::Unit::TestSuite;
14
use Test::Unit::Loader;
15
use Test::Unit::Result;
16
17
use Data::Dumper;
18
19
use Benchmark;
20
21
my $exit_code = 0;
22
23
sub new {
24
    my $class = shift;
25
    my ($filehandle) = @_;
26
    $filehandle = \*STDOUT unless $filehandle;
27
    select((select($filehandle), $| = 1)[0]);
28
    bless { _Print_stream => $filehandle }, $class;
29
}
30
31
sub print_stream {
32
    my $self = shift;
33
    return $self->{_Print_stream};
34
}
35
36
sub _print {
37
    my $self = shift;
38
    my (@args) = @_;
39
    $self->print_stream->print(@args);
40
}
41
42
sub add_error {
43
    my $self = shift;
44
    my ($test, $exception) = @_; 
0.67.30 by Bernt M. Johnsen
Fixed unit test reporting
45
    my $tn = ref $test;
46
    $self->_print("Error: ".$tn."::".$test->name()."\n");
0.67.24 by Bernt M. Johnsen
Added unit test framework and started on unit tests
47
}
48
	
49
sub add_failure {
50
    my $self = shift;
51
    my ($test, $exception) = @_;
0.67.30 by Bernt M. Johnsen
Fixed unit test reporting
52
    my $tn = ref $test;
53
    $self->_print("Failure: ".$tn."::".$test->name()."\n");
0.67.24 by Bernt M. Johnsen
Added unit test framework and started on unit tests
54
}
55
56
sub add_pass {
57
    my $self = shift;
0.67.30 by Bernt M. Johnsen
Fixed unit test reporting
58
    my ($test, $exception) = @_;    my $tn = ref $test;
59
    my $tn = ref $test;
60
    $self->_print("Success: ".$tn."::".$test->name()."\n");
0.67.24 by Bernt M. Johnsen
Added unit test framework and started on unit tests
61
}
62
63
sub do_run {
64
    my $self = shift;
65
    my ($suite, $wait) = @_;
66
    my $result = $self->create_test_result();
67
    $result->add_listener($self);
68
    my $start_time = new Benchmark();
69
    $suite->run($result, $self);
70
    my $end_time = new Benchmark();
71
    
72
    $self->print_result($result, $start_time, $end_time);
73
    
74
    if ($wait) {
75
        print "<RETURN> to continue"; # go to STDIN any case
76
        <STDIN>;
77
    }
78
79
    $self->_print("\nTest was not successful.\n")
80
      unless $result->was_successful;
81
82
    return $result->was_successful;
83
}
84
85
sub end_test {
86
}
87
88
sub main {
89
    my $self = shift;
90
    my $a_test_runner = Test::Unit::TestRunner->new();
91
    $a_test_runner->start(@_);
92
}
93
94
sub print_result {
95
    my $self = shift;
96
    my ($result, $start_time, $end_time) = @_;
97
98
    my $run_time = timediff($end_time, $start_time);
99
    $self->_print("\n", "Time: ", timestr($run_time), "\n");
100
101
    $self->print_header($result);
102
    $self->print_errors($result);
103
    $self->print_failures($result);
104
}
105
106
sub print_errors {
107
    my $self = shift;
108
    my ($result) = @_;
109
    return unless my $error_count = $result->error_count();
110
    my $msg = "\nThere " .
111
              ($error_count == 1 ?
112
                "was 1 error"
113
              : "were $error_count errors") .
114
              ":\n";
115
    $self->_print($msg);
116
117
    my $i = 0;
118
    for my $e (@{$result->errors()}) {
119
        chomp(my $e_to_str = $e);
120
        $i++;
121
        $self->_print("$i) $e_to_str\n");
122
        $self->_print("\nAnnotations:\n", $e->object->annotations())
123
          if $e->object->annotations();
124
    }
125
}
126
127
sub print_failures {
128
    my $self = shift;
129
    my ($result) = @_;
130
    return unless my $failure_count = $result->failure_count;
131
    my $msg = "\nThere " .
132
              ($failure_count == 1 ?
133
                "was 1 failure"
134
              : "were $failure_count failures") .
135
              ":\n";
136
    $self->_print($msg);
137
138
    my $i = 0; 
139
    for my $f (@{$result->failures()}) {
140
        chomp(my $f_to_str = $f);
141
        $self->_print("\n") if $i++;
142
        $self->_print("$i) $f_to_str\n");
143
        $self->_print("\nAnnotations:\n", $f->object->annotations())
144
          if $f->object->annotations();
145
    }
146
}
147
148
sub print_header {
149
    my $self = shift;
150
    my ($result) = @_;
151
    if ($result->was_successful()) {
152
        $exit_code = 0;
153
        $self->_print("\n", "OK", " (", $result->run_count(), " tests)\n");
154
    } else {
155
        $exit_code = 1;
156
        $self->_print("\n", "!!!FAILURES!!!", "\n",
157
                      "Test Results:\n",
158
                      "Run: ", $result->run_count(), 
159
                      ", Failures: ", $result->failure_count(),
160
                      ", Errors: ", $result->error_count(),
161
                      "\n");
162
    }
163
}
164
165
sub run {
166
    my $self = shift;
167
    my ($class) = @_;
168
    my $a_test_runner = Test::Unit::TestRunner->new();
169
    $a_test_runner->do_run(Test::Unit::TestSuite->new($class), 0);
170
}
171
	
172
sub run_and_wait {
173
    my $self = shift;
174
    my ($test) = @_;
175
    my $a_test_runner = Test::Unit::TestRunner->new();
176
    $a_test_runner->do_run(Test::Unit::TestSuite->new($test), 1);
177
}
178
179
sub start {
180
    my $self = shift;
181
    my (@args) = @_;
182
183
    my $test = "";
184
    my $wait = 0;
185
186
    for (my $i = 0; $i < @args; $i++) {
187
        if ($args[$i] eq "-wait") {
188
            $wait = 1;
189
        } elsif ($args[$i] eq "-v") {
190
	    print Test::Unit::COPYRIGHT_SHORT;
191
        } else {
192
            $test = $args[$i];
193
        }
194
    }
195
    if ($test eq "") {
196
        die "Usage: TestRunner.pl [-wait] name, where name is the name of the Test class\n";
197
    }
198
    
199
    my $suite = Test::Unit::Loader::load($test);
200
    $self->do_run($suite, $wait);
201
    return $exit_code;
202
}
203
204
sub start_test {
205
    my $self = shift;
206
    my ($test) = @_;
207
}
208
209
1;
210
__END__
211
212
213
=head1 NAME
214
215
Test::Unit::TestRunner - unit testing framework helper class
216
217
=head1 SYNOPSIS
218
219
    use Test::Unit::TestRunner;
220
221
    my $testrunner = Test::Unit::TestRunner->new();
222
    $testrunner->start($my_test_class);
223
224
=head1 DESCRIPTION
225
226
This class is the test runner for the command line style use
227
of the testing framework.
228
229
It is used by simple command line tools like the F<TestRunner.pl>
230
script provided.
231
232
The class needs one argument, which is the name of the class
233
encapsulating the tests to be run.
234
235
=head1 OPTIONS
236
237
=over 4
238
239
=item -wait
240
241
wait for user confirmation between tests
242
243
=item -v
244
245
version info
246
247
=back
248
249
250
=head1 AUTHOR
251
252
Copyright (c) 2000-2002, 2005 the PerlUnit Development Team
253
(see L<Test::Unit> or the F<AUTHORS> file included in this
254
distribution).
255
256
All rights reserved. This program is free software; you can
257
redistribute it and/or modify it under the same terms as Perl itself.
258
259
=head1 SEE ALSO
260
261
=over 4
262
263
=item *
264
265
L<Test::Unit::TestCase>
266
267
=item *
268
269
L<Test::Unit::Listener>
270
271
=item *
272
273
L<Test::Unit::TestSuite>
274
275
=item *
276
277
L<Test::Unit::Result>
278
279
=item *
280
281
L<Test::Unit::TkTestRunner>
282
283
=item *
284
285
For further examples, take a look at the framework self test
286
collection (t::tlib::AllTests).
287
288
=back
289
290
=cut