Commit | Line | Data |
4dd974da |
1 | package Test::Simple; |
2 | |
d020a79a |
3 | use 5.004; |
4dd974da |
4 | |
ccbd73a4 |
5 | use strict; |
6 | |
3e887aae |
7 | our $VERSION = '0.92'; |
ccbd73a4 |
8 | $VERSION = eval $VERSION; ## no critic (BuiltinFunctions::ProhibitStringyEval) |
d020a79a |
9 | |
b1ddf169 |
10 | use Test::Builder::Module; |
ccbd73a4 |
11 | our @ISA = qw(Test::Builder::Module); |
12 | our @EXPORT = qw(ok); |
4dd974da |
13 | |
b1ddf169 |
14 | my $CLASS = __PACKAGE__; |
4dd974da |
15 | |
4dd974da |
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 | |
d020a79a |
29 | ** If you are unfamiliar with testing B<read Test::Tutorial> first! ** |
30 | |
4dd974da |
31 | This is an extremely simple, extremely basic module for writing tests |
d020a79a |
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). |
4dd974da |
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 | |
9631ec48 |
40 | The only other constraint is you must pre-declare how many tests you |
4dd974da |
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 | |
89c1e84a |
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. |
4dd974da |
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 |
89c1e84a |
69 | what your test is for. It's highly recommended you use test names. |
4dd974da |
70 | |
71 | All tests are run in scalar context. So this: |
72 | |
73 | ok( @stuff, 'I have some stuff' ); |
74 | |
d020a79a |
75 | will do what you mean (fail if stuff is empty) |
4dd974da |
76 | |
77 | =cut |
78 | |
ccbd73a4 |
79 | sub ok ($;$) { ## no critic (Subroutines::ProhibitSubroutinePrototypes) |
80 | return $CLASS->builder->ok(@_); |
d020a79a |
81 | } |
82 | |
4dd974da |
83 | =back |
84 | |
85 | Test::Simple will start by printing number of tests run in the form |
86 | "1..M" (so "1..5" means you're going to run 5 tests). This strange |
87 | format lets Test::Harness know how many tests you plan on running in |
88 | case something goes horribly wrong. |
89 | |
90 | If all your tests passed, Test::Simple will exit with zero (which is |
91 | normal). If anything failed it will exit with how many failed. If |
92 | you run less (or more) tests than you planned, the missing (or extras) |
93 | will be considered failures. If no tests were ever run Test::Simple |
94 | will throw a warning and exit with 255. If the test died, even after |
95 | having successfully completed all its tests, it will still be |
96 | considered a failure and will exit with 255. |
97 | |
98 | So the exit codes are... |
99 | |
100 | 0 all tests successful |
b1ddf169 |
101 | 255 test died or all passed but wrong # of tests run |
4dd974da |
102 | any other number how many failed (including missing or extras) |
103 | |
104 | If you fail more than 254 tests, it will be reported as 254. |
105 | |
4dd974da |
106 | This module is by no means trying to be a complete testing system. |
89c1e84a |
107 | It's just to get you started. Once you're off the ground its |
4dd974da |
108 | recommended you look at L<Test::More>. |
109 | |
110 | |
111 | =head1 EXAMPLE |
112 | |
113 | Here's an example of a simple .t file for the fictional Film module. |
114 | |
115 | use Test::Simple tests => 5; |
116 | |
117 | use Film; # What you're testing. |
118 | |
119 | my $btaste = Film->new({ Title => 'Bad Taste', |
120 | Director => 'Peter Jackson', |
121 | Rating => 'R', |
122 | NumExplodingSheep => 1 |
123 | }); |
5143c659 |
124 | ok( defined($btaste) && ref $btaste eq 'Film, 'new() works' ); |
4dd974da |
125 | |
126 | ok( $btaste->Title eq 'Bad Taste', 'Title() get' ); |
d020a79a |
127 | ok( $btaste->Director eq 'Peter Jackson', 'Director() get' ); |
4dd974da |
128 | ok( $btaste->Rating eq 'R', 'Rating() get' ); |
129 | ok( $btaste->NumExplodingSheep == 1, 'NumExplodingSheep() get' ); |
130 | |
131 | It will produce output like this: |
132 | |
133 | 1..5 |
134 | ok 1 - new() works |
135 | ok 2 - Title() get |
136 | ok 3 - Director() get |
137 | not ok 4 - Rating() get |
b1ddf169 |
138 | # Failed test 'Rating() get' |
139 | # in t/film.t at line 14. |
4dd974da |
140 | ok 5 - NumExplodingSheep() get |
d020a79a |
141 | # Looks like you failed 1 tests of 5 |
4dd974da |
142 | |
143 | Indicating the Film::Rating() method is broken. |
144 | |
145 | |
146 | =head1 CAVEATS |
147 | |
148 | Test::Simple will only report a maximum of 254 failures in its exit |
149 | code. If this is a problem, you probably have a huge test script. |
150 | Split it into multiple files. (Otherwise blame the Unix folks for |
151 | using an unsigned short integer as the exit status). |
152 | |
d020a79a |
153 | Because VMS's exit codes are much, much different than the rest of the |
154 | universe, and perl does horrible mangling to them that gets in my way, |
155 | it works like this on VMS. |
156 | |
157 | 0 SS$_NORMAL all tests successful |
158 | 4 SS$_ABORT something went wrong |
159 | |
160 | Unfortunately, I can't differentiate any further. |
161 | |
162 | |
163 | =head1 NOTES |
164 | |
165 | Test::Simple is B<explicitly> tested all the way back to perl 5.004. |
166 | |
a344be10 |
167 | Test::Simple is thread-safe in perl 5.8.0 and up. |
4dd974da |
168 | |
169 | =head1 HISTORY |
170 | |
171 | This module was conceived while talking with Tony Bowden in his |
172 | kitchen one night about the problems I was having writing some really |
173 | complicated feature into the new Testing module. He observed that the |
174 | main problem is not dealing with these edge cases but that people hate |
175 | to write tests B<at all>. What was needed was a dead simple module |
176 | that took all the hard work out of testing and was really, really easy |
177 | to learn. Paul Johnson simultaneously had this idea (unfortunately, |
178 | he wasn't in Tony's kitchen). This is it. |
179 | |
180 | |
4dd974da |
181 | =head1 SEE ALSO |
182 | |
183 | =over 4 |
184 | |
185 | =item L<Test::More> |
186 | |
187 | More testing functions! Once you outgrow Test::Simple, look at |
188 | Test::More. Test::Simple is 100% forward compatible with Test::More |
9631ec48 |
189 | (i.e. you can just use Test::More instead of Test::Simple in your |
4dd974da |
190 | programs and things will still work). |
191 | |
4dd974da |
192 | =back |
193 | |
ccbd73a4 |
194 | Look in Test::More's SEE ALSO for more testing modules. |
195 | |
9631ec48 |
196 | |
197 | =head1 AUTHORS |
198 | |
199 | Idea by Tony Bowden and Paul Johnson, code by Michael G Schwern |
200 | E<lt>schwern@pobox.comE<gt>, wardrobe by Calvin Klein. |
201 | |
202 | |
203 | =head1 COPYRIGHT |
204 | |
ccbd73a4 |
205 | Copyright 2001-2008 by Michael G Schwern E<lt>schwern@pobox.comE<gt>. |
9631ec48 |
206 | |
207 | This program is free software; you can redistribute it and/or |
208 | modify it under the same terms as Perl itself. |
209 | |
a9153838 |
210 | See F<http://www.perl.com/perl/misc/Artistic.html> |
9631ec48 |
211 | |
4dd974da |
212 | =cut |
213 | |
214 | 1; |