~ubuntu-branches/ubuntu/trusty/bioperl/trusty

« back to all changes in this revision

Viewing changes to t/lib/Test/Simple.pm

  • Committer: Package Import Robot
  • Author(s): Charles Plessy
  • Date: 2013-09-22 13:39:48 UTC
  • mfrom: (3.1.11 sid)
  • Revision ID: package-import@ubuntu.com-20130922133948-c6z62zegjyp7ztou
Tags: 1.6.922-1
* New upstream release.
* Replaces and Breaks grinder (<< 0.5.3-3~) because of overlaping contents.
  Closes: #722910
* Stop Replacing and Breaking bioperl ( << 1.6.9 ): not needed anymore. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package Test::Simple;
2
 
 
3
 
use 5.004;
4
 
 
5
 
use strict 'vars';
6
 
use vars qw($VERSION @EXPORT);
7
 
$VERSION = '0.64';
8
 
$VERSION = eval $VERSION;    # make the alpha version come out as a number
9
 
 
10
 
use base qw(Test::Builder::Module);
11
 
@EXPORT = qw(ok);
12
 
 
13
 
my $CLASS = __PACKAGE__;
14
 
 
15
 
 
16
 
=head1 NAME
17
 
 
18
 
Test::Simple - Basic utilities for writing tests.
19
 
 
20
 
=head1 SYNOPSIS
21
 
 
22
 
  use Test::Simple tests => 1;
23
 
 
24
 
  ok( $foo eq $bar, 'foo is bar' );
25
 
 
26
 
 
27
 
=head1 DESCRIPTION
28
 
 
29
 
** If you are unfamiliar with testing B<read Test::Tutorial> first! **
30
 
 
31
 
This is an extremely simple, extremely basic module for writing tests
32
 
suitable for CPAN modules and other pursuits.  If you wish to do more
33
 
complicated testing, use the Test::More module (a drop-in replacement
34
 
for this one).
35
 
 
36
 
The basic unit of Perl testing is the ok.  For each thing you want to
37
 
test your program will print out an "ok" or "not ok" to indicate pass
38
 
or fail.  You do this with the ok() function (see below).
39
 
 
40
 
The only other constraint is you must pre-declare how many tests you
41
 
plan to run.  This is in case something goes horribly wrong during the
42
 
test and your test program aborts, or skips a test or whatever.  You
43
 
do this like so:
44
 
 
45
 
    use Test::Simple tests => 23;
46
 
 
47
 
You must have a plan.
48
 
 
49
 
 
50
 
=over 4
51
 
 
52
 
=item B<ok>
53
 
 
54
 
  ok( $foo eq $bar, $name );
55
 
  ok( $foo eq $bar );
56
 
 
57
 
ok() is given an expression (in this case C<$foo eq $bar>).  If it's
58
 
true, the test passed.  If it's false, it didn't.  That's about it.
59
 
 
60
 
ok() prints out either "ok" or "not ok" along with a test number (it
61
 
keeps track of that for you).
62
 
 
63
 
  # This produces "ok 1 - Hell not yet frozen over" (or not ok)
64
 
  ok( get_temperature($hell) > 0, 'Hell not yet frozen over' );
65
 
 
66
 
If you provide a $name, that will be printed along with the "ok/not
67
 
ok" to make it easier to find your test when if fails (just search for
68
 
the name).  It also makes it easier for the next guy to understand
69
 
what your test is for.  It's highly recommended you use test names.
70
 
 
71
 
All tests are run in scalar context.  So this:
72
 
 
73
 
    ok( @stuff, 'I have some stuff' );
74
 
 
75
 
will do what you mean (fail if stuff is empty)
76
 
 
77
 
=cut
78
 
 
79
 
sub ok ($;$) {
80
 
    $CLASS->builder->ok(@_);
81
 
}
82
 
 
83
 
 
84
 
=back
85
 
 
86
 
Test::Simple will start by printing number of tests run in the form
87
 
