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