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
39 it's true, the test passed. If it's false, it didn't. That's
40 about it.
41
42 "ok()" prints out either "ok" or "not ok" along with a test number
43 (it keeps track of that for you).
44
45 # This produces "ok 1 - Hell not yet frozen over" (or not ok)
46 ok( get_temperature($hell) > 0, 'Hell not yet frozen over' );
47
48 If you provide a $name, that will be printed along with the "ok/not
49 ok" to make it easier to find your test when if fails (just search
50 for the name). It also makes it easier for the next guy to
51 understand what your test is for. It's highly recommended you use
52 test names.
53
54 All tests are run in scalar context. So this:
55
56 ok( @stuff, 'I have some stuff' );
57
58 will do what you mean (fail if stuff is empty)
59
60 Test::Simple will start by printing number of tests run in the form
61 "1..M" (so "1..5" means you're going to run 5 tests). This strange
62 format lets Test::Harness know how many tests you plan on running in
63 case something goes horribly wrong.
64
65 If all your tests passed, Test::Simple will exit with zero (which is
66 normal). If anything failed it will exit with how many failed. If you
67 run less (or more) tests than you planned, the missing (or extras) will
68 be considered failures. If no tests were ever run Test::Simple will
69 throw a warning and exit with 255. If the test died, even after having
70 successfully completed all its tests, it will still be considered a
71 failure and will exit with 255.
72
73 So the exit codes are...
74
75 0 all tests successful
76 255 test died or all passed but wrong # of tests run
77 any other number how many failed (including missing or extras)
78
79 If you fail more than 254 tests, it will be reported as 254.
80
81 This module is by no means trying to be a complete testing system.
82 It's just to get you started. Once you're off the ground its
83 recommended you look at Test::More.
84
86 Here's an example of a simple .t file for the fictional Film module.
87
88 use Test::Simple tests => 5;
89
90 use Film; # What you're testing.
91
92 my $btaste = Film->new({ Title => 'Bad Taste',
93 Director => 'Peter Jackson',
94 Rating => 'R',
95 NumExplodingSheep => 1
96 });
97 ok( defined($btaste) && ref $btaste eq 'Film', 'new() works' );
98
99 ok( $btaste->Title eq 'Bad Taste', 'Title() get' );
100 ok( $btaste->Director eq 'Peter Jackson', 'Director() get' );
101 ok( $btaste->Rating eq 'R', 'Rating() get' );
102 ok( $btaste->NumExplodingSheep == 1, 'NumExplodingSheep() get' );
103
104 It will produce output like this:
105
106 1..5
107 ok 1 - new() works
108 ok 2 - Title() get
109 ok 3 - Director() get
110 not ok 4 - Rating() get
111 # Failed test 'Rating() get'
112 # in t/film.t at line 14.
113 ok 5 - NumExplodingSheep() get
114 # Looks like you failed 1 tests of 5
115
116 Indicating the Film::Rating() method is broken.
117
119 Test::Simple will only report a maximum of 254 failures in its exit
120 code. If this is a problem, you probably have a huge test script.
121 Split it into multiple files. (Otherwise blame the Unix folks for
122 using an unsigned short integer as the exit status).
123
124 Because VMS's exit codes are much, much different than the rest of the
125 universe, and perl does horrible mangling to them that gets in my way,
126 it works like this on VMS.
127
128 0 SS$_NORMAL all tests successful
129 4 SS$_ABORT something went wrong
130
131 Unfortunately, I can't differentiate any further.
132
134 Test::Simple is explicitly tested all the way back to perl 5.6.0.
135
136 Test::Simple is thread-safe in perl 5.8.1 and up.
137
139 This module was conceived while talking with Tony Bowden in his kitchen
140 one night about the problems I was having writing some really
141 complicated feature into the new Testing module. He observed that the
142 main problem is not dealing with these edge cases but that people hate
143 to write tests at all. What was needed was a dead simple module that
144 took all the hard work out of testing and was really, really easy to
145 learn. Paul Johnson simultaneously had this idea (unfortunately, he
146 wasn't in Tony's kitchen). This is it.
147
149 Test::More
150 More testing functions! Once you outgrow Test::Simple, look at
151 Test::More. Test::Simple is 100% forward compatible with
152 Test::More (i.e. you can just use Test::More instead of
153 Test::Simple in your programs and things will still work).
154
155 Look in Test::More's SEE ALSO for more testing modules.
156
158 Idea by Tony Bowden and Paul Johnson, code by Michael G Schwern
159 <schwern@pobox.com>, wardrobe by Calvin Klein.
160
162 Chad Granum <exodist@cpan.org>
163
165 Copyright 2001-2008 by Michael G Schwern <schwern@pobox.com>.
166
167 This program is free software; you can redistribute it and/or modify it
168 under the same terms as Perl itself.
169
170 See http://www.perl.com/perl/misc/Artistic.html
171
172
173
174perl v5.34.0 2022-03-05 Test::Simple(3)