"1..M" (so "1..5" means you're going to run 5 tests).  This strange
88
 
format lets Test::Harness know how many tests you plan on running in
89
 
case something goes horribly wrong.
90
 
 
91
 
If all your tests passed, Test::Simple will exit with zero (which is
92
 
normal).  If anything failed it will exit with how many failed.  If
93
 
you run less (or more) tests than you planned, the missing (or extras)
94
 
will be considered failures.  If no tests were ever run Test::Simple
95
 
will throw a warning and exit with 255.  If the test died, even after
96
 
having successfully completed all its tests, it will still be
97
 
considered a failure and will exit with 255.
98
 
 
99
 
So the exit codes are...
100
 
 
101
 
    0                   all tests successful
102
 
    255                 test died or all passed but wrong # of tests run
103
 
    any other number    how many failed (including missing or extras)
104
 
 
105
 
If you fail more than 254 tests, it will be reported as 254.
106
 
 
107
 
This module is by no means trying to be a complete testing system.
108
 
It's just to get you started.  Once you're off the ground its
109
 
recommended you look at L<Test::More>.
110
 
 
111
 
 
112
 
=head1 EXAMPLE
113
 
 
114
 
Here's an example of a simple .t file for the fictional Film module.
115
 
 
116
 
    use Test::Simple tests => 5;
117
 
 
118
 
    use Film;  # What you're testing.
119
 
 
120
 
    my $btaste = Film->new({ Title    => 'Bad Taste',
121
 
                             Director => 'Peter Jackson',
122
 
                             Rating   => 'R',
123
 
                             NumExplodingSheep => 1
124
 
                           });
125
 
    ok( defined($btaste) && ref $btaste eq 'Film,     'new() works' );
126
 
 
127
 
    ok( $btaste->Title      eq 'Bad Taste',     'Title() get'    );
128
 
    ok( $btaste->Director   eq 'Peter Jackson', 'Director() get' );
129
 
    ok( $btaste->Rating     eq 'R',             'Rating() get'   );
130
 
    ok( $btaste->NumExplodingSheep == 1,        'NumExplodingSheep() get' );
131
 
 
132
 
It will produce output like this:
133
 
 
134
 
    1..5
135
 
    ok 1 - new() works
136
 
    ok 2 - Title() get
137
 
    ok 3 - Director() get
138
 
    not ok 4 - Rating() get
139
 
    #   Failed test 'Rating() get'
140
 
    #   in t/film.t at line 14.
141
 
    ok 5 - NumExplodingSheep() get
142
 
    # Looks like you failed 1 tests of 5
143
 
 
144
 
Indicating the Film::Rating() method is broken.
145
 
 
146
 
 
147
 
=head1 CAVEATS
148
 
 
149
 
Test::Simple will only report a maximum of 254 failures in its exit
150
 
code.  If this is a problem, you probably have a huge test script.
151
 
Split it into multiple files.  (Otherwise blame the Unix folks for
152
 
using an unsigned short integer as the exit status).
153
 
 
154
 
Because VMS's exit codes are much, much different than the rest of the
155
 
universe, and perl does horrible mangling to them that gets in my way,
156
 
it works like this on VMS.
157
 
 
158
 
    0     SS$_NORMAL        all tests successful
159
 
    4     SS$_ABORT         something went wrong
160
 
 
161
 
Unfortunately, I can't differentiate any further.
162
 
 
163
 
 
164
 
=head1 NOTES
165
 
 
166
 
Test::Simple is B<explicitly> tested all the way back to perl 5.004.
167
 
 
168
 
Test::Simple is thread-safe in perl 5.8.0 and up.
169
 
 
170
 
=head1 HISTORY
171
 
 
172
 
This module was conceived while talking with Tony Bowden in his
173
 
kitchen one night about the problems I was having writing some really
174
 
complicated feature into the new Testing module.  He observed that the
175
 
main problem is not dealing with these edge cases but that people hate
176
 
to write tests B<at all>.  What was needed was a dead simple module
177
 
that took all the hard work out of testing and was really, really easy
178
 
to learn.  Paul Johnson simultaneously had this idea (unfortunately,
179
 
he wasn't in Tony's kitchen).  This is it.
180
 
 
181
 
 
182
 
=head1 SEE ALSO
183
 
 
184
 
=over 4
185
 
 
186
 
=item L<Test::More>
187
 
 
188
 
More testing functions!  Once you outgrow Test::Simple, look at
189
 
Test::More.  Test::Simple is 100% forward compatible with Test::More
190
 
(i.e. you can just use Test::More instead of Test::Simple in your
191
 
programs and things will still work).
192
 
 
193
 
=item L<Test>
194
 
 
195
 
The original Perl testing module.
196
 
 
197
 
=item L<Test::Unit>
198
 
 
199
 
Elaborate unit testing.
200
 
 
201
 
=item L<Test::Inline>, L<SelfTest>
202
 
 
203
 
Embed tests in your code!
204
 
 
205
 
=item L<Test::Harness>
206
 
 
207
 
Interprets the output of your test program.
208
 
 
209
 
=back
210
 
 
211
 
 
212
 
=head1 AUTHORS
213
 
 
214
 
Idea by Tony Bowden and Paul Johnson, code by Michael G Schwern
215
 
E<lt>schwern@pobox.comE<gt>, wardrobe by Calvin Klein.
216
 
 
217
 
 
218
 
=head1 COPYRIGHT
219
 
 
220
 
Copyright 2001, 2002, 2004 by Michael G Schwern E<lt>schwern@pobox.comE<gt>.
221
 
 
222
 
This program is free software; you can redistribute it and/or 
223
 
modify it under the same terms as Perl itself.
224
 
 
225
 
See F<http://www.perl.com/perl/misc/Artistic.html>
226
 
 
227
 
=cut
228
 
 
229
 
1;