1Test::Simple(3) User Contributed Perl Documentation Test::Simple(3)
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
50 understand what your test is for. It's highly recommended you use
51 test 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
82 recommended 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.6.0.
134
135 Test::Simple is thread-safe in perl 5.8.1 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
140 complicated feature into the new Testing module. He observed that the
141 main problem is not dealing with these edge cases but that people hate
142 to write tests at all. What was needed was a dead simple module that
143 took all the hard work out of testing and was really, really easy to
144 learn. Paul Johnson simultaneously had this idea (unfortunately, he
145 wasn't in 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
152 Test::Simple in your programs and things will still work).
153
154 Look in Test::More's SEE ALSO for more testing modules.
155
157 Idea by Tony Bowden and Paul Johnson, code by Michael G Schwern
158 <schwern@pobox.com>, wardrobe by Calvin Klein.
159
161 Copyright 2001-2008 by Michael G Schwern <schwern@pobox.com>.
162
163 This program is free software; you can redistribute it and/or modify it
164 under the same terms as Perl itself.
165
166 See http://www.perl.com/perl/misc/Artistic.html
167
168
169
170perl v5.16.3 2011-02-23 Test::Simple(3)